Android笔记:视屏播放、VideoView、surfaceView,简易视频播放

简介:

一、VideoView方法

1.activity_video.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     xmlns:tools= "http://schemas.android.com/tools"
     android:layout_width= "match_parent"
     android:layout_height= "match_parent"
     tools:context= ".VideoActivity"  >
 
     <VideoView
         android:id= "@+id/video_videoView"
         android:layout_width= "fill_parent"
         android:layout_height= "fill_parent"
         android:layout_alignParentLeft= "true"
         android:layout_alignParentTop= "true"  />
 
</RelativeLayout>


2.代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package  com.example.vediotest;
 
import  android.media.MediaPlayer;
import  android.net.Uri;
import  android.os.Bundle;
import  android.app.Activity;
import  android.content.pm.ActivityInfo;
import  android.view.Menu;
import  android.view.Window;
import  android.view.WindowManager;
import  android.widget.MediaController;
import  android.widget.VideoView;
 
public  class  VideoActivity  extends  Activity
{
     
     private  VideoView videoView;
     private  Uri mUri;
     private  int  mPositionWhenPaused;
     
     @Override
     protected  void  onCreate(Bundle savedInstanceState)
     {
         super .onCreate(savedInstanceState);
         
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 设置成全屏模式
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // 强制为横屏
         setContentView(R.layout.activity_video);
         
         String url =  "http://videofile.xxxx.cn/Upload/Video/File/20140411/201404110228168972.mp4" ;
         // String url =
         // "http://player.youku.com/player.php/sid/XNDYwOTEzNzQ4/v.swf";
         
         mUri = Uri.parse(url);
         
         videoView = (VideoView) findViewById(R.id.video_videoView);
         MediaController mediaController =  new  MediaController( this );
         videoView.setMediaController(mediaController);
         
         // videoView.setVideoPath("/sdcard/xyx.3gp");
         // videoView.setVideoURI(mUri);
         // videoView.requestFocus();
         // videoView.start();
         
     }
     
     public  void  onStart()
     {
         // Play Video
         videoView.setVideoURI(mUri);
         videoView.start();
         
         super .onStart();
     }
     
     public  void  onPause()
     {
         // Stop video when the activity is pause.
         mPositionWhenPaused = videoView.getCurrentPosition();
         videoView.stopPlayback();
         
         super .onPause();
     }
     
     public  void  onResume()
     {
         // Resume video player
         if  (mPositionWhenPaused >=  0 )
         {
             videoView.seekTo(mPositionWhenPaused);
             mPositionWhenPaused = - 1 ;
         }
         
         super .onResume();
     }
     
     public  boolean  onError(MediaPlayer player,  int  arg1,  int  arg2)
     {
         return  false ;
     }
     
     public  void  onCompletion(MediaPlayer mp)
     {
         this .finish();
     }
}



二、surfaceView方法

(一)

1.activity_video_surface.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:layout_width= "fill_parent"
     android:layout_height= "fill_parent"
     android:orientation= "vertical"  >
 
     <TextView
         android:layout_width= "fill_parent"
         android:layout_height= "wrap_content"
         android:text= "标题"  />
 
     <SurfaceView
         android:id= "@+id/surfaceVideo_surfaceView"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:layout_gravity= "center"  >
     </SurfaceView>
 
</LinearLayout>


2.代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package  com.example.vediotest;
 
import  android.media.AudioManager;
import  android.media.MediaPlayer;
import  android.media.MediaPlayer.OnBufferingUpdateListener;
import  android.media.MediaPlayer.OnCompletionListener;
import  android.media.MediaPlayer.OnPreparedListener;
import  android.net.Uri;
import  android.os.Bundle;
import  android.app.Activity;
import  android.util.Log;
import  android.view.Menu;
import  android.view.SurfaceHolder;
import  android.view.SurfaceHolder.Callback;
import  android.view.SurfaceView;
 
public  class  SurfaceVideoActivity  extends  Activity  implements  Callback, OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener
{
     
