您好!欢迎来到源码码网

Android VideoView视频以及视频播放器实例

  • 源码教程
  • 来源:源码码网
  • 编辑:admin
  • 时间:2021-01-14 20:02
  • 阅读:431

首先我们来看一下 Android N 支持的视频文件有哪些。Android N 支持的视频格式下表所示。

Android N 支持的视频文件

image.png

视频播放器

与音频播放相比,视频播放需要使用视觉组件将影像显示出来。

在 Android SDK 中提供了多种播放视频文件的方法。例如,可以用 VideoView 或 SurfaceView 来播放视频,其中使用 VideoView 组件播放视频最为方便。

实例 VideoPlayDemo 演示了使用 android.widget.VideoView 组件进行视频播放的方法,运行效果如图 1 所示。

VideoPlayDemo运行结果

实例 VideoPlayDemo 中含有两个 Activity,其中 PlayVideo 含有 VideoView 组件对象,用于播放视频。视频文件存放在 SD 卡中,路径为“Movies/movie.3gp”。而 VideoPlayAcitvity 为主 Activity,用于启动 PlayVideo。

实例 VideoPlayDemo 中 MainActivity.java 的代码如下:


  1. package introduction.android.videoplaydemo;



  2. import android.app.Activity;

  3. import android.content.Intent;


  4. import android.os.Bundle;

  5. import android.view.View;


  6. import android.view.View.OnClickListener;

  7. import android.widget.Button;


  8. public class MainAcitvity extends Activity {


  9. /**

  10.     * Called when the activity is first created.

  11.     */

  12. private Button buttonOl;


  13. @Override


  14. public void onCreate(Bundle savedInstanceState) {

  15. super.onCreate(savedInstanceState);

  16. setContentView(R.layout.main);

  17. button01 = (Button) findViewById(R.id.buttonOl);

  18. button0l.setOnClickListener(new buttonListener());

  19. }


  20. class buttonListener implements OnClickListener {

  21. @Override

  22. public void onClick(View v) {

  23. // TODO Auto-generated method stub

  24. Intent intent = new Intent(MainActivity.this, PlayVideo.class);

  25. MainAcitvity.this.startActivity(intent);


  26. }


  27. }

  28. }

实例 VideoPlayDemo 中 PlayVideo.java 的代码如下:


  1. package introduction.android.videoplaydemo;



  2. import android.app.Activity;

  3. import android.media.MediaPlayer;

  4. import android.net.Uri;

  5. import android.os.Bundle;

  6. import android.widget.MediaController;

  7. import android.widget.Toast;

  8. import android.widget.VideoView;


  9. public class PlayVideo extends Activity {

  10. private VideoView videoView;

  11. private MediaController mc;

  12. private String path;


  13. @Override


  14. public void onCreate(Bundle savedInstanceState) {

  15. super.onCreate(savedInstanceState);

  16. setContentView(R.layout.other);


  17. videoView = (VideoView) this.findViewById(R.id.videoView);

  18. path = "sdcard/Movies/movie.3gp";

  19. mc = new MediaController(this);

  20. videoView.setMediaController(mc);

  21. videoView.setVideoPath(path);

  22. videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

  23. @Override

  24. public void onCompletion(MediaPlayer argO) {

  25. // TODO Auto-generated method stub

  26. finish();

  27. }

  28. });

  29. videoView.requestFocus();

  30. videoView.start();

  31. }

  32. }

其中,MediaController 类为 Android SDK 提供的视频控制器,用于显示播放时间,对播放视频进行控制。

通过 VideoView 类的 setMediaController() 方法可以将视频控制器和 VideoView 类结合在一起,对 VideoView 中播放的视频进行控制,大大降低了编码强度。

由于要播放的视频为放置在 SD 卡中的“Movies/movie.3gp”文件,VideoView 组件使用 setVideoPath() 方法即可指定该文件,并通过 start() 方法进行播放。

  1. videoView.setOnCompletionListener(new OnCompletionListener(){

  2. @Override

  3. public void onCompletion(MediaPlayer argO) {

  4. // TODO Auto-generated method stub

  5. finish();

  6. }

  7. })

这行代码指定了 videoView 组件的视频播放完成事件的触发器,当视频播放完成后,关闭当前 Activity。

PlayVideo 使用的布局为 R.layout.other,该布局中含有 VideoView 组件,其所对应的 XML 文件 other.xml 的代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="fill_parent"

  4. android:layout_height="fill_parent"

  5. android:orientation="vertical">

  6. <VideoView

  7. android:id="@+id/videoView"

  8. android:layout_width="320px"

  9. android:layout_height="240px" />

  10. </LinearLayout>


实例 VideoPlayDemo 中 AndroidManifest.xml 文件的代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>

  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"

  3. package="introduction.android.videoplaydemo">


  4. <application

  5. android:allowBackup="true"

  6. android:icon="@mipmap/ic_launcher"

  7. android:label="@string/app_name"

  8. android:roundIcon="@mipmap/ic_launcher_round"

  9. android:supportsRtl="true"

  10. android:theme="@style/AppTheme">

  11. <activity android:name=".MainActivity">

  12. <intent-filter>

  13. <action android:name="android.intent.action.MAIN" />

  14. <category android:name="android.intent.category.LAUNCHER" />

  15. </intent-filter>

  16. </activity>

  17. <activity android:name="introduction.android.playvideo.PlayVideo" />

  18. </application>


  19. </manifest>

