查看: 469|回复: 0

[Android教程] Android多媒体教程之播放视频的四种方法

发表于 2017-11-28 08:00:01

本文主要给大家介绍的是关于Android播放视频的四种方法,分享出来供大家参考学习,下面来一起看看详细的介绍:

一、通过intent的方式,调用系统自带的播放器

  1. Uri uri = Uri.parse("/storage/emulated/0/DCIM/Camera/20170521_200117.mp4");
  2. //调用系统自带的播放器
  3. Intent intent = new Intent(Intent.ACTION_VIEW);
  4. intent.setDataAndType(uri, "/storage/emulated/0/DCIM/Camera/20170521_200117.mp4");
  5. startActivity(intent);
复制代码

二、使用VideoView

布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:id="@+id/activity_video_play_by_vv"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent">
  8. <VideoView
  9. android:id="@+id/video_view"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"/>
  12. </RelativeLayout>
复制代码

Activity

  1. public class VideoPlayByVVActivity extends AppCompatActivity {
  2. private VideoView mVideoView;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. // requestWindowFeature(Window.FEATURE_NO_TITLE); //去掉 title
  7. // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //设置全屏
  8. // getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); //设置屏幕常亮
  9. setContentView(R.layout.activity_video_play_by_vv);
  10. mVideoView = (VideoView) findViewById(R.id.video_view);
  11. init();
  12. }
  13. private void init() {
  14. String path = "/storage/emulated/0/DCIM/Camera/20170521_200117.mp4";
  15. Uri uri = Uri.parse(path);
  16. mVideoView.setVideoPath(path);
  17. mVideoView.start();
  18. mVideoView.requestFocus();
  19. }
  20. }
复制代码

三、MediaPlayer + SurfaceView

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:id="@+id/activity_video_play_by_sur"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent">
  8. <SurfaceView
  9. android:id="@+id/surface_view"
  10. android:layout_width="180dp"
  11. android:layout_height="wrap_content"/>
  12. <LinearLayout
  13. android:layout_alignParentBottom="true"
  14. android:layout_width="match_parent"
  15. android:layout_height="40dp">
  16. <Button
  17. android:id="@+id/stop"
  18. android:text="stop"
  19. android:layout_weight="1"
  20. android:layout_width="0dp"
  21. android:layout_height="match_parent"/>
  22. <Button
  23. android:id="@+id/play"
  24. android:text="play"
  25. android:layout_weight="1"
  26. android:layout_width="0dp"
  27. android:layout_height="match_parent"/>
  28. <Button
  29. android:id="@+id/pasue"
  30. android:text="pasue"
  31. android:layout_weight="1"
  32. android:layout_width="0dp"
  33. android:layout_height="match_parent"/>
  34. </LinearLayout>
  35. </RelativeLayout>
复制代码

Activity

  1. public class VideoPlayBySurActivity extends AppCompatActivity implements View.OnClickListener {
  2. private SurfaceView mSurfaceView;
  3. private MediaPlayer mMediaPlayer;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_video_play_by_sur);
  8. mSurfaceView = (SurfaceView) findViewById(R.id.surface_view);
  9. findViewById(R.id.stop).setOnClickListener(this);
  10. findViewById(R.id.pasue).setOnClickListener(this);
  11. findViewById(R.id.play).setOnClickListener(this);
  12. init();
  13. }
  14. private void init() {
  15. mMediaPlayer = new MediaPlayer();
  16. mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
  17. @Override
  18. public void surfaceCreated(SurfaceHolder holder) {
  19. play();
  20. }
  21. @Override
  22. public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  23. }
  24. @Override
  25. public void surfaceDestroyed(SurfaceHolder holder) {
  26. }
  27. });
  28. }
  29. @Override
  30. public void onClick(View v) {
  31. switch (v.getId()){
  32. case R.id.stop:
  33. stop();
  34. break;
  35. case R.id.play:
  36. if(!mMediaPlayer.isPlaying()){
  37. play();
  38. }
  39. break;
  40. case R.id.pasue:
  41. pasue();
  42. break;
  43. }
  44. }
  45. public void stop(){
  46. if(mMediaPlayer.isPlaying()){
  47. mMediaPlayer.stop();
  48. }
  49. }
  50. public void pasue(){
  51. if(mMediaPlayer.isPlaying()){
  52. mMediaPlayer.pause();
  53. }else{
  54. mMediaPlayer.start();
  55. }
  56. }
  57. public void play(){
  58. String path = "/storage/emulated/0/DCIM/Camera/20170521_200117.mp4";
  59. try {
  60. mMediaPlayer.reset();
  61. mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  62. //设置需要播放的视频
  63. mMediaPlayer.setDataSource(this, Uri.parse(path));
  64. //把视频画面输出到SurfaceView
  65. mMediaPlayer.setDisplay(mSurfaceView.getHolder());
  66. mMediaPlayer.prepare();
  67. mMediaPlayer.start();
  68. } catch (IOException e) {
  69. e.printStackTrace();
  70. }
  71. }
  72. }
