edw*_*win 5 java multithreading android video-streaming android-videoview
我想在Android视频视图中流式视频表单网址.我使用示例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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在这里你可以看到我正在使用uithread来传输视频.有什么方法可以通过我的线程处理这个问题
我尝试的是
new Thered(new Runnable() {
public void run() {
playVideo();
}
}).start();
Run Code Online (Sandbox Code Playgroud)
但它失败了
而且在Android 2.2中运行第一个代码运行,它error(-38,0)在Android 2.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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)