由于seekTo()导致m3u8声音丢失

时间:2012-05-30 07:36:28

标签: android audio streaming

我有一个播放 .mp4 的应用的源代码,我必须让它适用于HLS。

播放视频和声音,但我只有.m3u8:

有2个问题

1)当 seekTo()被激活(注释被删除)时,声音被禁用,当我想要退出播放器时,它需要很长时间才能完成,这会使应用程序崩溃。< / p>

2)setLooping不起作用并返回错误(-38,0),尝试执行错误的seekTo

state: mPlayer=0x1e0380, mCurrentState=0

注意:这些问题仅适用于.M3U8

播放器代码:

private void playVideo() {
    doCleanUp();
    try {
        // Create a new media player and set the listeners

        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setDataSource(mFilePath);
        mMediaPlayer.setDisplay(mSurfaceHolder);       
        mMediaPlayer.prepare(); 
        mMediaPlayer.start();
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnVideoSizeChangedListener(this);
        mMediaPlayer.setOnPreparedListener(this);
        //mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
        //mMediaPlayer.seekTo(mResumePosition);
        //mMediaPlayer.setLooping(true);


    } catch (Exception e) {
        Log.e(LOG_TAG, "error: " + e.getMessage(), e);
        showErrorMessage(mErrorPlayingVideo);
        // Toast.makeText(this, "Impossible de jouer la vidéo",
        // 5000).show();
    }
}

在setDataSource()下激活setLooping时记录:

error (-38, 0)
prepareAsync called in wrong state 0
prepareAsync_l return error =-38
error: null
java.lang.IllegalStateException
at android.media.MediaPlayer.prepare(Native Method)
at fr.niji.broadpeak.activity.BroadpeakDemoPlayer.playVideo(BroadpeakDemoPlayer.java:409)
at fr.niji.broadpeak.activity.BroadpeakDemoPlayer.onRequestFinished(BroadpeakDemoPlayer.java:585)
at fr.niji.lib.dataproxy.service.DataManager.handleResult(DataManager.java:262)
at fr.niji.lib.dataproxy.service.DataManager.onRequestFinished(DataManager.java:292)
at fr.niji.lib.dataproxy.service.ServiceHelper.handleResult(ServiceHelper.java:297)
at fr.niji.lib.dataproxy.service.ServiceHelper$EvalReceiver.onReceiveResult(ServiceHelper.java:119)
at android.os.ResultReceiver$MyRunnable.run(ResultReceiver.java:43)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4126)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)

以及代码下方:

Attempt to perform seekTo in wrong state: mPlayer=0x1e0380, 
mCurrentState=0
error (-38, 0)
Error (-38,0)
Error (-38,0)
Attempt to perform seekTo in wrong state: mPlayer=0x1e0380, mCurrentState=0
Error (-38,0)
Error (-38,0)
Attempt to perform seekTo in wrong state: mPlayer=0x1e0380, mCurrentState=0

2 个答案:

答案 0 :(得分:2)

好的,我发现了问题。当您使用带有.m3u8文件的seekTo()并且括号之间的值设置为0时,它不起作用所以我在playBackVideo()方法中添加了一个条件:

if (mResumePosition > 0) mMediaPlayer.seekTo(mResumePosition);

因此mResumePosition永远不会设置为0并在文件的开头播放。另外,我在

中解决了setLooping()问题

public void onCompletion(final MediaPlayer mediaPlayer)

我添加了mMediaPlayer.release(); playVideo();

循环效果非常好!

答案 1 :(得分:0)

你需要检查它是否是LIVE案例。如果是,则可能无法进行循环和搜索。