04-23-2012 02:57 AM - edited 04-23-2012 08:00 AM
@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. ![]()
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();
}
}
}
04-23-2012 12:15 PM - edited 04-24-2012 06:31 AM
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.
04-24-2012 02:40 AM - edited 04-24-2012 02:44 AM
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.
04-24-2012 04:48 AM
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?
04-24-2012 05:42 AM
@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();
}
}
04-24-2012 06:37 AM
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.
04-24-2012 06:53 AM - edited 04-24-2012 07:58 AM
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)
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.
04-24-2012 07:59 AM
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.
04-24-2012 09:01 AM
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 .
04-25-2012 01:17 AM - edited 04-25-2012 02:48 AM
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 ![]()
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 .
:-)