赞
踩
In this tutorial, we’ll learn about Android VideoView. We will create an android app in Android Studio and play video from URL, we will also do some customizations to it’s control panel.
在本教程中,我们将学习Android VideoView。 我们将在Android Studio中创建一个android应用并从URL播放视频,我们还将对其控制面板进行一些自定义。
Android VideoView class is used to display Video files in them. Following are the acceptable formats:
Android VideoView类用于在其中显示视频文件。 以下是可接受的格式:
VideoViews can play videos either from resource files, local data or url specified. Following are some of the methods used on VideoView:
VideoView可以播放来自资源文件,本地数据或指定URL的视频。 以下是VideoView上使用的一些方法:
Note: VideoView does not retain its full state when going into the background.
注意:进入背景时,VideoView不会保留其完整状态。
In the following section, we’ll be creating an application that runs Videos from urls one after the other.
We’ll see how the MediaController
works with the VideoView
.
在下一节中,我们将创建一个应用程序,该应用程序一个接一个地运行来自url的视频。
我们将看到MediaController
如何与VideoView
。
Do not forget to add the Internet Permission in your AndroidManifest.xml file.
不要忘记在您的AndroidManifest.xml文件中添加Internet权限。
activity_main.xml
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.journaldev.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>
The code for the MainActivity.java looks like this:
MainActivity.java的代码如下所示:
- package com.journaldev.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;
- }
- });
- }
- }
We’ve added two urls in an ArrayList. We set the anchorView()
on the VideoView to keep the MediaControl
inside the VideoView
.
我们在ArrayList中添加了两个URL。 我们在VideoView上设置anchorView()
,以将MediaControl
保留在VideoView
。
The output looks something like this:
输出看起来像这样:
Well, the MediaControl doesn’t know the dimensions of the VideoView until the video is started.
好吧,在视频开始播放之前,MediaControl才知道VideoView的尺寸。
For having media control inside the video, we need to set the onPreparedListener
on our VideoView and then set the anchor inside it. This way the MediaController is set inside the VideoView.
为了在视频内部拥有媒体控制权,我们需要在VideoView上设置onPreparedListener
,然后在其中设置锚点。 这样,可以在VideoView内部设置MediaController。
Our updated MainActivity.java class looks like this now:
我们更新后的MainActivity.java类现在看起来像这样:
- package com.journaldev.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()
is set to release the media player resources. It should be done to prevent memory leaks. Any videoView calls after this line would lead to a CRASH.
mp.release()
设置为释放媒体播放器资源。 应该这样做以防止内存泄漏 。 此行之后的任何videoView调用都将导致CRASH。
Now the output of the above application in action is given below:
This brings an end to android video view tutorial. You can download the final Android VideoView project from the link below.
这就结束了android video view tutorial。 您可以从下面的链接下载最终的Android VideoView项目。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。