示例代码
public class MediaPlayActivity extends AppCompatActivity implements View.OnClickListener { private MediaPlayer mediaplay = new MediaPlayer(); private VideoView videoView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_media_play); initView(); } private void initView() { findViewById(R.id.start).setOnClickListener(this); findViewById(R.id.pause).setOnClickListener(this); findViewById(R.id.stop).setOnClickListener(this); findViewById(R.id.vd_start).setOnClickListener(this); findViewById(R.id.vd_pause).setOnClickListener(this); findViewById(R.id.vd_replay).setOnClickListener(this); videoView = ((VideoView) findViewById(R.id.vd_show)); // 运行时权限判断 if (ContextCompat.checkSelfPermission(MediaPlayActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MediaPlayActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); } else { initMediaPlayer(); initVidoPlay(); } } /** * 初始化视频播放参数 */ private void initVidoPlay() { File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera", "20170223_113151.mp4"); videoView.setVideoPath(file.getPath()); } /** * 初始化音频播放参数 */ private void initMediaPlayer() { try { File file = new File(Environment.getExternalStorageDirectory() + "/msc/", "tts.wav"); mediaplay.setDataSource(file.getPath());//指定音频文件的路径 mediaplay.prepare();//进入准备状态 } catch (IOException e) { e.printStackTrace(); } } /** * 运行时权限的管理 * * @param requestCode * @param permissions * @param grantResults */ @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case 1: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { initMediaPlayer(); initVidoPlay(); } else { Toast.makeText(this, "没有权限啦~~", Toast.LENGTH_SHORT).show(); } break; } } @Override public void onClick(View v) { switch (v.getId()) { // 音频 case R.id.start: if (!mediaplay.isPlaying()) { mediaplay.start(); } break; case R.id.pause: if (mediaplay.isPlaying()) { mediaplay.pause(); } break; case R.id.stop: if (mediaplay.isPlaying()) { mediaplay.reset(); initMediaPlayer(); } break; // 视频 case R.id.vd_start: if (!videoView.isPlaying()) { videoView.start(); } break; case R.id.vd_pause: if (videoView.isPlaying()) { videoView.pause(); } break; case R.id.vd_replay: if (videoView.isPlaying()) { videoView.resume(); } break; } } /** * 记得在这里释放资源 */ @Override protected void onDestroy() { super.onDestroy(); if (null != mediaplay) { mediaplay.stop(); mediaplay.release(); } if (null != videoView) { videoView.suspend(); } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_media_play" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.lvyequeen.test.day05.mediaplay.MediaPlayActivity"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start" /> <Button android:id="@+id/pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="pause" /> <Button android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="stop" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:background="@color/colorPrimary" android:gravity="center_horizontal" android:text="视频播放" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center_horizontal" android:orientation="horizontal"> <Button android:id="@+id/vd_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start" /> <Button android:id="@+id/vd_pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="pause" /> <Button android:id="@+id/vd_replay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="replay" /> </LinearLayout> <VideoView android:id="@+id/vd_show" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>