package com.example.yanlei.myyk; import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.MediaController;
import android.widget.VideoView; import java.io.File; public class MainActivity extends AppCompatActivity { private VideoView videoView;
MediaController mediaco;
private Uri mUri; public static final String TAG = "VideoPlayer"; private int mPositionWhenPaused = -1; private MediaController mMediaController; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = (VideoView) findViewById(R.id.videoview);
//文件路径 mUri = Uri.parse(Environment.getExternalStorageDirectory() + "/videoviewdemo.mp4"); //Create media controller
mediaco = new MediaController(this);
//设置MediaController
videoView.setMediaController(mediaco);
} //监听MediaPlayer上报的错误信息 public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
return false;
} //Video播完的时候得到通知 public void onCompletion(MediaPlayer mp) {
this.finish();
} //开始
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();
Log.d(TAG, "OnStop: mPositionWhenPaused = " + mPositionWhenPaused);
Log.d(TAG, "OnStop: getDuration = " + videoView.getDuration()); super.onPause();
} public void onResume() {
// Resume video player
if (mPositionWhenPaused >= 0) {
videoView.seekTo(mPositionWhenPaused);
mPositionWhenPaused = -1;
} super.onResume();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId(); //noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} return super.onOptionsItemSelected(item);
}
}
权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
或者
package com.example.yanlei.myyk; import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.widget.MediaController;
import android.widget.VideoView; import java.io.File; public class MainActivity extends AppCompatActivity { private VideoView video1;
MediaController mediaco;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); video1=(VideoView)findViewById(R.id.videoview);
mediaco=new MediaController(this);
File file=new File(Environment.getExternalStorageDirectory() + "/videoviewdemo.mp4");
if(file.exists()){
//VideoView与MediaController进行关联
video1.setVideoPath(file.getAbsolutePath());
video1.setMediaController(mediaco);
mediaco.setMediaPlayer(video1);
//让VideiView获取焦点
video1.requestFocus();
} } }