     private  int  width =  0 ;
     private  int  height =  0 ;
     private  MediaPlayer mMediaPlayer =  null ;
     private  SurfaceView mSurfaceView =  null ;
     private  SurfaceHolder holder =  null ;
     private  String path =  "" ;
     
     @Override
     protected  void  onCreate(Bundle savedInstanceState)
     {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_video_surface);
         
         mSurfaceView = (SurfaceView)  this .findViewById(R.id.surfaceVideo_surfaceView);
         holder = mSurfaceView.getHolder();
         holder.addCallback( this );
         holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // 设置风格
         
     }
     
     public  void  playVedio()
     {
         try
         {
             path = android.os.Environment.getExternalStorageDirectory() +  "/moto_0012.3gp" ;
             mMediaPlayer =  new  MediaPlayer();
             // mMediaPlayer.setDataSource(path);
             
           String url =  "http://videofile.housebox.cn/Upload/Video/File/20140411/201404110228168972.mp4" ;
//            String url = "http://player.youku.com/player.php/sid/XNDYwOTEzNzQ4/v.swf";
             
             mMediaPlayer.setDataSource( this , Uri.parse(url));
             mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
             mMediaPlayer.setDisplay(holder);
             mMediaPlayer.prepare(); // 准备
             Log.e( "TAG-Duration" , mMediaPlayer.getDuration() +  "" );
             mMediaPlayer.setOnBufferingUpdateListener( this );
             mMediaPlayer.setOnCompletionListener( this );
             mMediaPlayer.setOnPreparedListener( this );
         }
         catch  (Exception ex)
         {
             
         }
     }
     
     public  void  onBufferingUpdate(MediaPlayer mp,  int  percent)
     {
         // TODO Auto-generated method stub
         Log.i( "TAG-onBufferingUpdate" , percent +  "|"  + mMediaPlayer.getCurrentPosition());
     }
     
     public  void  onCompletion(MediaPlayer mp)
     {
         // TODO Auto-generated method stub
         Log.i( "TAG-onCompletion" "Completion" );
     }
     
     public  void  onPrepared(MediaPlayer mp)
     {
         // TODO Auto-generated method stub
         width = mMediaPlayer.getVideoWidth();
         height = mMediaPlayer.getVideoHeight();
         if  (width !=  0  && height !=  0 )
         {
             holder.setFixedSize(width, height); // 设置视频高宽
             mMediaPlayer.start();
             Log.i( "TAG-Duration2" , mMediaPlayer.getDuration() +  "" );
         }
     }
     
     public  void  surfaceChanged(SurfaceHolder holder,  int  format,  int  width,  int  height)
     {
         // TODO Auto-generated method stub
         
     }
     
     public  void  surfaceCreated(SurfaceHolder holder)
     {
         // TODO Auto-generated method stub
         playVedio();
     }
     
     public  void  surfaceDestroyed(SurfaceHolder holder)
     {
         // TODO Auto-generated method stub
         Log.i( "TAG-surfaceDestroyed" "surfaceDestroyed" );
     }
     
     @Override
     protected  void  onPause()
     {
         super .onPause();
         if  (mMediaPlayer !=  null )
         {
             if  (mMediaPlayer.isPlaying())
             {
                 mMediaPlayer.stop();
             }
             mMediaPlayer.reset();
             mMediaPlayer.release();
             mMediaPlayer =  null ;
         }
     }
     
}


(二)

  1. activity_video_surface2.xml

1
2
3
4
5
6
7
8
9
10
11
12
<RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:layout_width= "fill_parent"
     android:layout_height= "fill_parent"  >
 
     <SurfaceView
         android:id= "@+id/surface2_surfaceView"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:layout_gravity= "center"  >
     </SurfaceView>
 
</RelativeLayout>


2.代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package  com.example.vediotest;
 
import  java.io.IOException;
 
import  android.media.AudioManager;
import  android.media.MediaPlayer;
import  android.media.MediaPlayer.OnBufferingUpdateListener;
import  android.media.MediaPlayer.OnCompletionListener;
import  android.net.Uri;
import  android.os.Bundle;
import  android.app.Activity;
import  android.content.pm.ActivityInfo;
import  android.util.Log;
import  android.view.Menu;
import  android.view.SurfaceHolder;
import  android.view.SurfaceView;
import  android.view.Window;
 
