1.播放音频
使用MediaPlayer类来实现,该类对多种格式的音频提供了非常全面的控制,常用的控制方法有以下这些,图片来自《第一行代码2》
- 以下是实现一个简单的播放、暂停功能的代码
注:实现该功能前,需先声明读取sdcard的权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.anwser_mac.playaudiotest">
<!--声明读取sdcard的权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- activity的代码如下
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
Button play = (Button) findViewById(R.id.play);
Button pause = (Button) findViewById(R.id.pause);
Button stop = (Button) findViewById(R.id.stop);
//播放按钮的点击事件
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
}
});
//暂停按钮的点击事件
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
}
});
//停止按钮的点击事件
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.reset();//停止播放
initMediaPlayer();
}
}
});
//判断是否已授权
if (ContextCompat.checkSelfPermission(MainActivity.this, WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//未授权
//发起授权请求ActivityCompat.requestPermissions(MainActivity.this, new String[] { WRITE_EXTERNAL_STORAGE }, 1);
} else {
initMediaPlayer();//初始化MediaPlayer
}
}
private void initMediaPlayer() {
try {
//播放sdcard根目录下的music.mp3
File file = new File(Environment.getExternalStorageDirectory(), "music.mp3");
mediaPlayer.setDataSource(file.getPath());
mediaPlayer.prepare();
} 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();
}
break;
default:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
}
}
- 界面如下
2.播放视频文件
播放视频跟播放音频类似,跟播放音频不同的是,播放视频需在布局文件中加入VideoView,播放视频使用的是VideoView类来实现,以下是以下常用的控制方法,图片来自《第一行代码第二版》
-
以下是实现一个简单的播放暂停视频功能的代码
和播放音频一样需要在Manifest文件中声明读取sd卡的权限。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.anwser_mac.playaudiotest">
<!--声明读取sdcard的权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- activity中的代码如下
public class MainActivity extends AppCompatActivity {
private VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
videoView = (VideoView) findViewById(R.id.video_view);
Button play = (Button) findViewById(R.id.play);
Button pause = (Button) findViewById(R.id.pause);
Button replay = (Button) findViewById(R.id.replay);
//点击播放
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!videoView.isPlaying()) {
videoView.start();
}
}
});
//点击暂停
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (videoView.isPlaying()) {
videoView.pause();
}
}
});
//点击重播
replay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (videoView.isPlaying()) {
videoView.resume();
}
}
});
//授权申请
if (ContextCompat.checkSelfPermission(MainActivity.this, WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[] { WRITE_EXTERNAL_STORAGE }, 1);
} else {
initVideoPath();//初始化MediaPlayer
}
}
//初始化MediaPlayer
private void initVideoPath() {
File file = new File(Environment.getExternalStorageDirectory(), "movie.mp4");
videoView.setVideoPath(file.getPath());
}
//授权请求结束时调用
@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);
}
}
}
}