开发者

Android mediaplayer causing "dead thread" message

开发者 https://www.devze.com 2023-03-23 02:57 出处:网络
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).

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 stuf开发者_如何学Cf 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();
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消