Android mediaplayer causing "dead thread" message - android

I am starting a sound from a background service (IntentService), which is triggered by a system alarm (the thread of the service will most often be dead when the sound ends).
The relevant code is this:
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (alert == null)
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
MediaPlayer mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(this, alert);
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION) != 0) {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
mMediaPlayer.setLooping(false);
mMediaPlayer.prepareAsync();
mMediaPlayer.start();
}
This stuff works fine, but every time the sound is played, I get this in log cat:
WARN/MessageQueue(7508): Handler{482f97e0} sending message to a Handler on a dead thread
I think this could be due to a callback to the background thread when the sound is finished, or my repeated use of a media player before having finalized the previous one. Any ideas?

Very old question, but #Alex' xkcd link convinced to me answer it anyway.
I have a very similar situation, and was able to achieve the desired result by instantiating the MediaPlayer through a Runnable. In my case an IntentService calls an ongoing Service, which is responsible for the media playback. My solution looks as follows (relevant code only):
public class HelperService extends Service {
public void play() {
Thread thread = new Thread(new Runnable() {
public void run() {
soundStart();
}
});
thread.start();
}
private void soundStart() {
try {
AssetFileDescriptor afd = mContext.getResources().openRawResourceFd(R.raw.sound);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
mp.reset();
return false;
}
});
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// do stuff
}
});
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Related

Why MediaPlayer does not play the audio correctly?

I am working in a gallery of images that include an audio of how the things that the user is viewing on the screen are pronounced, which is executed with a button also included in the screen.
The following code works, but sometimes the audio is cut before it finishes reproducing, the problem with this is that I am not receiving any error log.
View.OnClickListener eventoPlay = (v) → {
#Override
public void onClick(View v) {
btnPlaySound.setClickable(false);
try{
Uri myUri = Uri.parse(urlroot + urlAudio); //global variables by current image
final MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
btnPlaySound.setClickable(true);
}
});
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
btnPlaySound.setClickable(true);
Log.e("Error in media player", String.valueOf(what));
Log.e("Error in media player", String.valueOf(extra));
return false;
}
});
mediaPlayer.prepareAsync();
}catch (Exception e){
e.printStackTrace();
btnPlaySound.setClickable(true);
}
}
};
btnPlaySound.setOnClickListener(eventoPlay);
btnPlaySound.performClick();
These are the scenarios that I have debugged:
-setOnPreparedListener() call naturally, always (the audio starts playing always).
-setOnCompletionListener() call only if the audio was finished playing (if the audio was not cut).
-setOnErrorListener() It never call, regardless of whether the audio is cut.
-catch This block is never executed either, regardless of whether the audio is cut.
I tested in devices like Samsung tab S3, Samsgung tab E, Samsung tab A, Alcatel A3, Xiaomi MiA2, Swissmobility.
In the most powerful devices happens less frequently. but I want this failure never to occur.
Tnks.
You may want to ensure btnPlaySound.setClickable(true) is being called on the UI thread. It could be causing an error. Also the media player may be getting garbage collected before finishing keep a (strong) reference to the MediaPlayer to prevent garbage collection.
Try this to test if it being garbage collected:
View.OnClickListener eventoPlay = (v) → {
#Override
public void onClick(View v) {
btnPlaySound.setClickable(false);
try{
Uri myUri = Uri.parse(urlroot + urlAudio); //global variables by current image
// KEEP A STRONG REFERENCE
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.setLooping(false);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
btnPlaySound.setClickable(true);
}
});
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
mp.release();
btnPlaySound.setClickable(true);
Log.e("Error in media player", String.valueOf(what));
Log.e("Error in media player", String.valueOf(extra));
return false;
}
});
mediaPlayer.prepareAsync();
}catch (Exception e){
e.printStackTrace();
btnPlaySound.setClickable(true);
}
}
};
btnPlaySound.setOnClickListener(eventoPlay);
btnPlaySound.performClick();

How to play/stop default sound automatically in Android?

