长时间(超过24小时)运行后,Android VideoView冻结

时间:2018-10-21 05:39:45

标签: java android android-studio android-videoview

我正在制作一个可循环播放视频的应用程序。只要打开设备,它就可以一直播放。我有一个变量中的视频列表,并且使用索引在每个视频中循环播放。该应用程序运行良好,但是当它在某个点运行了多个小时(超过20小时)后,videoView会冻结在一个帧上,并且不会调用onCompletiononError侦听器。我在具有API 17和API 19的设备上运行此命令。它们都收到相同的错误。 这是我的代码(执行播放循环的两种方法):

public void playNextVideo(){
    if(playIndex < 0) { playIndex = 0; }
     if(allPromos.size() > 0) {
          try {
              Promo p = allPromos.get(playIndex);
              playVideo(p);
          } catch (IndexOutOfBoundsException e) {
              Log.w("EXCEPTION", e);
              isPlaying = false;
          } finally {
              playIndex++;
              if (playIndex >= allPromos.size()) {
                  playIndex = 0;
              }
          }
      } else {
          Log.d("PlayNextVideo", "No videos in the playlist");
      }
}

public void playVideo(Promo promo) {
    File videoFile = new File(promo.getLocal_link());
    if (!videoFile.exists()) {
        Log.d("VIDEOPLAYER", "Video " + playIndex + " Has been skipped. File not found");
        playNextVideo();
    } else {
        if (videoView.isPlaying()) {
            videoView.stopPlayback();
        }
        videoView.setVideoPath(promo.getLocal_link());
        videoView.requestFocus();
        videoView.start();

        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setLooping(false);
            }
        });

        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                videoView.stopPlayback();
                playNextVideo();
            }
        });

        videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.d("MEDIA PLAYER ERROR", what + ", " + extra);
                videoView.stopPlayback();
                try{
                    mp.release();
                } catch (Exception e) {
                    Log.w("RELEASE EXCEPTION", e);
                }
                playNextVideo();
                return true;
            }
        });
    }
}

0 个答案:

没有答案