android 在线播放视频代码
时间: 2023-12-27 10:02:50 浏览: 36
要在Android上实现在线播放视频,可以使用VideoView类。以下是一个简单的示例代码:
1. 在布局文件中添加VideoView组件
```xml
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
2. 在Activity中设置VideoView的URI
```java
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
import androidx.appcompat.app.AppCompatActivity;
public class VideoPlayerActivity extends AppCompatActivity {
private VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player);
videoView = findViewById(R.id.video_view);
videoView.setMediaController(new MediaController(this));
videoView.setVideoURI(Uri.parse("http://example.com/video.mp4")); // 视频URL
videoView.start();
}
}
```
上面的代码中,我们首先在Activity的布局文件中添加了一个VideoView组件。然后,在Activity的onCreate()方法中,我们获取VideoView组件的引用,并设置一个MediaController对象,用于控制视频播放。接着,我们调用setVideoURI()方法,将视频的URL传递给VideoView组件,最后调用start()方法开始播放视频。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)