media player issue while continuously click on button? - android

i am playing mp3 sound using default media player of android, my code for plying sound below
MediaPlayer mp=MediaPlayer.create(hello.this,R.raw.abc);
if(mp!=null)
{
mp.start();
}
i need to play same sound on same button click. when i click continuously on button after some time sound is not working and i getting error MediaPlayer(7395): error (-19, 0).
Does any body have idea for this issue,
Please send me
Thank in advance.

call
mp.reset();
because ever time you are decalaring object
or declare on class level Mediaplayer OBject

Make global variable of mediaplayer and try this way
MediaPlayer mp;
if (mp!=null) {
mp.stop();
mp.release();
}
mp= MediaPlayer.create(hello.this,R.raw.abc);
mp.start();

mp = new MediaPlayer();
mp.create(this, R.raw.testmed);
mp.setVolume(100, 100);
mp.setOnPreparedListener(this);
mp.prepare();
Then you will need to define this and it should work:
public void onPrepared(MediaPlayer player) {
mp.start();
}

you can play music like below
// for play the song
MediaPlayer mp = new MediaPlayer();
try
{
mp.reset();
mp.setDataSource(songPath);
mp.prepare();
mp.start();
btnPlay.setBackgroundResource(R.drawable.img_btn_pause);
} catch (IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}

I have found solution for this issue. i have correct this issue using below code, May help other also.
Thanks
Call Method like
PlaySound(R.raw.abc);
//Method
private void PlaySound(int Sound_id)
{
mplayer = MediaPlayer.create(Act_Oceanwaves.this,Sound_id);
if(mplayer!=null)
{
mplayer.start();
}
mplayer.setOnCompletionListener(new OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
mp.release();
}
});
}

Related

When using MediaPlayer, sometimes it does not sound

for recording file output, I use MediaPlayer.
usually, When I button clicked, start media file.
but, sometime not sound play.
/MediaPlayer: mediaplayer went away with unhandled events
W/MediaPlayer-JNI: MediaPlayer finalized without being released
at first I think before the voice file is finished, when clicked button occur this problem.
but even if I press the button repeatedly, sometime not sound play.
source.
player = new MediaPlayer();
player.reset();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
player.reset();
AssetFileDescriptor assetFileDescriptor =
getApplicationContext().getResource().openRawResourceFd(R.raw.sound);
player.setDataSource(assetFileDescriptor.getFileDescriptor(),
assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength());
player.prepare();
player.start();
} catch (Exception e) {
e.printStackTrace();
}
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
if (player != null) {
player.stop();
player.release();
}
}
});
How to fix sometimes not sound play when i button clicked?
thanks.
There's no reason to call MediaPlayer.release() if it will be used again, as calling reset() will release all the memory & codecs that are in use.
init:
player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.reset();
}
});
Handling button clicks:
void onButtonClicked() {
player.reset();
AssetFileDescriptor assetFileDescriptor =
getApplicationContext().getResource().openRawResourceFd(R.raw.sound);
player.setDataSource(assetFileDescriptor.getFileDescriptor(),
assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength());
player.prepare();
player.start();
}

AudioManager volume starts high then goes low

I have the following code to play the default ringtone when a call is incoming.
Here is the code:
public AudioPlayer(Context c){
AudioManager am = (AudioManager)c.getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_RINGTONE);
mp = new MediaPlayer();
}
public void playRingtone(){
try{
mp.setDataSource(mContext.getApplicationContext(), Settings.System.DEFAULT_RINGTONE_URI);
mp.prepare();
mp.start();
}
catch(Exception e){
System.out.println("AudioManager: " + e.getLocalizedMessage());
}
}
public void stopRingtone(){
if(mp.isPlaying()){
mp.stop();
mp.reset();
mp.release();
}
}
This code starts playing the ringtone at the proper volume level but then cuts out and plays really really low. What is the reason for the change in volume and how can I fix it?
Thank you for your help

Android Mediaplayer Won't Stop

