简体   繁体   中英

MediaPlayer in Async Task : finalized without being released

I wrote a async task for playing BGM of a slideshow with fade in & out upon start & pause. I start the task and the the BGM when the slideshow activity start to play slideshow and cancel the task only when the slideshow activity is paused.

The BGM is played for a while upon start and stopped suddenly and looped back to the beginning of the track. The task haven't even been cancelled. I saw this error: mediaplayer finalized without being released. Any idea to fix this?

Also, I want to fade out and fade in the track on transition from end to beginning of the track. Is there any way to listen for certain point of the track, say 5 seconds before the end of the track so that I can fade out the track at the end.

Thanks in advance!

my code of the async class and the logcat are as follows:

import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import android.os.AsyncTask;

import android.media.MediaPlayer;
//import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.util.Log;


public class MediaPlayerController extends AsyncTask<String, String, String> {

//*******************

private final static int VOLUME_MIN = 0;

private static double VOLUME_MAX = 100;

private final static float FLOAT_VOLUME_MAX = 1.0f;
private final static float FLOAT_VOLUME_MIN = 0.0f;

private static double iVol;

private static double volChange;

private static String path;

private static MediaPlayer mMediaPlayer;

//*******************

public MediaPlayerController(int vol, String p) {
    super();
    iVol=vol;
    path=p;

    mMediaPlayer=null;

    VOLUME_MAX= iVol;

    volChange=iVol/5;

    Log.d("media player controller Async instance", "iVol "+ iVol + " vol change " + volChange);
}

 @Override
    protected void onPreExecute() {
        super.onPreExecute();
        startSound(path);

    }

    @Override
    protected String doInBackground(String... params) {

       return null;
    }

    @Override
    protected void onProgressUpdate(String... progress) {

    }
    @Override
    protected void onCancelled (){

      stopSound();
    }

    protected void onPostExecute(String file_url) {


    }

//-----------------------------------------------------------




public void startSound(String path){

    mMediaPlayer = new MediaPlayer();

    try {
        File soundFile = new File(path);
        if(soundFile.exists()){

            Log.d("start sound sound file", "exist");

            mMediaPlayer.setDataSource(path);
            mMediaPlayer.prepareAsync();

            //int dur= mMediaPlayer.getDuration();

            //Log.d("start sound mp duration", Integer.toString(dur));

            mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
                public void onPrepared(MediaPlayer mp) {

                    if (!mMediaPlayer.isPlaying())
                    startFadeInVol(1);  
                }
            }); 

        }else Log.d("start sound sound file", "NOT exist");
    }catch (IllegalArgumentException e1) {
        e1.printStackTrace();
    } catch (IllegalStateException e1) {
        e1.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}


public void stopSound() {
    if ((mMediaPlayer != null) && (mMediaPlayer.isPlaying())){

            if (mMediaPlayer.isPlaying()){
            stopFadeOutVol(1);
            Log.d("stop sound", "stopping");}   
    }       
}


private void updateVolume(double change)
{
    if((mMediaPlayer!=null) && (mMediaPlayer.isPlaying())){
    try{

    iVol =  iVol + change;

    Log.d("update vol", "iVol = "+iVol + " change " + change);

    if (iVol < VOLUME_MIN)
        iVol = VOLUME_MIN;
    else if (iVol > VOLUME_MAX)
        iVol = VOLUME_MAX;


    float fVol = 1 - ((float) Math.log(VOLUME_MAX - iVol) / (float) Math.log(VOLUME_MAX));


    if (fVol < VOLUME_MIN)
        fVol = FLOAT_VOLUME_MIN;
    else if (fVol > FLOAT_VOLUME_MAX)
        fVol = FLOAT_VOLUME_MAX;   

    Log.d("update vol fVol", Float.toString(fVol));

    mMediaPlayer.setVolume(fVol, fVol);

    }catch (Exception e){

        e.printStackTrace();
    }
}
}