此外,VideoView 也支持网络流媒体的播放,只需将 VideoView 的 setVideoPath() 方法替换为 setViewURI(),并指定对应的 URI 即可。

需要注意的是,并不是所有的 MP4 和 3GP 文件都可以被 VideoView 组件播放,只有使用 progressive streamable 模式转化的影片才可以被播放。

播放网络流媒体文件时,需要在 AndroidManifest.xml 文件中添加相应权限:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>

  • android.permission.INTERNET 权限使当前应用程序可以访问网络资源;

  • android. permission.WAKE_LOCK 权限使当前应用程序运行时,手机不会进入休眠状态,以便于视频播放。


使用 SurfaceView 组件播放视频的方法也不复杂,而且更加灵活。

实例 MediaPlayerVideoDemo 演示了使用 SurfaceView 和 MediaPlayer 组件播放视频的方法,运行效果如图 2 所示。

MediaPlayerVideoDemo运行结果

图 2  MediaPlayerVideoDemo的运行效果


对应的布局文件 main.xml 的内容如下:

  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="fill_parent"

  4. android:layout_height="fill_parent"

  5. android:orientation="vertical">


  6. <SurfaceView

  7. android:id="@+id/surfaceView1"

  8. android:layout_width="fill_parent"

  9. android:layout_height="wrap_content"

  10. android:layout_weight="1.01" />


  11. <LinearLayout

  12. android:id="@+id/linearLayout1"

  13. android:layout_width="match_parent"

  14. android:layout_height="wrap_content"

  15. android:gravity="center">


  16. <Button

  17. android:id="@+id/button1"

  18. android:layout_width="wrap_content"

  19. android:layout_height="wrap_content"

  20. android:text="播放" />


  21. <Button

  22. android:id="@+id/button2"

  23. android:layout_width="wrap_content"

  24. android:layout_height="wrap_content"

  25. android:text="暂停" />


  26. <Button

  27. android:id="@+id/button3"

  28. android:layout_width="wrap_content"

  29. android:layout_height="wrap_content"

  30. android:text="重置" />


  31. <Button

  32. android:id="@+id/button4"

  33. android:layout_width="wrap_content"

  34. android:layout_height="wrap_content"

  35. android:text="停止" />

  36. </LinearLayout>

  37. </LinearLayout>


实例 MediaPlayerVideoDemo 的配置文件 AndroidManifest.xml 的内容如下:

  1. <?xml version="1.0" encoding="utf-8"?>

  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"

  3. package="introduction.android.videoPlayDemo"

  4. android:versionCode="1"

  5. android:versionName="1.0">


  6. <uses-sdk android:minSdkVersion="14" />

  7. <application

  8. android:icon="@drawable/ic_launcher"

  9. android:label="@string/app_name">

  10. <activity

  11. android:name=".VideoPlayDemoActivity"

  12. android:label="@string/app_name">

  13. <intnt-filter>

  14. <action android:name="android.intent.action.MAIN" />

  15. <category android:name="android.intent.category.LAUNCHER" />

  16. </intnt-filter>

  17. </activity>

  18. </application>

  19. </manifest>

