我想在Android中制作媒体播放器并出错。 我有两个活动(FileBrowser和MainActivity(使用Player))。 当我点击文件浏览器中的listitem时,我将发送文件路径 到我的MainActivity.class。
private OnItemClickListener musicgridlistener = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
System.gc();
music_column_index = musiccursor
.getColumnIndexOrThrow(Audio.Media.DATA);
musiccursor.moveToPosition(position);
String filename = musiccursor.getString(music_column_index);
Uri path = Uri.parse(filename);
Player.initMusicPlayer(path);
finish();
}
};
我收到了MainActivity.class中“initMusicPlayer”方法的路径
public void initMusicPlayer(Uri filename){
player = new MediaPlayer();
playFile(filename);
}
“playFile”方法:
public void playFile(Uri filename){
Log.i("Filename: ", filename.toString());
stopPlaying();
player = MediaPlayer.create(MainActivity.this,filename);
player.start();
}
“stopPlaying”方法:
private void stopPlaying() {
if (player != null){
player.stop();
player.release();
player = null;
}
}
当我现在用新路径调用“initMusicPlayer”时,播放器会启动新文件但旧版本不会停止。
感谢您的帮助。