public void startFadeInVol(int fadeDuration)
{
    try{

        mMediaPlayer.start();
        mMediaPlayer.setLooping(true);
        Log.d("start fade in vol", "starting");


        iVol = VOLUME_MIN;


    updateVolume(0);


    if(fadeDuration > 0)
    {
        final Timer timer = new Timer(true);
        TimerTask timerTask = new TimerTask() 
        {
            @Override
            public void run() 
            {
                updateVolume(volChange);
                if (iVol >= VOLUME_MAX)
                {
                    timer.cancel();
                    timer.purge();
                }
            }
        };

        int delay = fadeDuration*1000;
        if (delay == 0) delay = 1000;

        timer.schedule(timerTask, 0, delay);
    }
    } catch (Exception e) {
          e.printStackTrace();
      }
}

  private void stopFadeOutVol(int fadeDuration)
  {
      try {
              iVol = VOLUME_MAX;

          updateVolume(0);

          if (fadeDuration > 0)
          {
              final Timer timer = new Timer(true);
              TimerTask timerTask = new TimerTask()
              {
                  @Override
                  public void run()
                  {
                      updateVolume(-volChange);

                      if ((mMediaPlayer!=null) && (iVol <= VOLUME_MIN))
                      {

                          timer.cancel();
                          timer.purge();

                          mMediaPlayer.stop();
                          mMediaPlayer.reset();
                          mMediaPlayer.release();
                          //mMediaPlayer=null;

                          Log.d("stop fade out vol","mp stop");
                      }
                  }
              };

              int delay = fadeDuration*1000;
              if (delay == 0)
                  delay = 1000;

              timer.schedule(timerTask, 0, delay);
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
  }

}

09-16 10:25:16.645: E/ExtMediaPlayer-JNI(10150): QCMediaPlayer could not be located.... 09-16 10:25:16.645: E/MediaPlayer-JNI(10150): QCMediaPlayer mediaplayer NOT present 09-16 10:25:16.665: V/MediaPlayerNative: notify(8, 0, 0) callback on disconnected mediaplayer 09-16 10:25:16.725: E/MediaPlayerNative: [MediaPlayer::getMediaSystemInfo] key = 4000 09-16 10:25:16.735: D/MediaPlayerNative: MediaPlayer::invoke::getparam 09-16 10:25:16.735: D/MediaPlayerNative: MediaPlayer::getParameter mCurrentState = 0x00000008 09-16 10:25:16.735: E/MediaPlayerNative: [MediaPlayer::getMediaSystemInfo] key = 4000 09-16 10:25:16.735: D/MediaPlayerNative: MediaPlayer::invoke::getparam 09-16 10:25:16.735: D/MediaPlayerNative: MediaPlayer::getParameter mCurrentState = 0x00000008 09-16 10:25:16.735: E/MediaPlayerNative: [MediaPlayer::getMediaSystemInfo] key = 4000 09-16 10:25:16.735: D/MediaPlayerNative: MediaPlayer::invoke::getparam 09-16 10:25:16.735: D/MediaPlayerNative: MediaPlayer::getParameter mCurrent State = 0x00000008 09-16 10:25:16.755: V/MediaPlayerNative: MediaPlayer::setLooping 09-16 10:25:16.755: V/MediaPlayerNative: MediaPlayer::setVolume(0.000000, 0.000000) 09-16 10:25:16.755: E/MediaPlayerNative: [MediaPlayer::getMediaSystemInfo] key = 4000 09-16 10:25:16.755: D/MediaPlayerNative: MediaPlayer::invoke::getparam 09-16 10:25:16.755: D/MediaPlayerNative: MediaPlayer::getParameter mCurrentState = 0x00000010 09-16 10:25:16.755: V/MediaPlayerNative: MediaPlayer::setVolume(0.082400, 0.082400) 09-16 10:25:17.755: V/MediaPlayerNative: MediaPlayer::setVolume(0.188632, 0.188632) 09-16 10:25:18.755: V/MediaPlayerNative: MediaPlayer::setVolume(0.338358, 0.338358) 09-16 10:25:19.755: V/MediaPlayerNative: MediaPlayer::setVolume(0.594316, 0.594316) 09-16 10:25:20.755: V/MediaPlayerNative: MediaPlayer::setVolume(1.000000, 1.000000) 09-16 10:26:06.955: E/ExtMediaPlayer-JNI(10150): QCMediaPlayer could not be located.... 09-16 10:26:06.955: E/MediaPlayer-JNI(10150): QCMediaPlayer mediaplayer N OT present 09-16 10:26:06.975: V/MediaPlayerNative: notify(8, 0, 0) callback on disconnected mediaplayer 09-16 10:26:07.025: E/MediaPlayerNative: [MediaPlayer::getMediaSystemInfo] key = 4000 09-16 10:26:07.025: D/MediaPlayerNative: MediaPlayer::invoke::getparam 09-16 10:26:07.025: D/MediaPlayerNative: MediaPlayer::getParameter mCurrentState = 0x00000004 09-16 10:26:07.035: E/MediaPlayerNative: [MediaPlayer::getMediaSystemInfo] key = 4000 09-16 10:26:07.035: D/MediaPlayerNative: MediaPlayer::invoke::getparam 09-16 10:26:07.035: D/MediaPlayerNative: MediaPlayer::getParameter mCurrentState = 0x00000004 09-16 10:26:07.045: E/MediaPlayerNative: [MediaPlayer::getMediaSystemInfo] key = 4000 09-16 10:26:07.045: D/MediaPlayerNative: MediaPlayer::invoke::getparam 09-16 10:26:07.045: D/MediaPlayerNative: MediaPlayer::getParameter mCurrentState = 0x00000008 09-16 10:26:07.045: V/MediaPlayerNative: MediaPlayer::setLooping 09-16 10:26:07.045: V/MediaPlayerNative: MediaPlayer::setVolume(0.000000, 0.0 00000) 09-16 10:26:07.055: V/MediaPlayerNative: MediaPlayer::setVolume(0.082400, 0.082400) 09-16 10:26:07.055: E/MediaPlayerNative: [MediaPlayer::getMediaSystemInfo] key = 4000 09-16 10:26:07.055: D/MediaPlayerNative: MediaPlayer::invoke::getparam 09-16 10:26:07.055: D/MediaPlayerNative: MediaPlayer::getParameter mCurrentState = 0x00000010 09-16 10:26:08.055: V/MediaPlayerNative: MediaPlayer::setVolume(0.188632, 0.188632) 09-16 10:26:08.295: W/MediaPlayer-JNI(10150): MediaPlayer finalized without being released 09-16 10:26:09.055: V/MediaPlayerNative: MediaPlayer::setVolume(0.338358, 0.338358) 09-16 10:26:10.055: V/MediaPlayerNative: MediaPlayer::setVolume(0.594316, 0.594316) 09-16 10:26:11.055: V/MediaPlayerNative: MediaPlayer::setVolume(1.000000, 1.000000)

Problem fixed. Thanks to the poor code by other developers. Thank you everyone here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM