一、播放音频
1、 Android中播放音频文件一般都是使用MediaPlayer类来实现的,MediaPlayer类中一些较为常用的控制方法如下:
2、使用MediaPlayer的步骤
1)创建一个MediaPlayer对象
2)调用setDataSource()方法来设置音频文件路径
3)调用prepare()方法使MediaPlayer进入到准备状态
4)调用start()方法播放音频,pause()方法暂停播放,reset()方法停止播放
二、播放视频
1、 步骤:
1)在界面布局文件中定义VideoView组件,或在程序中创建VideoView组件
2)调用 setVideoPath: 来加载指定的视频
3)调用VideoView的start()、stop()、pause()方法来控制视频的播放
1、主界面
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="#多媒体的运用#"
android:textSize="25dp"
android:textColor="#000"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="100dp" />
<Button
android:id="@+id/btn_media"
android:text="音频播放"
android:textSize="25dp"
android:background="@drawable/corner"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="80dp" />
<Button
android:id="@+id/btn_video"
android:text="视频播放"
android:textSize="25dp"
android:background="@drawable/corner"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="80dp" />
LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Button btn_media,btn_video;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
btn_media = (Button) findViewById(R.id.btn_media);
btn_media.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClass(MainActivity.this,MediaPlayerActivity.class);
startActivity(intent);
}
});
btn_video = (Button) findViewById(R.id.btn_video);
btn_video.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClass(MainActivity.this,VideoActivity.class);
startActivity(intent);
}
});
}
}
主界面主要就是设置了按钮进行跳转,没什么说的
2、播放音频页面
布局 activity_media_player.xml
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
MediaPlayerActivity.java
public class MediaPlayerActivity extends AppCompatActivity implements View.OnClickListener{
private Button media_play,media_pause,media_stop;
private MediaPlayer mediaPlayer = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media_player);
initView();
//自我检查,写入SD卡权限是否在系统已允许的权限库里
if (ContextCompat.checkSelfPermission(MediaPlayerActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
//若不在系统已允许的权限库里,则弹出权限申请窗口。requestPermissions方法中的整数参数是为了有多个权限时加以区分
ActivityCompat.requestPermissions(MediaPlayerActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
}else{
initMediaPlayer(); //初始化MediaPlayer
}
}
private void initMediaPlayer() {
try{
File file = new File(Environment.getExternalStorageDirectory(),"music.mp3");
// ediaPlayer的setDataSource一共四个方法:
// setDataSource (String path)
// setDataSource (FileDescriptor fd)
// setDataSource (Context context, Uri uri)
// setDataSource (FileDescriptor fd, long offset, long length)
mediaPlayer.setDataSource(file.getPath()); //指定音频文件的路径
mediaPlayer.prepare(); //让MediaPlayer进入到准备状态
}catch (Exception e){
e.printStackTrace();
}
}
@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();
}else{
//决绝授予权限
Toast.makeText(this, "拒绝权限将无法使用程序", Toast.LENGTH_SHORT).show();
finish(); //关闭程序
}
}
}
private void initView() {
media_play = (Button) findViewById(R.id.media_play);
media_play.setOnClickListener(this);
media_pause = (Button) findViewById(R.id.media_pause);
media_pause.setOnClickListener(this);
media_stop = (Button) findViewById(R.id.media_stop);
media_stop.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.media_play:
if(!mediaPlayer.isPlaying()) {
mediaPlayer.start(); //开始播放
}break;
case R.id.media_pause:
if (mediaPlayer.isPlaying()){
mediaPlayer.pause();//暂停播放
}break;
case R.id.media_stop:
if(mediaPlayer.isPlaying()){
mediaPlayer.reset(); //停止播放
}break;
default:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null){
mediaPlayer.stop(); //停止播放
mediaPlayer.release(); //释放资源
}
}
}
3、播放视频
布局 activity_video_view.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="400dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/video_start"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="50dp"
android:textSize="30dp"
android:gravity="center"
android:text="开始"/>
<Button
android:id="@+id/video_pause"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="50dp"
android:textSize="30dp"
android:gravity="center"
android:text="暂停"/>
<Button
android:id="@+id/video_replay"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="50dp"
android:textSize="30dp"
android:gravity="center"
android:text="重播"/>
LinearLayout>
LinearLayout>
VideoActivity.java
public class VideoActivity extends AppCompatActivity implements View.OnClickListener{
//视频
private VideoView video_view;
//开始 停止 暂停
private Button video_start,video_pause,video_replay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_view);
initView();
if (ContextCompat.checkSelfPermission(VideoActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(VideoActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
}else {
initVideoPath();//初始化MediaPlayer
}
}
private void initVideoPath() {
File file = new File(Environment.getExternalStorageDirectory(),"movie.mp4");
video_view.setVideoPath(file.getPath()); //指定视频文件的路径
}
//初始化view
private void initView() {
video_view = (VideoView) findViewById(R.id.video_view);
video_start = (Button) findViewById(R.id.video_start);
video_start.setOnClickListener(this);
video_pause = (Button) findViewById(R.id.video_pause);
video_pause.setOnClickListener(this);
video_replay = (Button) findViewById(R.id.video_replay);
video_replay.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.video_start:
video_view.start(); //开始播放
break;
case R.id.video_pause:
video_view.pause(); //暂停播放
break;
case R.id.video_replay:
video_view.resume(); //重新播放
break;
}
}
@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){
initVideoPath();
}else{
Toast.makeText(this, "拒绝权限将无法使用程序", Toast.LENGTH_SHORT).show();
finish();
}break;
default:
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (video_view !=null){
video_view.suspend();
}
}
}
4、别忘了在AndroidManifest.xml清单文件中声明权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
OKlalalalalala~~~~~~
业精于勤荒于嬉,行成于思毁于随