I have the following code to play an mp3 file from the web and this is working but when I use the stop functionality the audio does not stop. Can anyone point me towards a resource to find out more about this or tell me where I am going wrong? Thanks.
showAudio.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
if (!showAudio.getText().equals("Stop")) {
try {
String url = lblAudio.getText().toString();
if (url.length() > 2) {
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
mediaPlayer.start();
showAudio.setText("Stop");
}
} catch (Exception e) {
Toast.makeText(getBaseContext(), "Sorry, there was a problem playing audio.", Toast.LENGTH_SHORT).show();
}
} else {
try {
mediaPlayer.stop();
mediaPlayer.release();
} catch (Exception ex) {
ex.printStackTrace();
}
showAudio.setText("Audio");
}
}
});
You are creating a new MediaPlayer every time you click the button. Create the player outside of the click handler.
Try out :
try {
if(mediaPlayer.isPlaying())
{
mediaPlayer.stop();
mediaPlayer.release();
}
} catch(Exception ex) {
ex.printStackTrace();
}
Try making your media player a global variable and make it static....
I have a working media player that starts and stops
Uri ringtone;
MediaPlayer mp;
ringtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
mp = MediaPlayer.create(getApplicationContext(), ringtone);
//code to start the mediaplayer
if (Flags.notificationReceived) {
showAlert(Flags.patientModel);
Flags.notificationReceived = false;
mp.start();
mp.setLooping(true);
vibrate(2000);
}
//code to stop the media player
if (mp.isPlaying()) {
mp.stop();
mp.reset();
mp.release();
mp = MediaPlayer.create(getApplicationContext(), ringtone);
}

MediaPlayer sound not always playing, not getting granted audio focus

I'm having an issue with some android code to play a sound notification on certain events.
Here's the code:
int result = audioManager.requestAudioFocus(MainRunningService.afChangeListener,AudioManager.STREAM_NOTIFICATION,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mp = new MediaPlayer();
mp.setDataSource(context, soundToPlay);
mp.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
mp.prepare();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
try {
Log.d(LOGTAG, "SoundService MUSIC: MUSIC IS NOW COMPLETE");
mp.release();
mp = null;
Log.d(LOGTAG, "SoundService MUSIC: MUSIC IS NOW COMPLETE - RELEASED");
} catch (Exception e) {
e.printStackTrace();
}
}
});
mp.setLooping(false);
mp.start();
}
else{
Log.w(LOGTAG, "SoundService Audiofocus was not granted");
}
What I'm finding is that sometimes it will play, whereas other times it won't play. When it doesn't play it's hitting the "SoundSerice Audiofocus was not granted" log line. I've looked at the system logs, but can't find anything that says why it's not been granted.
Once this has happened, then every request seems to fail.
Does anyone have any pointers to what I might be doing wrong?
//call the method playAssistClip() on button click or wherever you want.
private MediaPlayer mp;
private void playAssistClip() {
if (workoutReps.equalsIgnoreCase("6x6x6")) {
mp = MediaPlayer.create(ProcessWorkout.this,
R.raw.six_six_six_formated_ogg);
mp.setOnCompletionListener(new OnCompletionListener() {
//do something after the audi ends
mp.reset();
mp.release();
finish();
}
});
mp.start();
//to get the duration of clip
spentTimeOnWorkout = mp.getDuration();
}

error -19,0 from using Mediaplayer?

E/MediaPlayer(20473): error (-19, 0)
I/MyApp (20473): Decoding lala.mp3
I/StagefrightPlayer( 68): setDataSource('mypath')
E/AudioFlinger( 68): no more track names available
E/AudioTrack( 68): AudioFlinger could not create track, status: -12
E/AudioSink( 68): Unable to create audio track
Does any1 know why i'm getting this? This usually happens after playing about 100+ of so audio files using mediaPLayer. I'm playing it like this
public RenderResultFormat DoIt() {
if(mp!=null){
mp.release();
mp = null;
}
AudioRenderer mr = new AudioRenderer(); mp = mr.AudioRenderer(filePath);}
private class AudioRenderer extends Activity {
private MediaPlayer AudioRenderer(String filePath) {
//delcare mediaplayer variables, path etc
mp= MediaPlayer.create(this, path);
if(mp != null){
int duration = mp.getDuration();
mp.start();
try {
Thread.sleep(duration);
} catch (InterruptedException e2) {
e2.printStackTrace();
System.out.println("I've been interrupted >:(");
}
}
}return mp;}
Am i missing something? Quite new to android development. Thank you
This solution works well for me (playing a ressource). Just implement an OnCompletionListener
private void playbeep(int id) {
MediaPlayer mPlayer;
mPlayer = MediaPlayer.create(context, id);
mPlayer.start();
mPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}

Categories

Resources