public  class  SurfaceVideo2Activity  extends  Activity  implements  OnBufferingUpdateListener, OnCompletionListener, MediaPlayer.OnPreparedListener,
         SurfaceHolder.Callback
{
     private  MediaPlayer mediaPlayer;
     private  SurfaceView surfaceView;
     private  SurfaceHolder surfaceHolder;
     private  int  videoWidth;
     private  int  videoHeight;
     
     @Override
     protected  void  onCreate(Bundle savedInstanceState)
     {
         super .onCreate(savedInstanceState);
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         setContentView(R.layout.activity_video_surface2);
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // 强制为横屏
         this .surfaceView = (SurfaceView)  this .findViewById(R.id.surface2_surfaceView);
         this .surfaceHolder =  this .surfaceView.getHolder();
         this .surfaceHolder.addCallback( this );
         this .surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
         Log.v( "cat" ">>>create ok." );
         
     }
     
     private  void  playVideo()  throws  IllegalArgumentException, IllegalStateException, IOException
     {
       String url =  "http://videofile.housebox.cn/Upload/Video/File/20140411/201404110228168972.mp4" ;
//        String url = "http://player.youku.com/player.php/sid/XNDYwOTEzNzQ4/v.swf";
         
         
         this .mediaPlayer =  new  MediaPlayer();
//        this.mediaPlayer.setDataSource("/sdcard/daoxiang.3gp");
         this .mediaPlayer.setDataSource( this , Uri.parse(url));
         
         this .mediaPlayer.setDisplay( this .surfaceHolder);
         this .mediaPlayer.prepare();
         this .mediaPlayer.setOnBufferingUpdateListener( this );
         this .mediaPlayer.setOnPreparedListener( this );
         this .mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
         Log.i( "mplayer" ">>>play video" );
     }
     
     @Override
     public  void  surfaceChanged(SurfaceHolder arg0,  int  arg1,  int  arg2,  int  arg3)
     {
         Log.i( "cat" ">>>surface changed" );
     }
     
     @Override
     public  void  surfaceCreated(SurfaceHolder holder)
     {
         try
         {
             this .playVideo();
         }
         catch  (Exception e)
         {
             Log.i( "cat" ">>>error" , e);
         }
         Log.i( "cat" ">>>surface created" );
     }
     
     @Override
     public  void  surfaceDestroyed(SurfaceHolder holder)
     {
         Log.v( "mplayer" ">>>surface destroyed" );
     }
     
     @Override
     public  void  onCompletion(MediaPlayer arg0)
     {
         // TODO Auto-generated method stub
     }
     
     @Override
     public  void  onBufferingUpdate(MediaPlayer mp,  int  percent)
     {
         // TODO Auto-generated method stub
     }
     
     @Override
     public  void  onPrepared(MediaPlayer arg0)
     {
         this .videoWidth =  this .mediaPlayer.getVideoWidth();
         this .videoHeight =  this .mediaPlayer.getVideoHeight();
         if  ( this .videoHeight !=  0  &&  this .videoWidth !=  0 )
         {
             this .surfaceHolder.setFixedSize( this .videoWidth,  this .videoHeight);
             this .mediaPlayer.start();
         }
     }
     
     @Override
     protected  void  onDestroy()
     {
         super .onDestroy();
         if  ( this .mediaPlayer !=  null )
         {
             this .mediaPlayer.release();
             this .mediaPlayer =  null ;
         }
     }
     
}




本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1414232,如需转载请自行联系原作者

