Android 播放视频文件
1 package com.example.myvideo2; 2 3 import java.io.File; 4 5 import android.app.Activity; 6 import android.net.Uri; 7 import android.os.Bundle; 8 import android.os.Environment; 9 import android.view.Menu; 10 import android.view.MenuItem; 11 import android.view.View; 12 import android.widget.Button; 13 import android.widget.MediaController; 14 import android.widget.MediaController.MediaPlayerControl; 15 import android.widget.VideoView; 16 /** 17 * Android 播放视频 18 * @Describe: 19 * @package: com.example.myvideo2 20 * @author shaobn 21 * @date 2015-9-14 上午10:57:41 22 */ 23 public class MainActivity extends Activity implements MediaPlayerControl { 24 private VideoView videoView; 25 private MediaController mediaController; 26 private Button button; 27 @Override 28 protected void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 setContentView(R.layout.activity_main); 31 mediaController = new MediaController(MainActivity.this); 32 videoView = (VideoView) this.findViewById(R.id.videoView1); 33 button = (Button) this.findViewById(R.id.button1); 34 videoView.setMediaController(mediaController); 35 button.setOnClickListener(new View.OnClickListener() { 36 37 @Override 38 public void onClick(View arg0) { 39 // TODO Auto-generated method stub 40 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ 41 File file1 = Environment.getExternalStorageDirectory(); 42 File file2 = new File(file1,"/Movies/Video.mp4"); 43 Uri uri = Uri.fromFile(file2); 44 videoView.setVideoURI(uri); 45 } 46 } 47 }); 48 49 } 50 @Override 51 public boolean canPause() { 52 // TODO Auto-generated method stub 53 return videoView.canPause(); 54 } 55 @Override 56 public boolean canSeekBackward() { 57 // TODO Auto-generated method stub 58 return videoView.canSeekBackward(); 59 } 60 @Override 61 public boolean canSeekForward() { 62 // TODO Auto-generated method stub 63 return videoView.canSeekForward(); 64 } 65 @Override 66 public int getAudioSessionId() { 67 // TODO Auto-generated method stub 68 return 0; 69 } 70 @Override 71 public int getBufferPercentage() { 72 // TODO Auto-generated method stub 73 return 0; 74 } 75 @Override 76 public int getCurrentPosition() { 77 // TODO Auto-generated method stub 78 return 0; 79 } 80 @Override 81 public int getDuration() { 82 // TODO Auto-generated method stub 83 return 0; 84 } 85 @Override 86 public boolean isPlaying() { 87 // TODO Auto-generated method stub 88 return videoView.isPlaying(); 89 } 90 @Override 91 public void pause() { 92 // TODO Auto-generated method stub 93 if (videoView.isPlaying()) { 94 videoView.pause(); 95 } 96 } 97 @Override 98 public void seekTo(int arg0) { 99 // TODO Auto-generated method stub 100 videoView.seekTo(arg0); 101 } 102 @Override 103 public void start() { 104 // TODO Auto-generated method stub 105 videoView.start(); 106 } 107 108 109 }
实现播放视频文件。
吾宁做一叶扁舟,始航于湖边,遨游于海上,浪迹于江中。