android code acting differently on device than emulator - android

hi i developed a simple app that should play a sound if a boolean is true and then another sound when it completes or just play the second sound if the boolean is false and it all works perfectly on the avd but when run on device it doesint let the first sound complete before jumping to the second
this is in the onCreate
setContentView(screen);//pl
if(msg==true) {//boolean set from other activity
playInfo();
}else if(msg==false){
playSound();
}
}
this is the play info method which starts to play but after a second it jumps to playsound method on the phone but works perfectly on avd
private void playInfo(){
mp = MediaPlayer.create(this, R.raw.msg);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
msg = false;
playSound();
}
});
}
this is the playsound method
private void playSound() {
mp = MediaPlayer.create(this, song);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
playSound();//loop the sound
}
});
}
again this works perfect on emulator but not on a device why would they act differently and any ideas on how it would be resolved?

The activity was set to landscape mode on the device this caused the activity to be redrawn very quickly after launching so it started got destroyed and started again with the boolean set to false however on the emulator this does not happen it just draws the activity once in landscape mode solved the problem by using 2 different mediaplayers and just play the first short sound over the other rather than rework the code.

Related

Android MediaPlayer.start does not start

I have yet to find an answer to this.
I have a local file (R.raw.Bob); and I am trying to use MediaPlayer to play the file.
Sometimes it plays, sometimes it does not. I have another file which plays seemingly fine every time.
My activity flow is like this: In onCreate I do the following:
MediaPlayer mBackground = MediaPlayer.create(MainAct.this, R.raw.background);
mBackground.start(); // Works as expected.
Now in a different part of the activity I have the following:
MediaPlayer mBob= MediaPlayer.create(MainActivity.this, R.raw.Bob);
mBob.start();
And nothing occurs. I have used Log.i() and the execution goes through the relevant code but the file does not start.
Why does MediaPlayer sometimes work and sometimes does not, and is there a more reliable way of playing sound files?
Try this to start:
MediaPlayer mBob = MediaPlayer.create(MainActivity.this, R.raw.Bob);
mBob.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(final MediaPlayer mp) {
mp.start();
}
});
and this to stop:
mBob.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
}
});

Unable to play two MediaPlayer at same time in Nexus 5

I am trying to play some audio in background using two MediaPlayers inside a Service. Everything works fine if I want to play only one mp3, but If I try to play two at the same time, using two instances of MediaPlayer, only the last one works. It is weird because it works in emulator and some devices, but I am unable to make it work in a Nexus 5. This is the code I am using to create the two MediaPlayer instances:
private void playWithPiano() {
mp1 = initializePlayer(filenameVoz); // filenameVoz is the mp3 file name in raw folder
mp2 = initializePlayer("base_piano");
mp1.start();
mp2.start();
}
private MediaPlayer initializePlayer(String filename) {
int identifier = getResources().getIdentifier(filename,
"raw", getPackageName());
MediaPlayer mp = MediaPlayer.create(this, identifier);
mp.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.d(TAG_LOG, "OnErrorListener");
stop();
return false;
}
});
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
stop();
}
});
return mp;
}
If I comment the mp2.start line, then I can listen the mp1 audio. It is like when a MediaPlayer is started, any other instances are stopped I would appreciate any help with this.
I found other posts describing the same problem in Nexus 5 devices, but don't have a solution yet.
https://code.google.com/p/android/issues/detail?id=63099
https://code.google.com/p/android/issues/detail?id=62304
I tried using seekTo(0) like it was suggested in one of those forums, but it didn't work, so I tried another thing that seems like an specific workaround for my case.
I am using this inside a Service, and my second MediaPlayer is only to play some background music, so I started it half second after the first one and it worked.
new java.util.Timer().schedule(
new java.util.TimerTask() {
#Override
public void run() {
if (mp2 != null) mp2.start();
}
},
500
);
I still don't know why I am having this problem only in the Nexus 5, but I hope this may help somebody.

android mediaplayer loops forever on ICS

