Can I use Firebase Storage for online music streaming?

前端 未结 1 527
轻奢々
轻奢々 2020-12-04 18:39

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.

相关标签:
1条回答
  • 2020-12-04 18:55
    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();
              }
          }
      
    0 讨论(0)
提交回复
热议问题