实例 MediaPlayerVideoDemo 中的主 Activity 文件MainActivity.Java 的代码如下:


  1. package introduction.android.videoplaydemo;


  2. import java.io.IOException;


  3. import android.app.Activity;

  4. import android.media.AudioManager;

  5. import android.media.MediaPlayer;

  6. import android.os.Bundle;

  7. import android.util.Log;

  8. import android.view.SurfaceHolder;

  9. import android.view.SurfaceView;

  10. import android.view.View;


  11. import android.view.View.OnClickListener;

  12. import android.widget.Button;


  13. public class MainActivity extends Activity {


  14. /**

  15.     * Called when the activity is first created.

  16.     */

  17. private Button playbtn;

  18. private Button pausebtn;

  19. private Button replaybtn;

  20. private Button stopbtn;

  21. private SurfaceView surview;

  22. private SurfaceHolder surHolder;

  23. private MediaPlayer mp;


  24. private String path = "sdcard/movies/movie.3gp";

  25. protected boolean pause = false;


  26. @Override

  27. public void onCreate(Bundle savedInstanceState) {

  28. super.onCreate(savedInstanceState);

  29. setContentView(R.layout.main);


  30. surview = (SurfaceView) this.findViewById(R.id.surfaceView1);

  31. surHolder = surview.getHolder();

  32. surHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

  33. mp = new MediaPlayer();

  34. mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

  35. @Override

  36. public void onCompletion(MediaPlayer mp) {

  37. // TODO Auto-generated method stub

  38. Log.i("mediaplayer", "播放完成");

  39. }

  40. });

  41. playbtn = (Button) this.findViewById(R.id.buttonl);

  42. playbtn.setOnClickListener(new OnClickListener() {

  43. @Override

  44. public void onClick(View argO) {

  45. // TODO Auto-generated method stub

  46. if (!pause) {

  47. //开始播放

  48. mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

  49. mp.setDisplay(surHolder);

  50. try {

  51. mp.setDataSource(path);

  52. mp.prepare();

  53. mp.start();

  54. } catch (IOException e) {

  55. // TODO Auto-generated catch block

  56. e.printStackTrace();

  57. }

  58. } else {

  59. //暂停播放

  60. mp.start();

  61. pause = false;

  62. }

  63. }

  64. });

  65. pausebtn = (Button) this.findViewById(R.id.button2);

  66. pausebtn.setOnClickListener(new OnClickListener() {

  67. //暂停播放

  68. @Override

  69. public void onClick(View argO) {

  70. // TODO Auto-generated method stub

  71. if (mp != null) {

  72. pause = true;

  73. mp.pause();

  74. }

  75. }

  76. });

  77. replaybtn = (Button) this.findViewById(R.id.button3);

  78. replaybtn.setOnClickListener(new OnClickListener() {

  79. //重新播放

  80. @Override

  81. public void onClick(View argO) {

  82. // TODO Auto-generated method stub

  83. if (mp != null) {

  84. mp.seekTo(0);

  85. }

  86. }

  87. });

  88. stopbtn = (Button) this.findViewById(R.id.button4);

  89. stopbtn.setOnClickListener(new OnClickListener() {

  90. @Override

  91. public void onClick(View argO) {

  92. // TODO Auto-generated method stub

  93. if (mp != null) {

  94. mp.stop();

  95. mp.release();

  96. }

  97. }

  98. });

  99. }

  100. }


特别声明:
1、如无特殊说明,内容均为本站原创发布,转载请注明出处;
2、部分转载文章已注明出处,转载目的为学习和交流,如有侵犯,请联系客服删除;
3、编辑非《源码码网》的文章均由用户编辑发布,不代表本站立场,如涉及侵犯,请联系删除;
全部评论(0)
推荐阅读
  • bootstrap ui框架能用在uniapp中吗?
  • bootstrap ui框架能用在uniapp中吗?
  • BootstrapUI框架通常是前端开发中的一种工具,它提供了一套预定义的CSS样式和组件,用于快速构建响应式布局的网页。然而,UniApp是一个使用Vue.js开发跨平台应用的框架,它可以用来开发iOS、Android、以及各种小程序和H5应用。
  • 互动社区
  • 来源:源码码网
  • 编辑:热度建站
  • 时间:2024-04-12 00:04
  • 阅读:134
  • css实现banner图由中心点动态放大效果
  • css实现banner图由中心点动态放大效果
  • 在日常的网页设计中,为了让网页增加一定的特效以达到交互的目的,我们尝尝会在网页中使用一些动画效果。今天来说说实现banner图由中心点动态放大效果,实现这个效果需要用到css中的动画:animation​和关键帧:@keyframes,具体示例如下:
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-04-11 18:52
  • 阅读:148
  • countUp.js实现鼠标滑动到某个位置数字自动滚动增加的效果
  • countUp.js实现鼠标滑动到某个位置数字自动滚动增加的效果
  • 在网页开发中为了提升网页的交互效果,经常会用到使用js给网页增加一定的特效,下边就来说说使用js实现鼠标滑动到某个位置数字自动滚动增加的效果。其实这种效果有很多中解决办法,自己也可以去写,下边我们借助countUp.js来实现,关于这个js文件,我放在末尾:
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-04-08 09:20
  • 阅读:226
  • 响应式网页设计思路及注意事项
  • 响应式网页设计思路及注意事项
  • 一、什么是响应式网页响应式网页设计就是让网页具有根据设备类型应用CSS样式的能力。设计:设想、计划。设计就是把想法实现。网页设计:按照一定的设计思路布局网页内容。传统网页设计:都是针对PC端浏览器而设计的,不具备查询设备的能力,更不能对多种访问设备做出响应。传统网页设计的弊端:在移动互联网时代,传统的网页设计不适合多屏幕时代。响应式网页设计应运而生。响应式网页设计是一种设计网页的思想/方法。响应:指让我们的网页能够自动查询用户的访问设备
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-04-02 11:24
  • 阅读:129
  • css中rel的属性值都有哪些,分别代表什么意思
  • css中rel的属性值都有哪些,分别代表什么意思
  • 在HTML中,元素的rel属性用于定义当前文档与被链接文档之间的关系。这个属性在CSS的上下文中经常与样式表关联,但rel属性的用途远不止于此。以下是一些常见的rel属性值及其意义:1、stylesheet:表示被链接的文档是一个样式表。这通常用于链接CSS文件。
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-03-28 12:28
  • 阅读:263
联系客服
源码代售 源码咨询 素材咨询 联系客服
029-84538663
手机版

扫一扫进手机版
返回顶部