Firebase Storage getDownloadUrl()始终返回我选择的最后结果 [英] Firebase Storage getDownloadUrl() always return last result I select

查看:70
本文介绍了Firebase Storage getDownloadUrl()始终返回我选择的最后结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过获取downloadUrl播放存储在Firebase存储器中的mp3文件.我使用两个微调器来确定与存储中的文件夹名称和文件名称匹配的音乐类型和音乐名称.

I'm trying to play the mp3 files which are stored on Firebase storage by getting downloadUrl. I use two spinners to decide the music types and music name which matches the folder name and file name on storage.

但是在第一次选择之后,我得到的Uri是Null.当我选择第二个时,我得到的Uri是我刚刚选择的第一个.我选择第三个,第二个,依此类推.

But after the first time choosing, the Uri I get is Null. When I choose the second one the Uri I get is the first one I just choose. I choose the third one, I get the second one and so on.

这是我得到的网址的代码.

Here is the code I get the Url.

private void prepareMusic() {
    btnPlay.setText(getString(R.string.btnplay));
    btnPlay.setEnabled(false);
    btnStop.setEnabled(false);

    mStorageRef.child("music/"+musicType+"/"+musicName).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            uriTest = uri.toString();
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            //uriTest =" ";
        }
    });
//the toast here is the correct type and name I choose
    Toast.makeText(this,"Now musicType is: " + musicType + " musicName is:" + musicName, Toast.LENGTH_SHORT).show();
//the Uri here is always last one I choose and null at beginning
    Toast.makeText(this,"uri is: " + uriTest, Toast.LENGTH_LONG).show();

    try{
        mper.reset();
        mper.setDataSource(uriTest);
        mper.prepareAsync();
    }catch (Exception e){
        tos.setText(getString(R.string.setTrackError) + e.toString());
        tos.show();
    }
}

我在这里搜索了很多问题,但是没有很好的答案来解决我的问题.

I have searched lots of question here, but there is not a good answer to deal with my problem.

推荐答案

getDownloadUrl()是异步的,并在调用代表当前工作的Task对象后立即返回.然后,您的代码将在 下载网址可用之前显示两个祝酒词.

getDownloadUrl() is asynchronous and returns immediately after it's called with a Task object that represents the work in progress. Your code will then display two toasts before the download url is available.

仅应在调用成功回调后立即使用下载URL,因此将代码移入其中,或在其中调用使用URL的方法.

You should only be using the download URL from the moment that the success callback is invoked, so move your code in there, or call a method in there that uses the URL.

要了解有关Firebase API为何异步的更多信息,请阅读此博客.

To learn more about why Firebase APIs are asynchronous, read this blog.

这篇关于Firebase Storage getDownloadUrl()始终返回我选择的最后结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