gpt4 book ai didi

java - Android Video View in another Thread & Issue with android 2.1

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:06:04 29 4
gpt4 key购买 nike

我想要在 android 视频 View 中流式传输视频形式的 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但找不到这是什么错误??

谁能指导我??

最佳答案

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

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();
}
}
}

}

关于java - Android Video View in another Thread & Issue with android 2.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15090187/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com