5

我想在 android 视频视图中流式传输视频表单 url。我使用示例 api 代码并对其进行了少量修改来满足我的需要。我的代码是

public class VideoViewDemo extends Activity {

private static final String TAG = "VideoViewDemo";
private String current;

/**
 * TODO: Set the path variable to a streaming video URL or a local media
 * file path.
 */
private String path = "http://www.boisestatefootball.com/sites/default/files/videos/original/01%20-%20coach%20pete%20bio_4.mp4";
private VideoView mVideoView;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.videoview);
    mVideoView = (VideoView) findViewById(R.id.surface_view);
    runOnUiThread(new Runnable() {
        public void run() {
            playVideo();
        }
    });
}

private void playVideo() {
    try {
        // final String path = path;
        Log.v(TAG, "path: " + path);
        if (path == null || path.length() == 0) {
            Toast.makeText(VideoViewDemo.this, "File URL/path is empty",
                    Toast.LENGTH_LONG).show();

        } else {
            // If the path has not changed, just start the media player
            if (path.equals(current) && mVideoView != null) {
                mVideoView.start();
                mVideoView.requestFocus();
                return;
            }
            current = path;
            mVideoView.setVideoPath(getDataSource(path));
            mVideoView.start();
            mVideoView.setMediaController(new MediaController(this));
            mVideoView.requestFocus();

        }
    } catch (Exception e) {
        Log.e(TAG, "error: " + e.getMessage(), e);
        if (mVideoView != null) {
            mVideoView.stopPlayback();
        }
    }
}

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");
        temp.deleteOnExit();
        String tempPath = temp.getAbsolutePath();
        FileOutputStream out = new FileOutputStream(temp);
        byte buf[] = new byte[128];
        do {
            int numread = stream.read(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;
    }
}
}

在这里你可以看到我正在使用 uithread 流式传输视频。有什么方法可以通过我的线程处理这个

我试过的是

new Thered(new Runnable() {
        public void run() {
            playVideo();
        }
    }).start();

但它失败了

而且在 Android 2.2 中运行时,第一个代码运行,它error(-38,0)在 Android 2.1 中显示这个错误是什么?我检查了这个文件,但找不到这是什么错误??

有人可以指导我吗??

4

1 回答 1

2

您不需要获取整个视频,并将其保存在文件系统中,然后运行它..您提到的视频大小为 32Mb,通过网络获取需要很长时间。相反,您可以提供指向 videoview 的直接链接,它将逐步获取/缓冲视频并播放它。您试图在 UI 线程中获取视频数据,这是不可接受的。这是更正的代码,您可以检查它。

public class VideoViewDemo extends Activity {

private static final String TAG = "VideoViewDemo";
private String current;

/**
 * TODO: Set the path variable to a streaming video URL or a local media
 * file path.
 */
private String path = "http://www.boisestatefootball.com/sites/default/files/videos/original/01%20-%20coach%20pete%20bio_4.mp4";
private VideoView mVideoView;

private Handler handler;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    handler = new Handler();
    setContentView(R.layout.videoview);
    mVideoView = (VideoView) findViewById(R.id.surface_view);
/*    runOnUiThread(new Runnable() {
        public void run() {
            playVideo();
        }
    });
*/    
    playVideo();
    Log.v(TAG, "activity oncreate finished");
}

private void playVideo() {
    try {
        // final String path = path;
        Log.v(TAG, "path: " + path);
        if (path == null || path.length() == 0) {
            Toast.makeText(VideoViewDemo.this, "File URL/path is empty",
                    Toast.LENGTH_LONG).show();

        } else {
            // If the path has not changed, just start the media player
            if (path.equals(current) && mVideoView != null) {
                mVideoView.start();
                mVideoView.requestFocus();
                return;
            }
            current = path;

            mVideoView.setVideoPath(path);
            mVideoView.start();
            mVideoView.setMediaController(new MediaController(VideoViewDemo.this));
            mVideoView.requestFocus();

        }
    } catch (Exception e) {
        Log.e(TAG, "error: " + e.getMessage(), e);
        if (mVideoView != null) {
            mVideoView.stopPlayback();
        }
    }
}

}
于 2013-08-07T11:48:43.270 回答