Android MediaPlayer Error(1, -2147483648) when playing an mp3 locally

99封情书 提交于 2019-12-10 20:18:51

问题


Shortly after Android 4.4 came out my code which has been working correctly since 2.2 suddenly went buggy.

My code will query the music database for a given music file. The path is returned in the query and this path is then passed onto the MediaPlayer.

Code:

String uri = "content://media/internal/audio/media";
String[] columns = [audio_id as _id, title, album, artist, album_id, _data];
String where = "album = 'XX'";
Cursor c = this.getContentResolver().query(uri, columns, where, null, MediaColumns.TITLE);
String musicPath = c.getString(5);
// musicPath is "/storage/emulated/0/Music/The XX/Islands.mp3"

player.reset();
player.setDataSource(musicPath);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.prepareAsync();

I end up with the useless Error(1, -2147483648) message in my LogCat.

I've also tried replacing

player.setDataSource(musicPath);

with

FileInputStream fileInputStream = new FileInputStream(musicPath);
player.setDataSource(fileInputStream.getFD());
fileInputStream.close();

But this produces a FileNotFoundException on the new FileInputStream(...):

java.io.FileNotFoundException: /storage/emulated/0/Music/The XX/Islands.mp3: open failed: EACCES (Permission denied)

This is interesting because the path returned by the query begins with "/storage/emulated/..." but if I look in the DDMS view in Eclipse the actual path is "/mnt/shell/emulated/...". Is this normal?

Despite that, if I change the path prefix manually to what I see in the file explorer, I still get a FileNotFoundException on the new FileInputStream(...):

java.io.FileNotFoundException: /mnt/shell/emulated/0/Music/The XX/Islands.mp3: open failed: EACCES (Permission denied)

Same exception. What is with this Permission denied business? Up until KitKat it was working fine. I've also since added the permission :

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

My app has this line in the manifest:

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" />

Has anybody seem something similar and found a solution to this unknown error? The mp3s that I am trying to play work fine in Play Music and another music player app, thus leading me to believe the problem is not a missing codec.

Thanks Sean

EDIT: I've checked immediately after the content query if the file exists and the path returned does not.

File f = new File(musicPath);
boolean exists = f.exists();  // false

How is this possible? I've tried querying internal media instead but that returns no results (as expected)


回答1:


I got around this with the (unfortunate) solution of adding a write permission to the app.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Apparently this is necessary from 4.4 onward. Can anybody confirm or deny this?

Sean



来源:https://stackoverflow.com/questions/20454673/android-mediaplayer-error1-2147483648-when-playing-an-mp3-locally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!