Android音视频的播放
音视频算是现代人玩手机的主要娱乐方式了,android中也提供了相应的类来实现播放的功能,先来看看音频的播放,
音频的播放在Android中一般都是使用MediaPlayer类来实现,该类中提供了十分全面的方法来控制音频的播放
播放音频的工作流程很简单,1.先创建出一个MediaPlayer对象。2.调用setDataSource()方法设置路径。3.调用prepare()方法进入准备状态。4.调用start(),pause(),reset()就会播放,暂停播放,停止了。
接下来准备一个PlayAudioTest来测试吧,布局很简单
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/play"
android:text="Play"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pause"
android:text="Pause"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/stop"
android:text="Stop"/>
</LinearLayout>
MainActivity的代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = new MediaPlayer();
findViewById(R.id.play).setOnClickListener(this);
findViewById(R.id.pause).setOnClickListener(this);
findViewById(R.id.stop).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 {
initMediaPlayer();
}
}
//初始化MediaPlayer对象
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(MainActivity.this,"You denied the permission and you can't use the player",Toast.LENGTH_SHORT).show();
finish();
}
break;
}
}
@Override
public void onClick(View v) {
switch (v.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;
}
}
@Override
protected void onDestroy() {
//释放MediaPlayer资源
super.onDestroy();
if (mediaPlayer!=null){
mediaPlayer.stop();
mediaPlayer.release();
}
}
}
动态申请权限是Android6.0之后的事情,Android系统中的MediaPlayer可以满足大部分音频的播放,而视频的播放与音频的播放差不多
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".VideoActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/play"
android:text="Play"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pause"
android:text="Pause"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/replay"
android:text="Replay"/>
<VideoView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/video_view"/>
</LinearLayout>
布局与音频的基本一致
public class VideoActivity extends AppCompatActivity implements View.OnClickListener{
private VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
videoView = findViewById(R.id.video_view);
findViewById(R.id.play).setOnClickListener(this);
findViewById(R.id.pause).setOnClickListener(this);
findViewById(R.id.replay).setOnClickListener(this);
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 {
initVideoPlayer();
}
}
@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){
initVideoPlayer();
}else {
Toast.makeText(VideoActivity.this,"You denied the permission and you can't use the player",Toast.LENGTH_SHORT).show();
finish();
}
break;
}
}
private void initVideoPlayer(){
File file = new File(Environment.getExternalStorageDirectory(),"movie.mp4");
videoView.setVideoPath(file.getPath());
}
@Override
public void onClick(View v) {
switch (v.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 = null;
videoView.suspend();
}
}
}
视频播放的类是VideoView类,这个类功能就相对不是那么强大了,不过播放一般的视频还是够用了。