Daniyal Ahmed Khan
Daniyal Ahmed Khan

Reputation: 88

Android Media Player with Firebase Multiple Mp3 Files

Hello Everyone I want to make Mp3 player and the songs on firebase storage. i am able to run the song first time when i changed the song app is unfortunately stopped and giving me this error

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)

My code is here:

 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();
    }
}

}

I have just added two buttons for testing app. First time i click any button it start playing song when i click another button app will be stopped.

Upvotes: 0

Views: 1752

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

Study the MediaPlayer state diagram and its description very carefully. Any time you call a method on MediaPlayer, it has to comply with the valid state transitions, otherwise it will throw IllegalStateException.

You can only call setDataSource() on MediaPlayer when it's in the idle state. You're trying to call it a second time after it was already set for the first play. If you want to reuse the same instance of MediaPlayer, you'll need to call reset() on it to send it back to the idle state before calling setDataSource() on it again.

Upvotes: 3

Related Questions