我做的一樣api guides -無法使用MediaPlayer對象來播放一些MP3文件
我只是有一些文件和文件路徑 - 我要玩它。
唯一的例外是:/storage/emulated/0/111.mp3(拒絕)
我已經添加的所有權限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
代碼:
public class PlayingModule {
private MediaPlayer _mediaPlayer;
private Activity _content;
public PlayingModule(Activity content) {
// this._mediaPlayer = new MediaPlayer();
this._content = content;
}
public void PlayMediaFile(URI mediaFilePath) throws IOException {
Uri auri = Uri.parse(mediaFilePath.toString());
_mediaPlayer = MediaPlayer.create(_content , auri);
_mediaPlayer.stop();
_mediaPlayer.reset();
_mediaPlayer.release();
_mediaPlayer = MediaPlayer.create(_content , auri);
_mediaPlayer.start();
}
}
調用此課程和播放方法:
public void play() throws IOException {
File path = Environment.getExternalStorageDirectory();
File file = new File(path + "/111.mp3");
PlayingModule pm = new PlayingModule(this);
pm.PlayMediaFile(file.toURI());
不明白爲什麼它不工作
我也更改代碼以這個代碼 - ...並且仍然無法正常工作..
Uri uri = Uri.parse(mediaFilePath.toString());
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(_content.getApplicationContext(), uri);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mediaPlayer.prepareAsync();
你持有'READ_EXTERNAL_STORAGE'和/或'WRITE_EXTERNAL_STORAGE'權限,包括運行時的權限?此外,使用'新的文件(路徑。「/111.mp3」);',而不是'新的文件(路徑+「/111.mp3」);'。另外,爲什麼你要創建兩個'MediaPlayer'實例?最後,「不工作」是什麼意思? – CommonsWare
添加權限..不起作用。 – Yanshof