'Playing audio file from Firebase storage in android MediaPlayer

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

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

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

Nothing worked.

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

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, 


Solution 1:[1]

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

I tried with audio from Firebase Storage and it works

Solution 2:[2]

I have had this issue in the past with many things regarding Firebase. You should make sure get your permissions/security rules in Firebase are correct. You may have to wait a few minutes before the rules are set, and then you may be able to read;write your files.

You can find your rules when clicking on "Storage" from the menu, and then clicking on "rules" tab. You should see "files", "rules", and "usage".

When first starting out, I usually make sure my rules read as follows:

rules_version = '2';
// Anyone can read or write to the bucket, even non-users of your app.
// Because it is shared with Google App Engine, this will also make
// files uploaded via GAE public.
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write;
    }
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kas?m Ă–zdemir
Solution 2 user18073549