我想在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