大家好我想制作Mp3播放器和firebase
存储上的歌曲。我能够第一次运行这首歌时,我改变了歌曲应用程序很遗憾地停止并给我这个错误
07-29 23:24:17.359 23466-23466/com.example.daniyal.audioplayer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.daniyal.audioplayer, PID: 23466
java.lang.IllegalStateException
at android.media.MediaPlayer.nativeSetDataSource(Native Method)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1071)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1032)
at com.example.daniyal.audioplayer.MainActivity$4.onSuccess(MainActivity.java:82)
at com.example.daniyal.audioplayer.MainActivity$4.onSuccess(MainActivity.java:76)
at com.google.android.gms.tasks.zzj.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
我的代码在这里:
MediaPlayer mediaPlayer;
FirebaseStorage firebaseStorage;
Button btn1 , btn2;
String jai = "https://firebasestorage.googleapis.com/v0/b/audioplayer-49893.appspot.com/o/jai.mp3?alt=media&token=f315beb6-83ea-44f0-9beb-91d5108d9baa";
String tose = "https://firebasestorage.googleapis.com/v0/b/audioplayer-49893.appspot.com/o/tose.mp3?alt=media&token=4b3d9449-ff26-4fe4-9f5c-b5d3e006a640";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.jai);
btn2 = (Button) findViewById(R.id.tose);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
playjai();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
playtose();
}
});
}
public void playjai(){
if (mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
playmusic(jai);
}
public void playtose(){
if (mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
playmusic(tose);
}
private void playmusic(String url){
firebaseStorage = FirebaseStorage.getInstance();
StorageReference storageRef = firebaseStorage.getReferenceFromUrl(url);
storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
try {
// Download url of file
String url = uri.toString();
mediaPlayer.setDataSource(url);
// wait for media player to get prepare
mediaPlayer.setOnPreparedListener( MainActivity.this);
mediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i("TAG", e.getMessage());
}
});
}
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
try {
mediaPlayer.start();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
}
我刚刚添加了两个用于测试应用的按钮。我第一次点击任何按钮,当我点击另一个按钮应用程序时,它将开始播放歌曲。
答案 0 :(得分:3)
非常仔细地研究MediaPlayer state diagram及其描述。每次在MediaPlayer上调用方法时,它都必须符合有效的状态转换,否则会抛出IllegalStateException。
您只能在MediaPlayer处于空闲状态时调用setDataSource()
。在第一次播放之后,你试图再次调用它。如果要重用同一个MediaPlayer实例,则需要在其上调用reset()
将其重新发送回空闲状态,然后再次调用setDataSource()
。