Android VideoView
时间:2020-02-23 14:29:22 来源:igfitidea点击:
在本教程中,我们将学习Android VideoView。
我们将在Android Studio中创建一个android应用并从URL播放视频,我们还将对其控制面板进行一些自定义。
Android VideoView
Android VideoView类用于其中显示视频文件。
以下是可接受的格式:
- 3gp
- MP4 –仅H.263,H.264,H.264编解码器有效。
VideoView可以播放来自资源文件,本地数据或者指定URL的视频。
以下是VideoView上使用的一些方法:
- setVideoURI()–用于设置URL路径。它需要解析为URI。
- setVideoPath()–用于本地路径。
- start()
- stopPlayback()
- seekTo(int milliSec)
- pause()
- resume()
- isPlaying()
- canPause()
- canSeekForward()
- canSeekBackward()
- setOnCompletedListener()
- addSubtitleSource()
- setMediaController():用于在视频上添加MediaControls。暂停/播放,搜寻
- getDuration()
- setOnPreparedListener():视频开始播放后被调用。
注意:进入背景时,VideoView不会保留其完整状态。
在下一节中,我们将创建一个应用程序,该应用程序一个接一个地运行来自url的视频。
我们将看到MediaController与VideoView一起工作。
Android VideoView示例
不要忘记在您的AndroidManifest.xml文件中添加Internet权限。
Android VideoView示例代码
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.theitroad.videoview.MainActivity">
<TextView
android:id="@+id/txtPlaceholder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/videoView"
android:layout_centerHorizontal="true"
android:layout_margin="16dp"
android:text="DOUBLE TAP TO VIEW CONTROLS"
android:textStyle="bold"
<VideoView
android:id="@+id/videoView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
</RelativeLayout>
MainActivity.java
MainActivity.java的代码如下所示:
package com.theitroad.videoview;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
import java.util.ArrayList;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
VideoView videoView;
ArrayList<String> arrayList = new ArrayList<>(Arrays.asList("https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4", "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4"));
int index = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = findViewById(R.id.videoView);
final MediaController mediacontroller = new MediaController(this);
mediacontroller.setAnchorView(videoView);
videoView.setMediaController(mediacontroller);
videoView.setVideoURI(Uri.parse(arrayList.get(index)));
videoView.requestFocus();
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Toast.makeText(getApplicationContext(), "Video over", Toast.LENGTH_SHORT).show();
if (index++ == arrayList.size()) {
index = 0;
mp.release();
Toast.makeText(getApplicationContext(), "Video over", Toast.LENGTH_SHORT).show();
} else {
videoView.setVideoURI(Uri.parse(arrayList.get(index)));
videoView.start();
}
}
});
videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.d("API123", "What " + what + " extra " + extra);
return false;
}
});
}
}
我们在ArrayList中添加了两个网址。
我们在VideoView上设置了anchorView(),以将MediaControl保留在VideoView中。
输出看起来像这样:
MediaControl为什么在VideoView之外?
好吧,直到视频开始播放之前,MediaControl才知道VideoView的尺寸。
为了在视频中拥有媒体控制权,我们需要在VideoView上设置" onPreparedListener",然后其中设置锚点。
这样,可以在VideoView内部设置MediaController。
我们更新后的MainActivity.java类现在看起来像这样:
package com.theitroad.videoview;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
import java.util.ArrayList;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
VideoView videoView;
ArrayList<String> arrayList = new ArrayList<>(Arrays.asList("https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4", "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4"));
int index = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = findViewById(R.id.videoView);
final MediaController mediacontroller = new MediaController(this);
mediacontroller.setAnchorView(videoView);
videoView.setMediaController(mediacontroller);
videoView.setVideoURI(Uri.parse(arrayList.get(index)));
videoView.requestFocus();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
videoView.setMediaController(mediacontroller);
mediacontroller.setAnchorView(videoView);
}
});
}
});
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Toast.makeText(getApplicationContext(), "Video over", Toast.LENGTH_SHORT).show();
if (index++ == arrayList.size()) {
index = 0;
mp.release();
Toast.makeText(getApplicationContext(), "Videos completed", Toast.LENGTH_SHORT).show();
} else {
videoView.setVideoURI(Uri.parse(arrayList.get(index)));
videoView.start();
}
}
});
videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.d("API123", "What " + what + " extra " + extra);
return false;
}
});
}
}
设置mp.release()以释放媒体播放器资源。
应该这样做以防止内存泄漏。
在此行之后的任何videoView调用都将导致CRASH。

