android ::如何在VideoView中播放视频时添加合适的延迟

时间:2013-06-20 21:08:12

标签: android android-videoview

我是android的新手。目前,我正在播放Android VideoView中的URL视频。 我已经搜索了很多,以便找到一种方法,如何通过Android代码在视频播放中添加重复延迟。

使用Thread.sleep(3000),我只能延迟一次但是我想在每5秒钟后延迟视频播放。

任务: 条件1:对于用户1 我必须在播放视频时添加重复延迟;意味着视频将播放5秒钟,然后它将遇到大约延迟。 4-5秒。事实上,我正在尝试创建一种情况,向观察者显示在视频缓冲期间存在一些延迟。

任何解决方案。

由于

private void playVideo(){

    try {
    final String path = "http://daily3gp.com/vids/747.3gp";
        if (path == null || path.length() == 0) {
            Toast.makeText(CStreaming.this, "File URL/path is empty",
                    Toast.LENGTH_LONG).show();
        } 

        else {              
            **Thread.sleep(3000);**
            // If the path has not changed, just start the media player
            Log.v(TAG,"Thread Sleep ....3000 msec");
            if (path.equals(current) && mVideoView != null) {
                mVideoView.start();
                mVideoView.requestFocus();
                return;
            }
            current = path;
            Log.i(TAG,"Value of path in PlayVideo()=" + path);
            mVideoView.setVideoPath(getDataSource(path));

            mVideoView.start();
            mVideoView.requestFocus();
            }

}

// getDataSource方法将由playVideo()

使用
private String getDataSource(String path) throws IOException {
    if (!URLUtil.isNetworkUrl(path)) {
        return path;
    } else {

        URL url = new URL(path);
        URLConnection cn = url.openConnection();
        cn.connect();
        InputStream stream = cn.getInputStream();
        if (stream == null)
            throw new RuntimeException("stream is null");
        File temp = File.createTempFile("mediaplayertmp", "dat",this.getCacheDir());
        temp.deleteOnExit();
        String tempPath = temp.getAbsolutePath();
        FileOutputStream out = new FileOutputStream(temp);
        byte buf[] = new byte[128];
        do {
            int numread = stream.read(buf);
        Log.i(TAG,"Buffer Printing via Array: " + Arrays.toString(buf));
            if (numread <= 0)
                break;
            out.write(buf, 0, numread);
        } while (true);
        try {
            stream.close();
        } catch (IOException ex) {
            Log.e(TAG, "error: " + ex.getMessage(), ex);
        }
        return tempPath;
    }
}

1 个答案:

答案 0 :(得分:0)

试试这段代码:

Handler h = new Handler(Looper.getMainLooper());

Runnable r = new Runnable() {
    @Override
    public void run() {
        //--code run in Main, UI thread  
        DoSomeThing();
    }
};

h.postDelayed(r,2000); //-- run after 2 seconds