I want to play a notification sound and my problem it that the sound loops forever, when it should sound only once.
I've tried two ways:
notification.sound = Uri.parse("content://media/internal/audio/media/38");
and
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
mMediaPlayer.setDataSource(this, Uri.parse("content://media/internal/audio/media/38"));
mMediaPlayer.setLooping(false);
mMediaPlayer.prepare();
mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
Log.v(Utils.TAG, "onprepared");
mp.start();
}
});
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
Log.v(Utils.TAG, "end, we should release");
mp.stop();
mp.release();
}
});
In the second case, I never see the trace "end, we should release", the audio is played over and over and over again.
Any idea?
Thank you very much
UPDATE:
I've tried two devices and:
It loops forever on a Galaxy Nexus with ICS 4.0.4
It works fine on a HTC Hero 2.2.1
There are sounds with "built-in" loops which will play for ever. This is different to setting the MediaPlayer to looping. If you start playing a sound with a built-in loop, it will never finish. These are mostly ring tones (as opposed to alarm tones or notification tones). You can set the RingtoneManager to return only notification tones or alarm tones with myringtonemanager.setType(RingtoneManager.TYPE_ALARM|RingtoneManager.TYPE_NOTIFICATION)
which will exclude most looping sounds, but unfortunaley this is not guaranteed to exclude them all on any device. :-(
Therefore the solution of Tiago Almeida is a good work-around (and i've voted it up) though it will also truncate all sounds which have just a few (and no infinite) loops.
Try this:
try {
mp.setDataSource(uri.toString());
mp.setAudioStreamType(AudioManager.STREAM_ALARM);
mp.prepare();
mp.start();
mp.setOnSeekCompleteListener(new OnSeekCompleteListener() {
public void onSeekComplete(MediaPlayer mp) {
mp.stop();
mp.release();
}
});
} catch (Exception e) {
e.printStackTrace();
}
One more workaround for this problem
boolean soundPlayedOnce = true;
if(soundPlayedOnce){
mp.start();
soundPlayedOnce = false;
}
set this boolean to true right before you want to play sound

Mediaplayer error (-19,0) after repeated plays

I have a game in which a sound plays when a level is completed. Everything works fine to start with but after repeating a level 10 or 20 times the logcat suddenly reports:
"MediaPlayer error (-19,0)" and/or "MediaPlayer start called in state 0" and the sounds are no longer made.
I originally had the all sounds in mp3 format but, after reading that ogg may be more reliable, I converted them all to ogg, but the errors appeared just the same.
Any idea how I can fix this problem?
I was getting the same problem, I solved it by adding the following code to release the player:
mp1 = MediaPlayer.create(sound.this, R.raw.pan1);
mp1.start();
mp1.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
};
});
I think you are not releasing the mediaplayers you are using to play the sound..
You need to release() the media players otherwise the resources are not released , and you soon get out of memory (since you allocate them again next time). so,I think you can play twice or even thrice... but not many times without releasing the resources
MediaPlayer is not a good option when you are playing small sound effects as the user can click on multiple buttons very soon and you will have to create a MP object for all of them which doesnt happen synchronously. That is why you are not hearing sounds for every click. Go for the SoundPool Class which allows you to keep smaller sounds loaded in memory and you can play them any time you want without any lag which you would feel in a mediaplayer. http://developer.android.com/reference/android/media/SoundPool.html Here is a nice tutorial : http://www.anddev.org/using_soundpool_instead_of_mediaplayer-t3115.html
I solved both the errors (-19,0) and (-38,0) , by creating a new object of MediaPlayer every time before playing and releasing it after that.
Before :
void play(int resourceID) {
if (getActivity() != null) {
//Using the same object - Problem persists
player = MediaPlayer.create(getActivity(), resourceID);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
player.release();
}
});
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
}
}
After:
void play(int resourceID) {
if (getActivity() != null) {
//Problem Solved
//Creating new MediaPlayer object every time and releasing it after completion
final MediaPlayer player = MediaPlayer.create(getActivity(), resourceID);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
player.release();
}
});
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
}
}
This is a very old question, But this came up first in my search results So other people with the same issue will probably come upon this page eventually.
Unlike what some others have said, you can in fact use MediaPlayer for small sounds without using a lot of memory. I'll put in a little modified snippit from my soundboard app to show you what I'm getting at.
private MediaPlayer mp;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
mp = new MediaPlayer();
}
private void playSound(int soundID){
mp.reset();
AssetFileDescriptor sound = getResources().openRawResourceFd(soundID);
try {
mp.setDataSource(sound.getFileDescriptor(),sound.getStartOffset(),sound.getLength());
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mp.start();
}
with the way I set it up, you create on MediaPlayer object that you reuse everytime you play a sound so that you don't use up too much space.
You call .reset() instead of .release() because .release() is only used if you are disposing of an object, however you want to keep your MediaPlayer Object.
You use an assetfiledescriptor to set a new soundfile for your mediaplayer to play instead of setting a new object to your mediaplayer address because that way you are creating new objects within the method that aren't being handled properly and you will eventually run into the same error as you described.
This is only one of many ways to use MediaPlayer but I personally think it is the most efficient if you are only using it for small sound applications. The only issue with it is that it is relatively restrictive in what you can accomplish, but that shouldn't be much of an issue if you are indeed using it for small sound applications.
i try delete emulator and new create emulator for remove error of (-19,0) media player.

Playing sound on EVERY buttonclick(Android=

I am currently working on an application for Android and I'm using Eclipse and of course Android SDK, but I have bumped in to a problem that is almost the only thing I need to fix before I can relaese a beta-version to Android Market.
So, my problem is that I have an xml with 4 different buttons, and if the user press on a certain button, one sound will be played, and if the user press any of the other buttons is pressed another sound will play but the sound only play sometimes, I want it to play every time the user press a button.
Here's my code(concerning the mediaplayer):
public MediaPlayer right=null;
public MediaPlayer wrong=null;
if(right!=null) {
right.reset();
right.release();
}
if(wrong!=null) {
wrong.reset();
wrong.release();
}
right = MediaPlayer.create(getBaseContext(), R.raw.rightsound);
wrong = MediaPlayer.create(getBaseContext(), R.raw.wrongsound);
if(****()){
right.start();
}
else {
wrong.start();
}
That's my code and I would be very grateful if somebody could help me solve my problem.
new Thread() {
public void run() {
int sound = R.raw.wrongsound;
if(****()) {
sound = R.raw.rightsound;
}
mp = MediaPlayer.create(Test.this, sound);
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
mp.start();
}
}.start();

Categories

Resources