ApiDemos学习知识点之media-VideoView(10)

时间:2022-01-18 09:26:20

//将path变量设置为流视频URL或本地媒体文件路径。

先看一下图片

ApiDemos学习知识点之media-VideoView(10)

如图所示,本文主要是通过VideoView控件进行视频音频的播放。

先看一下布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<VideoView
android:id="@+id/surface_view"
android:layout_width="320px"
android:layout_height="240px"
/>

</LinearLayout>
很简单的加入了一个VideoView的控件,并设置了宽和高

下面看一下代码及代码分析

public class VideoViewDemo extends Activity {

/**
* TODO: Set the path variable to a streaming video URL or a local media
* file path.
*/
private String path = "";
private VideoView mVideoView;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.videoview);
mVideoView = (VideoView) findViewById(R.id.surface_view);

if (path == "") {
// 告诉用户提供一个媒体文件URL /路径。.
Toast.makeText(
VideoViewDemo.this,
"Please edit VideoViewDemo Activity, and set path"
+ " variable to your media file URL/path",
Toast.LENGTH_LONG).show();

} else {

/*
* 或者,对于流媒体,您可以使用mvideoview . setvideouri(uri . parse(URLstring));
*/
mVideoView.setVideoPath(path);
mVideoView.setMediaController(new MediaController(this));//添加播放控制条,还是自定义好点
mVideoView.requestFocus();

}
}
}
//其他方法:mVideoView.pause();mVideoView.stop();mVideoView.resume();mVideoView.setOnPreparedListener(this);mVideoView.setOnErrorListener(this);mVideoView.setOnCompletionListener(this);**。