RonEskinder
RonEskinder

Reputation: 527

Android MediaPlayer won't play music on API 26+

I have an app that streams music for the user, this code works fine ok on devices running Android up to API 25, when i test in a device running API 26 or greater the Music wont play and wont show an error message either, attached is my log when using API 28 device.

What i have tried:

Log

2019-04-04 17:51:38.911 2947-24864/com.roneskinder.karaoke 
V/MediaPlayer: resetDrmState:  mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
2019-04-04 17:51:38.911 2947-24864/com.roneskinder.karaoke V/MediaPlayer: cleanDrmObj: mDrmObj=null mDrmSessionId=null
2019-04-04 17:51:38.913 2947-3054/com.roneskinder.karaoke D/EGL_emulation: eglMakeCurrent: 0xe830ce20: ver 3 0 (tinfo 0xe8315bd0)
2019-04-04 17:51:38.915 2947-24864/com.roneskinder.karaoke V/MediaHTTPService: MediaHTTPService(android.media.MediaHTTPService@aa03fa3): Cookies: null
2019-04-04 17:51:38.920 2947-4147/com.roneskinder.karaoke V/MediaHTTPService: makeHTTPConnection: CookieHandler (java.net.CookieManager@ead45b3) exists.
2019-04-04 17:51:38.920 2947-4147/com.roneskinder.karaoke V/MediaHTTPService: makeHTTPConnection(android.media.MediaHTTPService@aa03fa3): cookieHandler: java.net.CookieManager@ead45b3 Cookies: null

Upvotes: 7

Views: 7092

Answers (3)

Nikunj Paradva
Nikunj Paradva

Reputation: 16097

Just Add one line in application Tag

<application
        .......
        android:usesCleartextTraffic="true"
        .......>
        .......
    </application>

Upvotes: 1

Martin Zeitler
Martin Zeitler

Reputation: 76807

You need to define a res/xml/network_security_config.xml and permit HTTP for that host:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">server.com</domain>
    </domain-config>
</network-security-config>

That network_security_config.xml also needs to be referenced in the Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    ...>
    <application
        android:networkSecurityConfig="@xml/network_security_config"
        ...>
            ...
    </application>
</manifest>

The SDK documentation explains it all in detail - and this applies to all network traffic an app creates.

Instead of lowering the security standards, upgrading the connection to HTTPS should be preferred.


And concerning that one deprecated method .setAudioStreamType(), use .setAudioAttributes() instead - as your code already does. It does not seem to be the "main problem" here; verified that.

Upvotes: 8

Nikunj Sorathiya
Nikunj Sorathiya

Reputation: 353

Please use this code.

MediaPlayer player = new MediaPlayer();

player.setDataSource("http://www.server.com/mp3/%s.mp3");
player.setLooping(false);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.prepareAsync();

player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer player) {
                    player.start();

                }

            });

Upvotes: 0

Related Questions