源码下载:
https://download.csdn.net/download/qq_31939617/10443928下载
使用MediaPlayer播放assets目录下的媒体文件
1) 通过Context.getAssets()方法获得AssetManager对象
2) 通过AssetManager对象的openFd(String name)方法打开指定的原生资源文件夹,返回一个AssetFileDescriptor对象
3) 通过AssetFileDescriptor的getFileDescriptor()得到一个FileDescriptor对象
4) 通过public void setDataSource (FileDescriptor fd, long offset, long length)来创建MediaPlayer对象
5) 调用MediaPlayer.prepare()方法准备音频
6) 调用MediaPlayer的start()、pause()、stop()等方法控制
先上图:
MainActivity.class
package com.example.sz.mediaplayerdemo;
import android.content.Context;
import android.media.AudioManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import java.io.IOException;
/** * 使用MediaPlayer播放asset目录下的音乐MP3和raw目录下的音乐MP3 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "MainActivity";
private Button openAssetMusic;
private MediaPlayer mediaPlayer;
private Button pause;
private Button openRawMusic;
private Button pauseRawMusic;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
openAssetMusic = findViewById(R.id.openAssetMusic);
openAssetMusic.setOnClickListener(this);
pause = findViewById(R.id.pause);
pause.setOnClickListener(this);
openRawMusic = findViewById(R.id.openRawMusic);
openRawMusic.setOnClickListener(this);
pauseRawMusic = findViewById(R.id.pauseRawMusic);
pauseRawMusic.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.openAssetMusic:
//开始播放
openAssetMusics();
break;
case R.id.pause:
//暂停播放
mediaPlayer.pause();
break;
case R.id.openRawMusic:
//开始播放
openRawMusicS();
break;
case R.id.pauseRawMusic:
//暂停播放
mediaPlayer.pause();
break;
}
}
/** * 打开raw目录下的音乐mp3文件 */
private void openRawMusicS() {
mediaPlayer = MediaPlayer.create(this, R.raw.please_get_close);
//用prepare方法,会报错误java.lang.IllegalStateExceptio
//mediaPlayer.prepare();
mediaPlayer.start();
}
/** * 打开assets下的音乐mp3文件 */
private void openAssetMusics() {
try {
//播放 assets/a2.mp3 音乐文件
AssetFileDescriptor fd = getAssets().openFd("identification_success.mp3");
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:text="assets播放" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/openAssetMusic"/>
<Button android:text="暂停assets" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/pause"/>
<Button android:text="raw播放" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/openRawMusic"/>
<Button android:text="暂停raw" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/pauseRawMusic"/>
</LinearLayout>
源码下载:
https://download.csdn.net/download/qq_31939617/10443928下载