0
我一直在從SD卡獲取音樂文件列表,我很成功,但問題是,當我點擊一個文件,它應該播放該文件,但它是給我這個錯誤。 這裏是我的代碼:MediaPlayer準備faile:狀態= 0x1
private String[] mMusicList;
static long playlistid=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMediaPlayer = new MediaPlayer();
ListView mListView = (ListView) findViewById(R.id.phonemusiclist);
mMusicList = getMusic();
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mMusicList);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
try {
playSong(mMusicList[arg2]);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private String[] getMusic() {
final Cursor mCursor = managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media.DISPLAY_NAME }, null, null,
"LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
int count = mCursor.getCount();
String[] songs = new String[count];
int i = 0;
if (mCursor.moveToFirst()) {
do {
songs[i] = mCursor.getString(0);
i++;
} while (mCursor.moveToNext());
}
mCursor.close();
return songs;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void playSong(String path) throws IllegalArgumentException,IllegalStateException, IOException {
String extStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
path = extStorageDirectory + File.separator + path;
mMediaPlayer.reset();
FileInputStream fis = new FileInputStream(path);
mMediaPlayer.setDataSource(fis.getFD());
mMediaPlayer.prepare();
mMediaPlayer.start();
}
你好..我已經嘗試wt u建議。但仍然沒有實現的結果,似乎我錯了別的地方... – ank
然後做**編輯**你的問題,因爲人們被誤導了! – BlueSword