【问题标题】:Android Video View in another Thread & Issue with android 2.1另一个线程中的 Android 视频视图 & android 2.1 的问题
【发布时间】:2013-02-26 13:21:23
【问题描述】:

我想在 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 中运行时,第一个代码运行并在 Android 2.1 中显示 error(-38,0) 这是什么错误?我检查了This File,但找不到这是什么错误??

有人可以指导我吗?

【问题讨论】:

  • 有一个按钮说“播放视频”,并在其点击功能中放置您的可运行线程代码。如果处理不当,在 OnCreate() 函数中拥有自己的线程可能会变得混乱,因为活动创建必须始终正常进行。顺便问一下,您在 Android 模拟器或手机中尝试过吗?
  • @RahulSundar 我在电话上试过。我不想使用按钮。我希望在进入活动时直接播放视频。这就是为什么我没有使用任何按钮
  • 好的。只是为了检查线程是否一切正常,从按钮启动它。然后在 OnCreate() 函数中使用相同的方法。为确保 Activity 创建完成后开始视频渲染,请给予足够的延迟 Sleep(5 秒)并检查是否一切正常。
  • @RahulSundar 我会试试的。我使用相同的逻辑来播放音频。它可以正常工作。

标签: java android multithreading video-streaming android-videoview


【解决方案1】:

您不需要获取整个视频,并将其保存在文件系统中,然后运行它..您提到的视频大小为 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();
        }
    }
}

}

【讨论】:

  • @edwin,我的回答有用吗?
  • 感谢代码!我还有一个问题,你知道视频被完全观看后是否会保存在设备上的某个地方吗?那里可以访问吗?另请参阅我的question 关于该问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-28
  • 1970-01-01
  • 2017-02-22
  • 2021-07-30
  • 1970-01-01
  • 2011-08-06
  • 1970-01-01
相关资源
最近更新 更多