Media Player stops abruptly with a warning in logcat: TimedEventQueue(33): Event 4 was not found in the queue, already cancelled?

纵然是瞬间 提交于 2020-01-03 03:42:26

问题


I am trying to put background music in my app.I have created an intent service which creates a Media Player and starts the music.

Once my app is launched the music is played only for a second and after that I see the following warning in my logcat:-

09-13 20:12:54.082: WARN/TimedEventQueue(33): Event 4 was not found in the queue, already cancelled?

For every run of my App, the Event number changes.This time it was Event 5.

Here is my service class which implements media player:-

import android.app.IntentService;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnErrorListener;
import android.widget.Toast;

public class MusicService extends IntentService {

    MediaPlayer mPlayer;
    private OnErrorListener mErrorListener;

    public MusicService() {
        super("MusicService");
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub
          // Normally we would do some work here, like download a file.


    }   

    ///////////////////////////////////////////////////////////

    @Override
    public int onStartCommand (Intent intent, int flags, int startId)

    {
        Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
        mPlayer.setLooping(true);
        mPlayer.start();

        return super.onStartCommand(intent,flags,startId);


    }

    @Override

    public void onCreate ()

    {
        super.onCreate();
      //  try{
            mPlayer = MediaPlayer.create(this, R.raw.jingle);
        //}catch (IllegalArgumentException e) {
            //e.printStackTrace();
        //}catch (IllegalStateException e ) {
            //e.printStackTrace();
        //}

        if(mPlayer!= null)
        {
            mPlayer.setLooping(true); // Set looping
            mPlayer.setVolume(100,100);
        }


    mPlayer.setOnErrorListener(new OnErrorListener() {

        public boolean onError(MediaPlayer mp, int what, int extra) {
            // TODO Auto-generated method stub
            onPlayError();
            return true;
        }

    });


    }

    private void onPlayError() {
        Toast.makeText(this, "music player failed", Toast.LENGTH_SHORT).show(); 
        if(mPlayer != null)
        {
            try{
                mPlayer.stop();
                mPlayer.release();
            }finally {
                mPlayer = null;
            }
        }
    }

    @Override
    public void onDestroy ()

    {
        super.onDestroy();
        if(mPlayer != null)
        {
            try{
                mPlayer.stop();
                mPlayer.release();
            }finally {
                mPlayer = null;
            }
        }

    }



}

回答1:


I got the solution.The problem was that since I am using an Intent service, so after starting the service with an intent, the player started but immediately on Destroy was called where there is a code to release and stop the player.

After removing that code I could see the music playing continuously and moreover I did not see those timed queue warnings!!!



来源:https://stackoverflow.com/questions/7407979/media-player-stops-abruptly-with-a-warning-in-logcat-timedeventqueue33-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!