当前位置:   article > 正文

Android VideoView

android videoview

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 (Android VideoView)

Android VideoView class is used to display Video files in them. Following are the acceptable formats:

Android VideoView类用于在其中显示视频文件。 以下是可接受的格式:

  • 3gp

    3gp
  • MP4 – Only H.263, H.264, H.264 codecs work.

    MP4 –仅H.263,H.264,H.264编解码器有效。

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上使用的一些方法:

  • setVideoURI() – This is used to set the url path. It needs to be parsed as a URI.

    setVideoURI()–用于设置URL路径。 它需要解析为URI。
  • setVideoPath() – Used for local paths.

    setVideoPath()–用于本地路径。
  • start()

    开始()
  • stopPlayback()

    stopPlayback()
  • seekTo(int milliSec)

    seekTo(int milliSec)
  • pause()

    暂停()
  • resume()

    恢复()
  • isPlaying()

    正在玩()
  • canPause()

    canPause()
  • canSeekForward()

    canSeekForward()
  • canSeekBackward()

    canSeekBackward()
  • setOnCompletedListener()

    setOnCompletedListener()
  • addSubtitleSource()

    addSubtitleSource()
  • setMediaController() : Used to add MediaControls on the Video. Pause/Play, Seek

    setMediaController():用于在视频上添加MediaControls。 暂停/播放,搜寻
  • getDuration()

    getDuration()
  • setOnPreparedListener() : Gets invoked once the video starts.

    setOnPreparedListener():视频开始播放后调用。

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

Android VideoView示例 (Android VideoView Example)

Do not forget to add the Internet Permission in your AndroidManifest.xml file.

不要忘记在您的AndroidManifest.xml文件中添加Internet权限。

Android VideoView示例代码 (Android VideoView Example Code)

activity_main.xml

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
  3. xmlns:tools="https://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context="com.journaldev.videoview.MainActivity">
  7. <TextView
  8. android:id="@+id/txtPlaceholder"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_above="@+id/videoView"
  12. android:layout_centerHorizontal="true"
  13. android:layout_margin="16dp"
  14. android:text="DOUBLE TAP TO VIEW CONTROLS"
  15. android:textStyle="bold" />
  16. <VideoView
  17. android:id="@+id/videoView"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_centerHorizontal="true"
  21. android:layout_centerVertical="true" />
  22. </RelativeLayout>

MainActivity.java (MainActivity.java)

The code for the MainActivity.java looks like this:

MainActivity.java的代码如下所示:

  1. package com.journaldev.videoview;
  2. import android.media.MediaPlayer;
  3. import android.net.Uri;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.widget.MediaController;
  8. import android.widget.Toast;
  9. import android.widget.VideoView;
  10. import java.util.ArrayList;
  11. import java.util.Arrays;
  12. public class MainActivity extends AppCompatActivity {
  13. VideoView videoView;
  14. 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"));
  15. int index = 0;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20. videoView = findViewById(R.id.videoView);
  21. final MediaController mediacontroller = new MediaController(this);
  22. mediacontroller.setAnchorView(videoView);
  23. videoView.setMediaController(mediacontroller);
  24. videoView.setVideoURI(Uri.parse(arrayList.get(index)));
  25. videoView.requestFocus();
  26. videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
  27. @Override
  28. public void onCompletion(MediaPlayer mp) {
  29. Toast.makeText(getApplicationContext(), "Video over", Toast.LENGTH_SHORT).show();
  30. if (index++ == arrayList.size()) {
  31. index = 0;
  32. mp.release();
  33. Toast.makeText(getApplicationContext(), "Video over", Toast.LENGTH_SHORT).show();
  34. } else {
  35. videoView.setVideoURI(Uri.parse(arrayList.get(index)));
  36. videoView.start();
  37. }
  38. }
  39. });
  40. videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
  41. @Override
  42. public boolean onError(MediaPlayer mp, int what, int extra) {
  43. Log.d("API123", "What " + what + " extra " + extra);
  44. return false;
  45. }
  46. });
  47. }
  48. }

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:

输出看起来像这样:

MediaControl为什么在VideoView之外? (Why is the MediaControl outside the VideoView?)

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类现在看起来像这样:

  1. package com.journaldev.videoview;
  2. import android.media.MediaPlayer;
  3. import android.net.Uri;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.widget.MediaController;
  8. import android.widget.Toast;
  9. import android.widget.VideoView;
  10. import java.util.ArrayList;
  11. import java.util.Arrays;
  12. public class MainActivity extends AppCompatActivity {
  13. VideoView videoView;
  14. 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"));
  15. int index = 0;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20. videoView = findViewById(R.id.videoView);
  21. final MediaController mediacontroller = new MediaController(this);
  22. mediacontroller.setAnchorView(videoView);
  23. videoView.setMediaController(mediacontroller);
  24. videoView.setVideoURI(Uri.parse(arrayList.get(index)));
  25. videoView.requestFocus();
  26. videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
  27. @Override
  28. public void onPrepared(MediaPlayer mp) {
  29. mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
  30. @Override
  31. public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
  32. videoView.setMediaController(mediacontroller);
  33. mediacontroller.setAnchorView(videoView);
  34. }
  35. });
  36. }
  37. });
  38. videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
  39. @Override
  40. public void onCompletion(MediaPlayer mp) {
  41. Toast.makeText(getApplicationContext(), "Video over", Toast.LENGTH_SHORT).show();
  42. if (index++ == arrayList.size()) {
  43. index = 0;
  44. mp.release();
  45. Toast.makeText(getApplicationContext(), "Videos completed", Toast.LENGTH_SHORT).show();
  46. } else {
  47. videoView.setVideoURI(Uri.parse(arrayList.get(index)));
  48. videoView.start();
  49. }
  50. }
  51. });
  52. videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
  53. @Override
  54. public boolean onError(MediaPlayer mp, int what, int extra) {
  55. Log.d("API123", "What " + what + " extra " + extra);
  56. return false;
  57. }
  58. });
  59. }
  60. }

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:

android studio videoview tutorial

现在,上面应用程序的输出如下:

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项目。

翻译自: https://www.journaldev.com/20671/android-videoview

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/499339
推荐阅读
相关标签
  

闽ICP备14008679号