0

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

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

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

任何解决方案。

谢谢

私人无效播放视频(){

    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;
    }
}
4

1 回答 1

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
于 2014-01-07T03:25:16.550 回答