开发者

How can I detect an interruption in an Android VideoView playback?

I have a VideoView with all the Listeners defined and I'm trying to track down what to do if the network connection terminates during playback.

Currently the video just fr开发者_如何转开发eezes, and the OnErrorListener is not called. The only way to recover is to press the back button.

Any ideas?


A workaround might be to download the video file onto the sdcard (ex: with URLConnection and InputStream). I know for sure you could detect a network connection error during this download. Then when the download is done, the VideoView can play the video file directly from your local sdcard. The only issue with this is the VideoView must wait for the entire video to download before playing.


I solved this by setting up a timer which checks for connectivity.

public class CheckNetworkConnection extends AsyncTask {

private boolean networkIsAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null;
}

@Override
protected String doInBackground(String... params) {
    while (vv.isPlaying()) {
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            return "";
        }
        if ( !networkIsAvailable()) {
            vv.stopPlayback();
            VideoTime.this.finish();                
        }
    }
    return "";
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