MediaPlayer中播放Firebase存储中的音频文件

MediaPlayer中播放Firebase存储中的音频文件

本文介绍了在Android MediaPlayer中播放Firebase存储中的音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在应用程序中播放Firebase存储中的音频文件.

I'm trying to play audio file from Firebase storage in my application.

我在youtube视频中编写了与此完全相同的代码,但没有播放任何歌曲.

I wrote code exactly same from the youtube video about this, but it doesn't play a song.

谁能告诉我问题出在哪里?

Can anybody tell what is the problem?

我在某些问题上尝试了很多解决方案

I tried pretty many solutions in some questions

例如清单中缺少权限,MediaPlayer方法的重定位等.

such as missing permission in Manifest, relocation of MediaPlayer methods etc.

什么都没做.

public void play_Song(View view) {
    final MediaPlayer mediaPlayer = new MediaPlayer();
    try {
        mediaPlayer.setDataSource( url );
        mediaPlayer.prepare();
        mediaPlayer.setOnPreparedListener( new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        } );

    } catch (IOException e) {
        e.printStackTrace();
    }

    mediaPlayer.setOnCompletionListener( new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mediaPlayer.release();
        }
    } );
}

当我运行play_Song方法时,我可以检查以下内容

When I run play_Song method, I can check following

V/MediaHTTPService: MediaHTTPService(android.media.MediaHTTPService@aeb6194): Cookies: null
V/MediaHTTPService: makeHTTPConnection: CookieManager created: java.net.CookieManager@a6bd43d
makeHTTPConnection(android.media.MediaHTTPService@aeb6194): cookieHandler: java.net.CookieManager@a6bd43d Cookies: null
D/NetworkSecurityConfig: No Network Security Config specified, using platform default

过一会儿,我可以检查一下

and after a while, I can check this out

E/MediaPlayerNative: error (1, -2147483648)
W/System.err: java.io.IOException: Prepare failed.: status=0x1
        at android.media.MediaPlayer._prepare(Native Method)
        at android.media.MediaPlayer.prepare(MediaPlayer.java:1274)
        at techtown.testtest.mediaplayertest.MainActivity.play_Song(MainActivity.java:71)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
        at android.view.View.performClick(View.java:7125)
        at android.view.View.performClickInternal(View.java:7102)
        at android.view.View.access$3500(View.java:801)
W/System.err:     at android.view.View$PerformClick.run(View.java:27336)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
E/catch: complete
I/Choreographer: Skipped 1813 frames!  The application may be doing too much work on its main thread.
I/OpenGLRenderer: Davey! duration=30227ms; Flags=0, IntendedVsync=531877218816690, Vsync=531907435482148, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=531907438049220, AnimationStart=531907438097820, PerformTraversalsStart=531907438477920, DrawStart=531907438622820, SyncQueued=531907439252120, SyncStart=531907440144220, IssueDrawCommandsStart=531907440191720, SwapBuffers=531907441436520, FrameCompleted=531907447223120, DequeueBufferDuration=468000, QueueBufferDuration=353000,

推荐答案

您可以将其用于NetworkSecurityConfig错误:

You can use this for NetworkSecurityConfig error:

android:usesCleartextTraffic="true"

添加到清单文件中的应用程序.

add to application in manifest file.

mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
mediaplayer.prepareAsync();
mediaplayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.start();
    }
});

我尝试使用Firebase Storage的音频,并且可以正常工作

I tried with audio from Firebase Storage and it works

这篇关于在Android MediaPlayer中播放Firebase存储中的音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 03:49