复制代码

四、 MediaPlayer + TextureView

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:id="@+id/activity_video_play_by_textrue_view"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent">
  8. <TextureView
  9. android:id="@+id/texture_view"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"/>
  12. <ImageView
  13. android:id="@+id/video_image"
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent"
  16. android:src="@drawable/all_darkbackground"/>
  17. </RelativeLayout>
复制代码

Activity

  1. public class VideoPlayByTextrueViewActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener, MediaPlayer.OnInfoListener, MediaPlayer.OnBufferingUpdateListener {
  2. private TextureView mTextureView;
  3. private ImageView mImageVideo;
  4. private Surface mSurface;
  5. private MediaPlayer mMediaPlayer;
  6. private static String path = "/storage/emulated/0/DCIM/Camera/20170521_200117.mp4";
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_video_play_by_textrue_view);
  11. mTextureView = (TextureView) findViewById(R.id.texture_view);
  12. mImageVideo = (ImageView) findViewById(R.id.video_image);
  13. init();
  14. }
  15. private void init() {
  16. mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
  17. @Override
  18. public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
  19. mSurface = new Surface(surfaceTexture);
  20. Log.e("tag", "---- onSurfaceTextureAvailable");
  21. play();
  22. }
  23. @Override
  24. public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
  25. Log.e("tag", "---- onSurfaceTextureSizeChanged");
  26. }
  27. @Override
  28. public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
  29. mTextureView=null;
  30. mSurface=null;
  31. mMediaPlayer.stop();
  32. mMediaPlayer.release();
  33. return false;
  34. }
  35. @Override
  36. public void onSurfaceTextureUpdated(SurfaceTexture surface) {
  37. }
  38. });
  39. }
  40. public void play(){
  41. mMediaPlayer = new MediaPlayer();
  42. try {
  43. mMediaPlayer.setDataSource(getApplicationContext(), Uri.parse(path));
  44. mMediaPlayer.setSurface(mSurface);
  45. mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  46. mMediaPlayer.setOnPreparedListener(this);
  47. mMediaPlayer.setOnInfoListener(this);
  48. mMediaPlayer.setOnBufferingUpdateListener(this);
  49. mMediaPlayer.prepareAsync();
  50. } catch (IOException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. @Override
  55. public void onPrepared(MediaPlayer mp) {
  56. mImageVideo.setVisibility(View.GONE);
  57. mMediaPlayer.start();
  58. }
  59. @Override
  60. public boolean onInfo(MediaPlayer mp, int what, int extra) {
  61. return false;
  62. }
  63. @Override
  64. public void onBufferingUpdate(MediaPlayer mp, int percent) {
  65. }
  66. }
复制代码

参考文章

Android中SurfaceView的使用详解

Android 5.0(Lollipop)中的SurfaceTexture,TextureView, SurfaceView和GLSurfaceView

Android TextureView简易教程

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对程序员之家的支持。



回复

使用道具 举报

关闭

站长推荐上一条 /1 下一条