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
Developer
uttamtts
Posts: 144
Registered: ‎03-15-2011
My Carrier: Airtel

Re: Playing music fromBackground Application

[ Edited ]

@peter_strange as you told i made a class which have only one instance in that class's constructor i have initialized my player. But it not working i am having the same problem as before. One thing more can i manage to push the screen manually as if one screen has been pushed i dont want to push the second screen from background process rather i want to push it after the first screen has been closed can i do it??

 

 Player only play a file once and when it ends it is not playing it again. :smileysad:

 

player.setLoopCount(-1); 

  Here is the code for the Singleton and in this case

 

public class SingleTonPlayer {
	
	private static SingleTonPlayer _instance ;
	private static final long GUID = 0x49c25abe58d904d9L;
	
	private Player player;
	
	SingleTonPlayer (String musicToPlay) {
		try{
		FileConnection fileConnection = (FileConnection) Connector.open(musicToPlay);
		InputStream inputStream = fileConnection.openInputStream();
		player = Manager.createPlayer(inputStream, "audio/m4a");
		player.realize();
		player.prefetch();
		player.setLoopCount(-1);
		player.start();
		if(fileConnection != null) {
			fileConnection.close();
		}if(inputStream != null) {
			inputStream.close();
		}
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (MediaException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	}

      public static SingleTonPlayer getInstance (String getMusicFile) {
		if (_instance == null) {
			_instance = (SingleTonPlayer) RuntimeStore.getRuntimeStore().get(GUID);
		} if (_instance == null) {
			SingleTonPlayer singleTonPlayer = new SingleTonPlayer(getMusicFile);
			RuntimeStore.getRuntimeStore().put(GUID	, singleTonPlayer);
			_instance = singleTonPlayer;
		}
		return _instance;
	}

      public void stopMusic() {
		try {
			player.stop();
//			player.close();
		} catch (MediaException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

 

Please use plain text.
Developer
peter_strange
Posts: 19,510
Registered: ‎07-14-2008

Re: Playing music fromBackground Application

[ Edited ]

Sorry I only have time for a quick look.

 

The logic you have used is not exactly what I had in mind.

 

I had expected that if you wanted to start another file of music, you would stop the current one.

 

The quickest way to change your current code to do this would be to change getInstance to something like the following:

 

       public static SingleTonPlayer getInstance (String getMusicFile) {
  if (_instance != null) {

   _instance.stop();
   _instance = null;

  }

   SingleTonPlayer singleTonPlayer = new SingleTonPlayer(getMusicFile);
   RuntimeStore.getRuntimeStore().put(GUID , singleTonPlayer);
   _instance = singleTonPlayer;
  return _instance;
 }

 

This is not how I would code it, I would do this sort of processing in a startMusic(String getMusicFile) method. but this is not how you have coded your processing, so changing getInstance seems the simplest way to explain the process. 

 

Edit:

Code had become formatted incorrectly, now corrected.

Please use plain text.
Developer
uttamtts
Posts: 144
Registered: ‎03-15-2011
My Carrier: Airtel

Re: Playing music fromBackground Application

[ Edited ]

As you suggested @peter_strange i made some changes in my code now its running two times if there is two screen but now the problem is i dont have control on pushing the screen on display stack. can you tell me how to control pushing the screen on display stack from ui.pushGlobalScreen( Screen to be pushed, priority , FLAG); method. I dont know Currently there is two screen which is simultaneously being pushed on the Display stack.

Please use plain text.
Developer
peter_strange
Posts: 19,510
Registered: ‎07-14-2008

Re: Playing music fromBackground Application

Can we try to keep on Thread == one problem.  So if we have solved the Subject of this Thread, that is running music from the background, can we mark this Thread as solved and start a new Thread with the new problem.

 

With respect to the new problem, it seems your problem is that you get two Global Screens pushed.  Since it is your code that is pushing these screens, I am not sure how we can help you stop this happening.  Do you need to keep some variable that indicates that a Global Screen has already been pushed?

Please use plain text.
Developer
uttamtts
Posts: 144
Registered: ‎03-15-2011
My Carrier: Airtel

Re: Playing music fromBackground Application

@peter_strange but it never playing the music file again after end of the file i have used 

 

player.setLoopCount(-1);

 Here is my modified code

 

see if it has some flaw....

public class SingleTonPlayer {
	
	private static SingleTonPlayer _instance ;
	private static final long GUID = 0x49c25abe58d904d9L;
	
	private static Player player;
	
	SingleTonPlayer () {
		
	
	}

    public static SingleTonPlayer getInstance (String getMusicFile) {
         if(_instance != null) {			
				_instance.stopMusic();
				_instance.playMusic(getMusicFile);
				_instance = null;			
		}
		_instance = (SingleTonPlayer) RuntimeStore.getRuntimeStore().get(GUID);
		SingleTonPlayer singleTonPlayer = new SingleTonPlayer();			
		RuntimeStore.getRuntimeStore().put(GUID	, singleTonPlayer);
		singleTonPlayer.playMusic(getMusicFile);
		_instance = singleTonPlayer;
		return _instance;
    }

    public void playMusic (String musicToPlay) {
		try {
			FileConnection fileConnection = (FileConnection) Connector.open(musicToPlay);
			InputStream inputStream = fileConnection.openInputStream();
			player = Manager.createPlayer(inputStream, "audio/m4a");
			player.realize();
			player.prefetch();
			player.setLoopCount(-1);
			player.start();
			if(fileConnection != null) {
				fileConnection.close();
			}if(inputStream != null) {
				inputStream.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MediaException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void stopMusic() {
		try {
			player.stop();
//			player.close();
		} catch (MediaException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

 

Please use plain text.
Developer
peter_strange
Posts: 19,510
Registered: ‎07-14-2008

Re: Playing music fromBackground Application

Can you please review your getInstance() and compare with the one I supplied previously.

 

I'm not sure what the problem is here, however the fact that you have closed the input stream that the player is reading I suspect does not help. 

 

In this situation, I would actually read the audio bytes into a byte buffer

[(IOUtilities.streamToBytes(..) should help here with this]

close the FileConnection, and just supply the player with a ByteArrayInputStream - this has no real resources held other than memory so you can leave this forever without worrying too much.

 

Try that, see if it helps.

Please use plain text.
Developer
uttamtts
Posts: 144
Registered: ‎03-15-2011
My Carrier: Airtel

Re: Playing music fromBackground Application

[ Edited ]

hey as your suggestion was based upon the constructor because i was previously intializing the player in the constructor of the SingleTonPlayer class. Now i have changed that as you suggested i made a playMusic(String getFile) which is used to play the music file.
as i reviewed the code i have seeing only one change that you are not using this line  (_instance = (SingleTonPlayer) RuntimeStore.getRuntimeStore().get(GUID):smileywink: of code is this line causing any problem ??? And i was playing the file in the condition where _instance!= null; now removing it from my original code and running it to see what happens.


_instance = (SingleTonPlayer) RuntimeStore.getRuntimeStore().get(GUID);

Because in your code you didnt use the above line. Should i avoid this line in my code. And thanks For suggesting to change the audio bytes into a byte buffer.
i made the changes like this .....
public static SingleTonPlayer getInstance (String getMusicFile) {
if(_instance != null) {
_instance.stopMusic();
//_instance.playMusic(getMusicFile);
_instance = null;
}
//_instance = (SingleTonPlayer) RuntimeStore.getRuntimeStore().get(GUID);
SingleTonPlayer singleTonPlayer = new SingleTonPlayer();
RuntimeStore.getRuntimeStore().put(GUID , singleTonPlayer);
singleTonPlayer.playMusic(getMusicFile);
_instance = singleTonPlayer;
return _instance;
}

 

same problem i am getting.

Please use plain text.
Developer
peter_strange
Posts: 19,510
Registered: ‎07-14-2008

Re: Playing music fromBackground Application

Sorry I don't understand your getInstance() code:

 

I didn't use

_instance = (SingleTonPlayer) RuntimeStore.getRuntimeStore().get(GUID);

because I didn't think I needed to.  _instance can only be non null, when it is set to the same Object as is in RuntimeStore.  If you wanted to change the code that I provided, so that it did use this line, then the updated code would look like:

 

public static SingleTonPlayer getInstance (String getMusicFile) {
  _instance = (SingleTonPlayer) RuntimeStore.getRuntimeStore().get(GUID);
  if (_instance != null) {

   _instance.stop();
   _instance = null;

  }

  SingleTonPlayer singleTonPlayer = new SingleTonPlayer(getMusicFile);
  RuntimeStore.getRuntimeStore().put(GUID , singleTonPlayer);
  _instance = singleTonPlayer;
  return _instance;
}

 

If you compare that with your code you have this line:

instance.playMusic(getMusicFile);

within the the if statement.  To me, that line looks completely wrong. 

You want the new Player to pay the music:

singleTonPlayer.playMusic(getMusicFile);

not the old one.


I hope you understand why this line looks wrong to me and can fix up your playMusic() method correctly if it is wrong.

 

Let us know how you get on with the byte array input rather than the file. 

Please use plain text.
Developer
uttamtts
Posts: 144
Registered: ‎03-15-2011
My Carrier: Airtel

Re: Playing music fromBackground Application

yeah you were right i was closing the inputStream which was causing me the problem. And i am currently looking for implementing your suggestion. Didnt get succes though, but i am trying .

Please use plain text.
Developer
uttamtts
Posts: 144
Registered: ‎03-15-2011
My Carrier: Airtel

Re: Playing music fromBackground Application

[ Edited ]

Hi @peter_strange i have made changes as you told and i am now only closing the file connection only.

 

public static SingleTonPlayer getInstance (String getMusicFile) {

if(_instance != null) {            
                _instance.stopMusic();
                _instance = null;            
        }

 

SingleTonPlayer singleTonPlayer = new SingleTonPlayer();            
RuntimeStore.getRuntimeStore().put(GUID    , singleTonPlayer);

_instance = singleTonPlayer;

return _instance;

}

 

Now my playMusic(String musictoPlay) methos is looking like this is this what you were telling me to do if there is some flaw please tell me how to fix that . Many a thanks to you :smileyhappy:

 

public void playMusic (String musicToPlay) {
        try {
            FileConnection fileConnection = (FileConnection) Connector.open(musicToPlay);
            InputStream inputStream = fileConnection.openInputStream();
            byte [] audioData = IOUtilities.streamToBytes(inputStream);
            ByteArrayInputStream bais = new ByteArrayInputStream(audioData);
            player = Manager.createPlayer(bais, "audio/m4a");
            player.realize();
            player.prefetch();
            player.setLoopCount(-1);
            player.start();
            if(fileConnection != null) {
                fileConnection.close();
            }/*if(inputStream != null) {
                inputStream.close();       // HERE NOT CLOSING InputStream SHUD I CLOSE IT ??               
            }*/
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Getting exception from "+e.getMessage());
            e.printStackTrace();
        } catch (MediaException e) {
            // TODO Auto-generated catch block
            System.out.println("Getting exception from Media exception "+e.getMessage());
            e.printStackTrace();
        }
        
    }

 

and its working now i think i might accept it as solved but if i will get any problem. I will reopen it.  Thanks @peter_starnge you were really helped me a lot. Many a thanks to you sir and kudos to you .:smileyhappy: :-)

Please use plain text.