一、播放音频
Android中播放音频一般使用MediaPlayer类来实现。
一般分为以下几个步骤:
1)创建一个MediaPlayer对象。
2)调用setDataSource()方法设置音频文件路径。
3)调用prepare()方法使MediaPlayer进入准备状态。
4)调用start()方法开始播放音频。
5)调用pause()方法暂停播放。
6)调用reset()方法停止播放。
1、新建PlayAudioTest项目,修改activity_main中代码,添加三个按钮对音频文件进行播放、暂停和停止操作。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/play"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Play"/>
<Button
android:id="@+id/pause"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pause"/>
<Button
android:id="@+id/stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stop"/>
</LinearLayout>
2、修改MainActivity中代码。
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
// MediaPlayer实例
private MediaPlayer mediaPlayer = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button play = (Button) findViewById(R.id.play);
play.setOnClickListener(this);
Button pause = (Button) findViewById(R.id.pause);
pause.setOnClickListener(this);
Button stop = (Button) findViewById(R.id.stop);
stop.setOnClickListener(this);
//运行时权限处理,动态申请SD卡读写权限
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this, new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE }, 1);
}else{
initMediaPlayer();
}
}
//为播放音频做准备
private void initMediaPlayer() {
try {
//音频文件路径
File file = new File(Environment.getExternalStorageDirectory(),"music.mp3");
mediaPlayer.setDataSource(file.getPath());
mediaPlayer.prepare();
} catch (IOException 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, "You deny the permission", Toast.LENGTH_SHORT);
finish();
}
break;
default:
}
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.play:
if(!mediaPlayer.isPlaying()){
//开始播放
mediaPlayer.start();
}
break;
case R.id.pause:
if(mediaPlayer.isPlaying()){
//暂停播放
mediaPlayer.pause();
}
break;
case R.id.stop:
if(mediaPlayer.isPlaying()){
//重置为刚创建状态
mediaPlayer.reset();
initMediaPlayer();
}
break;
default:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(mediaPlayer != null){
//释放资源
mediaPlayer.stop();
mediaPlayer.release();
}
}
}
3、添加权限声明。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
二、播放视频
Android主要通过VideoView类来实现,具体用法与MediaPlayer相似,只是VideoView对MediaPlayer进行了封装,背后仍然是通过MediaPlayer进行控制。
1、新建PlayVideoTest项目,修改activity_main中代码,添加三个按钮,用于控制视频播放、暂停与重新播放,同时添加了一个VideoView用于显示视频。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/play"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Play"/>
<Button
android:id="@+id/pause"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pause"/>
<Button
android:id="@+id/replay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Replay"/>
</LinearLayout>
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
2、修改MainActivity中代码。
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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(this);
pause.setOnClickListener(this);
replay.setOnClickListener(this);
//运行时权限
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this, new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE },1);
}else{
initVideoPath();
}
}
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, "You deny the permission", Toast.LENGTH_SHORT).show();
finish();
}
break;
default:
break;
}
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.play:
if(!videoView.isPlaying()){
//播放视频
videoView.start();
}
break;
case R.id.pause:
if(videoView.isPlaying()){
//暂停播放
videoView.pause();
}
break;
case R.id.replay:
if(videoView.isPlaying()){
//重新播放
videoView.resume();
}
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(videoView != null){
//释放资源
videoView.suspend();
}
}
}
3、加入SD卡读写权限声明。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>