Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Super Contributor
tolmachevroman
Posts: 257
Registered: ‎05-05-2011
My Carrier: AT&T
Accepted Solution

Play music in background

Hi everyone,

 I wonder if it is possible to simultaneously play e.g. a game and a music file from the phone? I mean I want to create an app which could open user's music files and play them in the game. Is it possible? What limitations should I expect?

 

Please use plain text.
Developer
superdirt
Posts: 535
Registered: ‎05-17-2009

Re: Play music in background

I'm not sure if this has been changed but it used to be that it was impossible to play two sounds simultaneously on the BlackBerry.

 

Scott

Please use plain text.
Super Contributor
tolmachevroman
Posts: 257
Registered: ‎05-05-2011
My Carrier: AT&T

Re: Play music in background

thanx for replying :smileyhappy: Actually, I don't want 2 sounds to play simulataneously, I want a song playing in the background or a way to open user's songs in the application and play them.

Please use plain text.
Developer
Aman_vivek
Posts: 216
Registered: ‎02-18-2010
My Carrier: no

Re: Play music in background

You can play song in background by creating the instance of Player class. For opening the user's song, you can get the all song list programatically by using FileConnection.

Please use plain text.
Super Contributor
tolmachevroman
Posts: 257
Registered: ‎05-05-2011
My Carrier: AT&T

Re: Play music in background

thank you, I'll try it today and post the results

Please use plain text.
Super Contributor
tolmachevroman
Posts: 257
Registered: ‎05-05-2011
My Carrier: AT&T

Re: Play music in background

[ Edited ]

So far I've tested FileConnection, but have a few questions. I use the snippet from documentation:

 

 try {
    FileConnection fconn = (FileConnection)Connector.open("file:///store/home/user/music/");
     // If no exception is thrown, then the URI is valid, but the file may or may not exist.
         if (!fconn.exists())
           fconn.create();  // create the file if it doesn't exist
           for( Enumeration e = fconn.list();  e.hasMoreElements();) {
            System.out.println("___________________________HAS: " + e.nextElement());
            	}
            fconn.close();
        }
        catch (IOException ioe) {
        	System.out.println("________________________________________EXCEPTION");
        }

 

I tested it on 9550 simulator where I found a sample song "MusicDemo - World Traveller" sitting in Media/Music. But I tried both music and media in path string and it showed empty directories.  What I did wrong? Another question comes from the first one - where do people usually store their music files? On SD? Maybe it's better then to simulate SD and try to load music file from the PC?

Please use plain text.
Developer
Aman_vivek
Posts: 216
Registered: ‎02-18-2010
My Carrier: no

Re: Play music in background

try this

 

public void getFiles(String root)
	{
		try{
	//	FileConnection fc = (FileConnection)Connector.open("file:///" +root);
			FileConnection fc = (FileConnection)Connector.open("fileconn.dir.photos.name");
		if(fc!=null)
		{
		Enumeration e = fc.list();
		
		while(e.hasMoreElements())
		{
			String path=(String)e.nextElement();
			
			if(path.endsWith(".jpg")||path.endsWith(".png")||path.endsWith(".gif"))
			{
				
				byte []data=getData("file:///"+root+path);
				vector.addElement(data);
				//Bitmap image = Bitmap.createBitmapFromBytes( data, 0, -1, 4 );
				//add(new BitmapField(image,FOCUSABLE));
				
				
			add(new LabelField("Path :"+root+path)
			{
				protected void paint(Graphics graphics) {
					// TODO Auto-generated method stub
					graphics.setColor(Color.WHITE);
					super.paint(graphics);
				}
			});
		
			}
			getFiles(root+path);
		}
		}
		
		
		}catch(Exception e)
		{
			
		}

	}

 and call this function:

 

Enumeration e=FileSystemRegistry.listRoots();
		
		while(e.hasMoreElements())
		{
			
			String root=(String)e.nextElement();
		
			getFiles(root);
		
		}

 This function return all path of file which have  extension .jpg, .png, .gif

Please use plain text.
Developer
girishthakr
Posts: 232
Registered: ‎12-15-2008
My Carrier: Vodafone India

Re: Play music in background

In above code instead of

if(path.endsWith(".jpg")||path.endsWith(".png")||path.endsWith(".gif"))



Check If

 

if(path.endWith(.avi) || path.endWith(.3gp) || path.endWith(.mp4) || path.endWith(.mov) || path.endWith(.m4a) || path.endWith(.aac) )

 

 

U wil get the music files only...

 

Also in place of

FileConnection fc = (FileConnection)Connector.open("fileconn.dir.photos.name");



put "fileconn.dir.music "  to search from Device

 

and "fileconn.dir.memorycard.music " to search from MEmory Card.

 

Thnx :smileyhappy:

Please use plain text.
Super Contributor
tolmachevroman
Posts: 257
Registered: ‎05-05-2011
My Carrier: AT&T

Re: Play music in background

[ Edited ]

thank you guys, finally I managed to find this default song. I modified your code a bit:

 

try{
	FileConnection fc = (FileConnection)Connector.open("file:///" +root);
      //FileConnection fc = (FileConnection)Connector.open("fileconn.dir.music");
		if(fc!=null)
		{
		Enumeration e = fc.list();
		
		while(e.hasMoreElements())
		{
			String path=(String)e.nextElement();
			
	if(path.endsWith(".aac")||path.endsWith(".m4a")||path.endsWith(".mp4"))
			{	
	System.out.println("___________________________________HAS: " + root+path);
			}
			getFiles(root+path);
		}
		}		
		}catch(Exception e)	{}

 

  In the console I got:

 

 

[0.0] ___________________________________HAS: store/home/user/MusicDemo - World Traveller.aac

  

  The only question I have - why did you use "fileconn.dir..."? Where did you find this? :smileyhappy: I guess it depends on OS, I tested on 5.0 9550 default simulator.

Please use plain text.
Contributor
albertpv
Posts: 49
Registered: ‎12-16-2010
My Carrier: Vodafone

Re: Play music in background

Don't worry about using fileconn.dir.music is safe to use, it's one of the system properties defined on the Personal Digital Assistant Profile. @See http://www.blackberry.com/developers/docs/4.5.0api/java/lang/System.html

Please use plain text.