这首歌应该在另一个意图中播放,所以我在意图中传递 Uri 并在另一个活动中播放。但是一旦我点击一首歌,应用程序就会崩溃。帮助
在主活动中.java -
@Override
public void itemClicked(View view, int position) {
Intent intent = new Intent(this,playSong.class);
songData playSong = songList.get(position);
long currSong = playSong.getID();
Uri songUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,currSong);
intent.putExtra("songPosition",songUri);
startActivity(intent);
}
}
在播放歌曲中.java -
package com.example.ray.musico_mp3player;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.IOException;
public class playSong extends AppCompatActivity {
Uri songUri=null;
MediaPlayer mediaPlayer=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_song);
songUri = getIntent().getParcelableExtra("songPosition");
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(getApplicationContext(),songUri);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
}
}
在日志猫中:
08-05 14:21:20.945 926-2524/? I/ActivityManager: Removing activity ActivityRecord{1fe06f76 u0 com.example.ray.musico_mp3player/.playSong t62 f} from stack at 1: haveState=true stateNotNeeded=false finishing=true state=DESTROYING
java.lang.RuntimeException: here
at com.android.server.am.ActivityStack.removeHistoryRecordsForAppLocked(ActivityStack.java:4117)
at com.android.server.am.ActivityStack.handleAppDiedLocked(ActivityStack.java:4841)
at com.android.server.am.ActivityStackSupervisor.handleAppDiedLocked(ActivityStackSupervisor.java:3287)
at com.android.server.am.ActivityManagerService.handleAppDiedLocked(ActivityManagerService.java:4983)
at com.android.server.am.ActivityManagerService.appDiedLocked(ActivityManagerService.java:5143)
at com.android.server.am.ActivityManagerService$AppDeathRecipient.binderDied(ActivityManagerService.java:1302)
at android.os.BinderProxy.sendDeathNotice(Binder.java:559)
请初始化媒体播放器
public class playSong extends AppCompatActivity {
Uri songUri=null;
MediaPlayer mediaPlayer=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_song);
songUri = getIntent().getParcelableExtra("songPosition");
mediaPlayer = MediaPlayer();//Here the change
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(getApplicationContext(),songUri);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
}
}