I want to play and stop the default sound with following rules:
If the sound is not playing, let play it in 10 seconds.
If the sound is playing, let stop it and play at the first position.
Based on these above rules, I design a function as follows:
public MediaPlayer mp =null;
public void playDefaultSound(){
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
mp = MediaPlayer.create(getApplicationContext(), notification);
try {
if (mp.isPlaying()) {
mp.stop();
mp.release();
mp = MediaPlayer.create(getApplicationContext(), notification);
}
mp.start();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
mp.stop();
mp.release();
}
}, 10000);
} catch (Exception e) {
e.printStackTrace();
}
}
But sometime I still listen two sound are playing (in case of the first sound play and I call the playDefaultSound() function again). Do you think is it correct to delete the mp = MediaPlayer.create(getApplicationContext(), notification); bellow mp.release()? How could I correct the function to satisfy these rules? Thanks all
final MediaPlayer mp = new MediaPlayer();
public void playDefaultSound(){
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
try {
if (mp != null && mp.isPlaying()) {
mp.seekTo(0);
} else {
mp.reset();
mp.setDataSource(getApplicationNotification(), notification);
mp.start();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
mp.stop();
mp.release();
}
}, 10000);
} catch (Exception e) {
e.printStackTrace();
}
}
P.S. - Always see the state diagram or lifecycle of things whenever stuck.
Ref : [Android Media Player State Diagram][1]
[1]: https://developer.android.com/reference/android/media/MediaPlayer.html#StateDiagram "MediaPlayer State Diagram"

Android - How to remove MediaPlayer loop delays?

I set a music file to loop on MediaPlayer for my game, but it causes a 2 sec delay when it loops.
My code:
boolean activateSounds = getIntent().getBooleanExtra("Activate sounds", true);
if(mp!=null){
mp.reset();
mp.release();
}
mp = MediaPlayer.create(StartGame.this, R.raw.music1);
mp.setVolume(8f, 8f);
mp.setLooping(true); // This is causing delays
if (activateSounds) mp.start();
For a game, this is not interesting. Is there a way to make MediaPlayer run out of loop delays?
I was not able to make setLooping work without a gap.
Only solution that worked for me was to use setNextMediaPlayer call (which is able to start next loop without a gap) and two MediaPlayers.
'pseudo' code:
class Abc implements MediaPlayer.OnCompletionListener {
private final MediaPlayer[] mps = new MediaPlayer[2];
public Abc() {
mps[0] = new MediaPlayer();
mps[1] = new MediaPlayer();
mps[0].setOnCompletionListener(this);
mps[1].setOnCompletionListener(this);
}
public void start()
initMediaPlayer(mps[0]);
initMediaPlayer(mps[1]);
mps[0].setNextMediaPlayer(mps[1]);
mps[0].start();
}
private void initMediaPlayer(MediaPlayer mp)
{
if (mp.isPlaying()){
mp.stop();
}
mp.reset();
final float volume = 0.07f;
mp.setDataSource(MY_SOURCE);
mp.setVolume(volume, volume);
mp.setLooping(false);
try {
mp.prepare();
}catch(Exception error){
Log.d("BackgroundMusic", error.toString());
}
}
#Override
public void onCompletion(MediaPlayer mp)
{
MediaPlayer cur = mps[0] == mp ? mps[1] : mps[0];
initMediaPlayer(mp);
cur.setNextMediaPlayer(mp);
}
}

Can't stop Media Player in android

