我写了一个异步任务,用于播放幻灯片放映的BGM。在开始时&暂停。当幻灯片活动开始播放幻灯片时我启动任务和BGM,并且仅在幻灯片活动暂停时取消任务。
BGM在开始时播放一段时间并突然停止并循环回到音轨的开头。任务天堂甚至被取消了。我看到了这个错误:媒体播放器最终确定没有被释放。有什么想法解决这个问题吗?
另外,我希望在从曲目的结尾到开始的过渡时淡出并淡入音轨。是否有任何方法可以听到轨道的某个点,比如在轨道结束前5秒,这样我就可以在最后淡出轨道。
提前致谢!
async类和logcat的代码如下:
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 .... 09-16 10:25:16.645:E / MediaPlayer-JNI(10150):QCMediaPlayer媒体播放器不存在 09-16 10:25:16.665:V / MediaPlayerNative:在断开连接的媒体播放器上通知(8,0,0)回调 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 mCurrentState = 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:36:06.955:E / ExtMediaPlayer-JNI(10150):无法找到QCMediaPlayer .... 09-16 10:36:06.955:E / MediaPlayer-JNI(10150):QCMediaPlayer媒体播放器不存在 09-16 10:36:06.975:V / MediaPlayerNative:在断开连接的媒体播放器上通知(8,0,0)回调 09-16 10:36:07.025:E / MediaPlayerNative:[MediaPlayer :: getMediaSystemInfo] key = 4000 09-16 10:36:07.025:D / MediaPlayerNative:MediaPlayer :: invoke :: getparam 09-16 10:26:07.025:D / MediaPlayerNative:MediaPlayer :: getParameter mCurrentState = 0x00000004 09-16 10:36:07.035:E / MediaPlayerNative:[MediaPlayer :: getMediaSystemInfo] key = 4000 09-16 10:36:07.035:D / MediaPlayerNative:MediaPlayer :: invoke :: getparam 09-16 10:36:07.035:D / MediaPlayerNative:MediaPlayer :: getParameter mCurrentState = 0x00000004 09-16 10:36:07.045:E / MediaPlayerNative:[MediaPlayer :: getMediaSystemInfo] key = 4000 09-16 10:36:07.045:D / MediaPlayerNative:MediaPlayer :: invoke :: getparam 09-16 10:36:07.045:D / MediaPlayerNative:MediaPlayer :: getParameter mCurrentState = 0x00000008 09-16 10:36:07.045:V / MediaPlayerNative:MediaPlayer :: setLooping 09-16 10:26:07.045:V / MediaPlayerNative:MediaPlayer :: setVolume(0.000000,0.000000) 09-16 10:26:07.055:V / MediaPlayerNative:MediaPlayer :: setVolume(0.082400,0.082400) 09-16 10:36:07.055:E / MediaPlayerNative:[MediaPlayer :: getMediaSystemInfo] key = 4000 09-16 10:36: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未经发布而最终确定 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)
答案 0 :(得分:-2)
问题已解决。感谢其他开发人员的糟糕代码。谢谢大家。