android studio的videoview知识点
时间: 2023-11-18 14:04:43 浏览: 150
Android Studio中的VideoView是一个用于播放视频的视图组件。它可以在应用程序中嵌入视频并控制视频的播放。以下是一些关于Android Studio中VideoView的知识点:
1. 在XML布局文件中添加VideoView组件:
```xml
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
```
2. 在Java代码中设置VideoView的路径和媒体控制器:
```java
VideoView videoView = findViewById(R.id.videoView);
String videoPath = "android.resource://" + getPackageName() + "/" + R.raw.video;
Uri uri = Uri.parse(videoPath);
videoView.setVideoURI(uri);
MediaController mediaController = new MediaController(this);
videoView.setMediaController(mediaController);
mediaController.setAnchorView(videoView);
```
3. 自定义VideoView:
```java
public class CustomVideoView extends VideoView {
public CustomVideoView(Context context) {
super(context);
}
public CustomVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = getDefaultSize(0, widthMeasureSpec);
int height = getDefaultSize(0, heightMeasureSpec);
setMeasuredDimension(width, height);
}
}
```
4. 在XML布局文件中使用自定义VideoView:
```xml
<com.example.CustomVideoView
android:id="@+id/customVideoView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
```
5. 在Java代码中使用自定义VideoView:
```java
CustomVideoView customVideoView = findViewById(R.id.customVideoView);
String videoPath = "android.resource://" + getPackageName() + "/" + R.raw.video;
Uri uri = Uri.parse(videoPath);
customVideoView.setVideoURI(uri);
MediaController mediaController = new MediaController(this);
customVideoView.setMediaController(mediaController);
mediaController.setAnchorView(customVideoView);
```
阅读全文