在Android中,我们有三种方式来实现视频的播放:
1、使用其自带的播放器。指定Action为ACTION_VIEW,Data为Uri,Type为其MIME类型。
2、使用VideoView来播放。在布局文件中使用VideoView结合MediaController来实现对其控制。
3、使用MediaPlayer类和SurfaceView来实现,这种方式很灵活。
1、调用其自带的播放器:
1 | protected static final String SOURCE = "/mnt/sdb/sdb1/movies/test.mp4" Uri uri = Uri.formFile(new file(SOURCE) ); |
3 | Intent intent = new Intent(Intent.ACTION_VIEW); |
4 | Log.v( "URI:::::::::" , uri.toString()); |
5 | intent.setDataAndType(uri, "video/mp4" ); |
2、使用VideoView来实现:
1 | Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/Test_Movie.m4v" ); |
2 | VideoView videoView = (VideoView) this .findViewById(R.id.video_view); |
3 | videoView.setMediaController( new MediaController( this )); |
4 | videoView.setVideoURI(uri); |
6 | videoView.requestFocus(); |
3、使用MediaPlayer:
002 | import java.io.IOException; |
003 | import android.app.Activity; |
004 | import android.media.MediaPlayer; |
005 | import android.media.MediaPlayer.OnCompletionListener; |
006 | import android.media.MediaPlayer.OnErrorListener; |
007 | import android.media.MediaPlayer.OnInfoListener; |
008 | import android.media.MediaPlayer.OnPreparedListener; |
009 | import android.media.MediaPlayer.OnSeekCompleteListener; |
010 | import android.media.MediaPlayer.OnVideoSizeChangedListener; |
011 | import android.os.Bundle; |
012 | import android.os.Environment; |
013 | import android.util.Log; |
014 | import android.view.Display; |
015 | import android.view.SurfaceHolder; |
016 | import android.view.SurfaceView; |
017 | import android.widget.LinearLayout; |
019 | * 该实例中使用MediaPlayer完成播放,同时界面使用SurfaceView来实现 |
021 | * 这里我们实现MediaPlayer中很多状态变化时的监听器 |
023 | * 使用Mediaplayer时,也可以使用MediaController类,但是需要实现MediaController.mediaController接口 |
026 | * 然后,设置controller.setMediaPlayer(),setAnchorView(),setEnabled(),show()就可以了,这里不再实现 |
027 | * @author Administrator |
030 | public class VideoSurfaceDemo extends Activity implements OnCompletionListener,OnErrorListener,OnInfoListener, |
031 | OnPreparedListener, OnSeekCompleteListener,OnVideoSizeChangedListener,SurfaceHolder.Callback{ |
032 | private Display currDisplay; |
033 | private SurfaceView surfaceView; |
034 | private SurfaceHolder holder; |
035 | private MediaPlayer player; |
036 | private int vWidth,vHeight; |
039 | public void onCreate(Bundle savedInstanceState){ |
040 | super .onCreate(savedInstanceState); |
041 | this .setContentView(R.layout.video_surface); |
043 | surfaceView = (SurfaceView) this .findViewById(R.id.video_surface); |
045 | holder = surfaceView.getHolder(); |
046 | holder.addCallback( this ); |
048 | holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); |
051 | player = new MediaPlayer(); |
052 | player.setOnCompletionListener( this ); |
053 | player.setOnErrorListener( this ); |
054 | player.setOnInfoListener( this ); |
055 | player.setOnPreparedListener( this ); |
056 | player.setOnSeekCompleteListener( this ); |
057 | player.setOnVideoSizeChangedListener( this ); |
058 | Log.v( "Begin:::" , "surfaceDestroyed called" ); |
060 | String dataPath = Environment.getExternalStorageDirectory().getPath()+ "/Test_Movie.m4v" ; |
062 | player.setDataSource(dataPath); |
063 | Log.v( "Next:::" , "surfaceDestroyed called" ); |
064 | } catch (IllegalArgumentException e) { |
066 | } catch (IllegalStateException e) { |
068 | } catch (IOException e) { |
072 | currDisplay = this .getWindowManager().getDefaultDisplay(); |
076 | public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { |
078 | Log.v( "Surface Change:::" , "surfaceChanged called" ); |
081 | public void surfaceCreated(SurfaceHolder holder) { |
084 | player.setDisplay(holder); |
086 | player.prepareAsync(); |
090 | public void surfaceDestroyed(SurfaceHolder holder) { |
092 | Log.v( "Surface Destory:::" , "surfaceDestroyed called" ); |
095 | public void onVideoSizeChanged(MediaPlayer arg0, int arg1, int arg2) { |
098 | Log.v( "Video Size Change" , "onVideoSizeChanged called" ); |
102 | public void onSeekComplete(MediaPlayer arg0) { |
104 | Log.v( "Seek Completion" , "onSeekComplete called" ); |
108 | public void onPrepared(MediaPlayer player) { |
112 | vWidth = player.getVideoWidth(); |
113 | vHeight = player.getVideoHeight(); |
115 | if (vWidth > currDisplay.getWidth() || vHeight > currDisplay.getHeight()){ |
117 | float wRatio = ( float )vWidth/( float )currDisplay.getWidth(); |
118 | float hRatio = ( float )vHeight/( float )currDisplay.getHeight(); |
121 | float ratio = Math.max(wRatio, hRatio); |
123 | vWidth = ( int )Math.ceil(( float )vWidth/ratio); |
124 | vHeight = ( int )Math.ceil(( float )vHeight/ratio); |
127 | surfaceView.setLayoutParams( new LinearLayout.LayoutParams(vWidth, vHeight)); |
135 | public boolean onInfo(MediaPlayer player, int whatInfo, int extra) { |
138 | case MediaPlayer.MEDIA_INFO_BAD_INTERLEAVING: |
140 | case MediaPlayer.MEDIA_INFO_METADATA_UPDATE: |
142 | case MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING: |
144 | case MediaPlayer.MEDIA_INFO_NOT_SEEKABLE: |
150 | public boolean onError(MediaPlayer player, int whatError, int extra) { |
151 | Log.v( "Play Error:::" , "onError called" ); |
153 | case MediaPlayer.MEDIA_ERROR_SERVER_DIED: |
154 | Log.v( "Play Error:::" , "MEDIA_ERROR_SERVER_DIED" ); |
156 | case MediaPlayer.MEDIA_ERROR_UNKNOWN: |
157 | Log.v( "Play Error:::" , "MEDIA_ERROR_UNKNOWN" ); |
165 | public void onCompletion(MediaPlayer player) { |
167 | Log.v( "Play Over:::" , "onComletion called" ); |