I tried almost everything found on the internet and I can't stop the media player once it starts. I'm using broadcast receiver and I'm controlling the media player using SMS. Here is my code.
public class Receiver extends BroadcastReceiver{
String body;
String address;
public static final String SMS_EXTRA_NAME="pdus";
MediaPlayer mp = new MediaPlayer();
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
SharedPreferences obj1=context.getSharedPreferences("mypref", Context.MODE_PRIVATE);
String newstring=obj1.getString("key1", null);
String name=newstring;
Bundle bund=intent.getExtras();
String space="";
if(bund!=null)
{
Object[] smsExtra=(Object[])bund.get(SMS_EXTRA_NAME);
for(int i=0;i<smsExtra.length;i++)
{
SmsMessage sms=SmsMessage.createFromPdu((byte[])smsExtra[i]);
body=sms.getMessageBody().toString();
address=sms.getOriginatingAddress();
if(body.equals("ON"))
{
if(mp.isPlaying())
{
mp.stop();
}
try {
mp.reset();
AssetFileDescriptor afd;
afd = context.getAssets().openFd("file.mp3");
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepare();
mp.start();
mp.setLooping(true);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else if(body.equals("OFF"))
{
if (mp.isPlaying()==true||mp!=null)
{
try{
mp.stop();
mp.release();
} catch(Exception e){
System.out.println("Exception"+e);
}
}
}
}
}
}
}
The media player is turning on when I send "ON", but it won't turn off. And yes I have given the required permissions in the Manifest file.
The BroadcastReciever it stays alive for around 9 seconds, you should not create big operation in it. However, you can let it start an operation like start acitivty or service and there you play a track or start download a file ...etc
If you want to only start a player and no need for user interaction, I suggest that you start a service and there you play your what you want.
I spent a lot of time studying this problem, and found out that:
The problem here is that I create a MediaPlayer inside a thread that is managed by the IntentService. And at the time of starting playback the thread is no longer valid.
So the way out is:
final Handler handler = new Handler(getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
mediaPlayer.start();
}
});
handler.postDelayed(new Runnable() {
#Override
public void run() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
}
, 30 * 1000);
It helped me stop the mediaplayer.

How to correctly change MediaPlayer audio stream type?

I have a simple mp service to play, pause, resume audio. All works fine.
But, last night I have decided to add a feature for user to route audio to ear-piece or speaker and have been battling with mp.setAudioStreamType().
Problem is that I can't change it while service connected and mp created. I don't want to terminate service and/or unbind and rebind as it would require a lot of refactoring
How do I supposed to change AudioStreamType while playing an audio?
Here is my code:
Player service:
public class PService extends Service {
private MediaPlayer mp = new MediaPlayer();
public static final String PLAYING_FINISHED_MSG = "1";
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onDestroy() {
mp.stop();
mp.release();
}
private void playSong(String file) {
try {
mp.reset();
mp.setDataSource(file);
mp.setAudioStreamType(MYAPP.getAudioStreamType());
mp.prepare();
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
Intent i = new Intent();
i.setAction(MDService.PLAYING_FINISHED_MSG);
sendBroadcast(i);
}
});
toggle route button onclick
currentlyPlayingFile = file;
currentlyPlayingPhone = phone;
lastDurationBeforePause = mpInterface.getCurrentPosition();
if(MYAPP.getAudioStreamType() == AudioManager.STREAM_MUSIC)
{
MYAPP.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
recording_player_route_button.setImageResource(R.drawable.route_off);
}
else{
MYAPP.setAudioStreamType(AudioManager.STREAM_MUSIC);
recording_player_route_button.setImageResource(R.drawable.route_on);
}
try {
mpInterface.playFile(file);
player_seekbar.setProgress(0);
player_seekbar.setMax(mpInterface.getDuration());
//seekto last millisecond after switching from/to sepaker
if(seekTo>0)
{
mpInterface.seekTo(seekTo);
}
isPauseButtonPressed = false;
handleSeekBarUpdate.postDelayed(handleSeekBarUpdateJob, 1);
} catch (RemoteException e) {
e.printStackTrace();
}
The MODIFY_AUDIO_SETTINGS permission is needed in the Manifest for this to work.
AudioManager am=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_NORMAL);
MediaPlayer mp=new MediaPlayer();
Uri ringtoneUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
try
{
mp.setDataSource(getApplicationContext(), ringtoneUri);
mp.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
mp.prepare();
mp.start();
}
catch(Exception e)
{
//exception caught in the end zone
}

Categories

Resources