33

What I want is to save mp3 files on Firebase Storage and then stream it to an Android device. All the tutorials on Firebase discuss about the image file upload and download.

If there are any other cloud that is more easy than Firebase to store and stream audio for android, then please suggest.

AL.
  • 36,815
  • 10
  • 142
  • 281
Brijesh Kumar
  • 1,685
  • 2
  • 17
  • 28
  • 2
    at least you should try it first, and in case you have problem you can ask in SO. here for start you can upload any file in GC storage. https://firebase.google.com/docs/storage/android/upload-files to play/stream music you can use https://developer.android.com/guide/topics/media/mediaplayer.html#mediaplayer – hakim Sep 29 '16 at 07:54
  • 4
    We've got a streaming download API: https://firebase.google.com/docs/reference/android/com/google/firebase/storage/StreamDownloadTask – Mike McDonald Sep 29 '16 at 15:23

1 Answers1

33
  1. Firebase provide StreamDownloadTask as Mike has suggested, for getting InputStream but unfortunately MediaPlayer doesn't accept a direct stream. Now we have 2 options if we strictly like to continue with InputStream, as far as I know -

    a. Write a stream in the temporary file and pass it to the media player. Personally, I don't recommend this approach. It includes unnecessary data writing and you also have to maintain synchronization between streamed data, file writing and media play.

    b. Create StreamProxy server as npr news App doing. You can find the source code of App here.

  2. Simple way to play Music file from Firebase is to first fetch the download URL by using storage reference and pass that URL to media player as a dataSource. Below is the code snippet -

    public class MainActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener {
    
            private MediaPlayer mMediaplayer;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                mMediaplayer = new MediaPlayer();
                mMediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                fetchAudioUrlFromFirebase();
            }
    
            private void fetchAudioUrlFromFirebase() {
                final FirebaseStorage storage = FirebaseStorage.getInstance();
                // Create a storage reference from our app
                StorageReference storageRef = storage.getReferenceFromUrl("PATH_OF_YOUR_AUDIO_FILE");
                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(MainActivity.this);
                            mMediaplayer.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 mp) {
                mp.start();
            }
        }
    
Rahul
  • 10,457
  • 4
  • 35
  • 55