首页 文章

MediaPlayer准备失败IOException

提问于
浏览
1

我的问题是,有太多的MediaPlayers导致IOException preparefailed:status = 0x1来记录?

我的程序运行方式是我为每个想要播放的视频使用单独的Media Player实例 . 在运行结束时,我停止videoPlayer,释放它,并将其变为null;这有时候可以,但有时候当我在视频之间移动太快时,我会得到一个IO异常而视频将无法播放 . 我还有一个mediaPlayer在服务中播放一些背景音乐 .

基本上,每次文件结束播放时,我的视频活动都会收到一个新的呼叫 . 这可能是错误,我应该尝试使用不同的文件重用相同的媒体播放器?

提前致谢

1 回答

  • 1

    我找到了自己的答案 . 我不知道这是否是一个很好的方法,但是:

    if(videoFile != null)
            {
                Log.i("INITPLAYER", videoFile);
                afd = getAssets().openFd(videoFile);
    
                instructionVideoPlayer = new MediaPlayer();
    
                instructionVideoPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
    
                instructionVideoPlayer.setDisplay(holder);
    
                instructionVideoPlayer.prepare();
    
                instructionVideoPlayer.setOnCompletionListener(instructionVideoComplete);
                instructionVideoPlayer.setOnPreparedListener(this);
            }
            else
                Log.i("VideoPlayer", "noVideoFile");
    
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            Log.i(this.toString(), "IOEXception");
            e.printStackTrace();
    
    //Here is the fix:
            instructionVideoPlayer.release();
            instructionVideoPlayer = null;
            initPlayer();
    // reinit after prepare failed. although this can bring in an infinte loop if video file does not exits
        } catch (Exception e)
        {
            Log.i("InitPlayer", e.getClass().toString());
            e.printStackTrace();
        }
    

相关问题