我想在android视频视图流视频表单url。我使用示例API代码,做小的修改在实现我的需要。我的代码是Android视频查看另一个线程&问题与Android 2.1
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)
我n Android 2.1这个错误是什么?我检查了This File,但无法找出这是什么错误?
有人可以指导我吗?
有一个按钮说'播放视频',并在其点击功能放置您的Runnable线程代码。在OnCreate()函数中拥有自己的线程可能会变得混乱,因为活动创建必须始终保持良好状态。顺便说一下,你是在Android模拟器还是手机上试过这个? – 2013-04-12 18:22:06
@RahulSundar我试过在手机上。我不想使用按钮。我希望在进入活动时直接播放视频。这就是为什么我没有使用任何按钮 – edwin 2013-04-13 14:32:32
好吧。只是为了检查线程是否一切正常,从一个按钮启动它。然后在OnCreate()函数中使用相同的。为了确保活动创建完成后视频呈现开始,请给予足够的延迟睡眠(5秒)并检查一切是否正常。 – 2013-04-13 15:32:02