3

I'm writing a program in which I need to play a song from storage. I set up a MediaPlayer for this (code fragment below), but kept getting a java.io.IOException: Prepare failed.: status=0x1. Some websites indicated that this error may occur because the files I am trying to read are not world readable. If this is true, how could I change the file permissions from the program, get around this, or some other solution. If not, please help me figure out what is wrong with this. Thanks in advance.

MediaPlayer player = new MediaPlayer();
player.reset();
player.setDataSource(selectedFilePath);
player.prepare();
player.start();
4

1 回答 1

7

Where are you attempting to read the files from? It's possible that you're looking at the /sdcard/ area, and your application does not have the ability to read_external_storage

If that is the case, just make sure you add the following line to your manifest

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

[Edit] I'm not sure where you're trying to load from, but it might help to use a descriptor instead of a path. See if this helps:

FileInputStream fis = new FileInputStream(locationOfFile);
mediaPlayer.setDataSource(fis.getFD());

If this still doesn't help, I would recommend trying another file name to make sure your media player setup is correct. Or you can do something like

File thisFile = new File("/storage/sdcard0/Music/song.mp3");
if (thisFile.exists())
   Log.v("file", "Exists");
else
   Log.v("file", "Could not be located.");
于 2012-07-31T21:46:24.580 回答