目录
相关文章
|
9天前
|
Linux 编译器 Android开发
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
在Linux环境下,本文指导如何交叉编译x265的so库以适应Android。首先,需安装cmake和下载android-ndk-r21e。接着,下载x265源码,修改crosscompile.cmake的编译器设置。配置x265源码,使用指定的NDK路径,并在配置界面修改相关选项。随后,修改编译规则,编译并安装x265,调整pc描述文件并更新PKG_CONFIG_PATH。最后,修改FFmpeg配置脚本启用x265支持,编译安装FFmpeg,将生成的so文件导入Android工程,调整gradle配置以确保顺利运行。
32 1
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
|
10天前
|
Unix Linux Shell
FFmpeg开发笔记(八)Linux交叉编译Android的FFmpeg库
在Linux环境下交叉编译Android所需的FFmpeg so库,首先下载`android-ndk-r21e`,然后解压。接着,上传FFmpeg及相关库(如x264、freetype、lame)源码,修改相关sh文件,将`SYSTEM=windows-x86_64`改为`SYSTEM=linux-x86_64`并删除回车符。对x264的configure文件进行修改,然后编译x264。同样编译其他第三方库。设置环境变量`PKG_CONFIG_PATH`,最后在FFmpeg源码目录执行配置、编译和安装命令,生成的so文件复制到App工程指定目录。
43 9
FFmpeg开发笔记(八)Linux交叉编译Android的FFmpeg库
|
8月前
|
编解码 Android开发 数据安全/隐私保护
Android平台外部编码数据(H264/H265/AAC/PCMA/PCMU)实时预览播放技术实现
好多开发者可能疑惑,外部数据实时预览播放,到底有什么用? 是的,一般场景是用不到的,我们在开发这块前几年已经开发了非常稳定的RTMP、RTSP直播播放模块,不过也遇到这样的场景,部分设备输出编码后(视频:H.264/H.265,音频:AAC/PCMA/PCMU)的数据,比如无人机或部分智能硬件设备,回调出来的H.264/H.265数据,除了想转推到RTMP、轻量级RTSP服务或GB28181外,还需要本地预览甚至对数据做二次处理(视频分析、实时水印字符叠加等,然后二次编码),基于这样的场景诉求,我们开发了Android平台外部编码数据实时预览播放模块。
|
5月前
|
XML Java Android开发
Android Studio App开发中使用录音机、MediaRecorder录制音频和MediaPlayer播放音频讲解及实战(附源码)
Android Studio App开发中使用录音机、MediaRecorder录制音频和MediaPlayer播放音频讲解及实战(附源码)
78 0
|
8月前
|
前端开发 开发工具 Android开发
Android播放器之SurfaceView与GLSurfaceView
Surface的官方介绍:Handle onto a raw buffer that is being managed by the screen compositor,Surface是一个raw buffer的句柄,通过它在raw buffer上进行绘制,可以通过Surface获得一个Canvas。
120 0
|
7天前
|
Android开发 内存技术
Android 通过tinyalsa调试解决录制和播放音频问题
Android 通过tinyalsa调试解决录制和播放音频问题
24 1
|
7天前
|
存储 Java API
Android系统 文件访问权限笔记
Android系统 文件访问权限笔记
41 1
|
8月前
|
编解码 开发工具 Android开发
安卓端/iOS端如何播放4K分辨率的RTMP/RTSP流
4K分辨率即4096×2160的像素分辨率,它是2K投影机和高清电视分辨率的4倍,属于超高清分辨率。在此分辨率下,观众将可以看清画面中的每一个细节,每一个特写。影院如果采用惊人的4096×2160像素,无论在影院的哪个位置,观众都可以清楚的看到画面的每一个细节,影片色彩鲜艳、文字清晰锐丽,再配合超真实音效,这种感觉真的是一种难以言传的享受。
250 0
安卓端/iOS端如何播放4K分辨率的RTMP/RTSP流
|
5月前
|
XML Java 调度
Android开发音效增强中铃声播放Ringtone及声音池调度SoundPool的讲解及实战(超详细 附源码)
Android开发音效增强中铃声播放Ringtone及声音池调度SoundPool的讲解及实战(超详细 附源码)
61 0
|
5月前
|
XML Java Android开发
Android App开发实战项目之仿手机QQ动感影集动画播放(附源码和演示视频 可直接使用)
Android App开发实战项目之仿手机QQ动感影集动画播放(附源码和演示视频 可直接使用)
29 0