源码教程 2025年06月7日
0 收藏 0 点赞 247 浏览 8022 个字
摘要 :

首先我们来看一下 Android N 支持的视频文件有哪些。Android N 支持的视频格式下表所示。 Android N 支持的视频文件 视频播放器 与音频播放相比,视频播放需……

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

Android N 支持的视频文件

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

视频播放器

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

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

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

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

实例 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 所示。

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

图 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. }

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/1017.html

管理员

相关推荐
2025-07-05

对于一些使用WordPress进行外贸建站的商家来说,大部分人会通过在WordPress中添加JS代码和CSS样式表…

700
2025-07-05

当商家遇到WordPress独立站改版或者域名到期等问题时,不免会涉及到WordPress域名的更改,那么这一…

714
2025-07-05

用户在使用WooCommerce进行跨境电商独立站搭建工作时,有可能会借助WooCommerce短代码实现更加醒目…

306
2025-07-05

随着外贸建站行业的不断深入发展,WordPress的多语言功能也显得越来越重要,一个具有多语言的独立站…

1,038
2025-07-05

WooCommerce作为WordPress外贸建站生态的一部分,WooCommerce运费设置是商家在建站过程中不可忽视的…

834
2025-07-05

在外贸建站的过程中,有些商家会选择使用WordPress幻灯片为网站页面增加一定的动感和可观性,进而提…

721
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号