本文介绍了我可以将 Firebase 存储用于在线音乐流媒体吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的是将 mp3 文件保存在 Firebase 存储上,然后将其流式传输到 Android 设备.Firebase 上的所有教程都讨论了图像文件的上传和下载.

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.

如果有任何其他云比 Firebase 更容易为 android 存储和流式传输音频,请提出建议.

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

推荐答案

  1. Firebase 提供 StreamDownloadTask,正如 Mike 所建议的,用于获取 InputStream但不幸的是 MediaPlayer 不接受直接流.现在如果我们严格想继续使用 InputStream,我们有 2 个选择,据我所知 -

  1. Firebase provide StreamDownloadTask as Mike has suggested, for getting InputStreambut unfortunately MediaPlayer doesn't accept a direct stream. Nowwe 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.创建 StreamProxy 服务器作为 npr 新闻应用程序在做.您可以在这里找到应用程序的源代码.

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

从 Firebase 播放音乐文件的简单方法是首先使用存储引用获取下载 URL,然后将该 URL 作为数据源传递给媒体播放器.下面是代码片段-

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

这篇关于我可以将 Firebase 存储用于在线音乐流媒体吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 10:50