我正在MainActivity的onCreate()方法中创建一个MediaPlayer实例
MediaPlayer mPlayer = MediaPlayer.create(this, Uri.fromFile(new File("/storage/emulated/0/soundrecorder/My recording #26.wav")));
它已成功创建但我收到此错误:
07-06 18:33:44.266 18366-18366/com.audiorecorder.wel.voicerecorder E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
07-06 18:33:44.267 18366-18366/com.audiorecorder.wel.voicerecorder E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
还试过这个但是logcat上的错误:
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(this, Uri.fromFile(new File("/storage/emulated/0/soundrecorder/My recording #26.wav")));
} catch (IOException e) {
e.printStackTrace();
}
mp.prepareAsync();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
我尝试过不同格式的不同音频文件,但结果是同样的错误。我也尝试在stackoverflow上搜索答案,但无法解决问题。你能帮我解决这个问题吗?
答案 0 :(得分:0)
您的文件似乎存在问题。用此更新:
musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
然后按照以下步骤操作:
1. getLoaderManager().initLoader(0,null,this);
2. @Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
switch (id){
case 0 : return new CursorLoader(getApplicationContext(),musicUri,null,null,null,null);
return new Loader<>(this);
}
3. @Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
switch (loader.getId()) {
case 0 :
if(data != null && data.moveToFirst()) {
songTitleColumnIndex = data.getColumnIndex(MediaStore.Audio.Media.TITLE);
do {
songTitle = data.getString(songTitleColumnIndex);
songsList.add(songTitle);
} while (data.moveToNext());
}
break;
因此SongTitles被放入SongList中,这是一个字符串类型的ArraList。
希望这有助于。
答案 1 :(得分:0)
尝试使用FileInputStream来改为使用FileDescriptor启动MediaPlayer:
String yourFilePath = "/wherever/your/file/is.wav";
MediaPlayer mPlayer = new MediaPlayer();
try{
FileInputStream inputStream = new FileInputStream(yourFilePath);
mPlayer.setDataSource(inputStream.getFD());
inputStream.close();
mPlayer.prepare();
mPlayer.start();
}catch (IOException e){
Log.e("IOException", e.getMessage());
}