Search code examples
androidfirebasestreamingmp3firebase-storage

streaming a mp3 from a site/server


hi guys me and a few other djs want to be able to upload our mixes to a site that could then be streamed direct in to my app. at the moment i have the files stored on google drive. they will download and then i can play via a media player. but i want the user to be able to just stream the music direct from the site..

1) what site would you suggest for uploading of the music. i have a firebase account would this be an idea or would the data from the files i upload be expensive? im on the pay as you go account type. ****EDIT So ive found out that firebase could be a n actual viable option for doing this

2) what would be the best way to implement the code so that it streams the music direct rather than downloading it first?

****EDIT ive found some code while trawling the internet that could make this viable but i seam to be getting a null pointer error on the setDatasource

this is my code

i have this my onchild added

 final String url = dataSnapshot.child("url").getValue(String.class);
  urlList.add(url);

then in my onitemclick i use

mp3url = urlList.get(i);
 fetchAudioUrlFromFirebase();

finally the method for streaming the mp3

 private void fetchAudioUrlFromFirebase() {
    String mp3 = mp3url;
    final FirebaseStorage storage = FirebaseStorage.getInstance();
    storageRef = storage.getReferenceFromUrl(mp3);
    storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            try {
                // Download url of file
                final String url = uri.toString();
                mMediaplayer.setDataSource(url);
                // wait for media player to get prepare
                mMediaplayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mp.start();
                    }
                });
                mMediaplayer.prepareAsync();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.i("TAG", e.getMessage());
                }
            });

}

this is my full logcat with the null pointer error

07-11 18:39:45.326 2565-2565/com.example.harrops.h20droidapp2 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.example.harrops.h20droidapp2, PID: 2565
                                                                            java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setDataSource(java.lang.String)' on a null object reference
                                                                                at com.example.harrops.h20droidapp2.Mixes$3.onSuccess(Mixes.java:198)
                                                                                at com.example.harrops.h20droidapp2.Mixes$3.onSuccess(Mixes.java:192)
                                                                                at com.google.android.gms.tasks.zze$1.run(Unknown Source)
                                                                                at android.os.Handler.handleCallback(Handler.java:751)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:154)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Solution

  • Following line means: mMediaPlayer is null.

    java.lang.NullPointerException: Attempt to invoke virtual method 
    'void android.media.MediaPlayer.setDataSource(java.lang.String)'
    on a null object reference
    

    Please check mMediaplayer is initialized before onSuccess(Uri uri) method is called.

    mMediaplayer = new MediaPlayer();