Monday, 8 April 2019

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:

  • MediaPlayer.setAudioStreamType(int streamtype) Deprecated so i implemented AudioAttributes for API above 26
  • Different types of mp3 files, but they all play the same in api 25 and below, when i try on device or emulator with API 26 nothing happens
  • Search in the docs and examples, but all look the same i see no difference in my code

    private void playMusic() {
    try {
        mMediaPlayer.reset();
    
        // For Android API 26 (Android 8 Oreo) and newer, specify AudioAttributes.
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            Log.d("Record", "setAudioAttributes()");
            AudioAttributes.Builder builder = new AudioAttributes.Builder();
            builder.setUsage(AudioAttributes.USAGE_MEDIA);
            builder.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC);
            AudioAttributes attributes = builder.build();
            mMediaPlayer.setAudioAttributes(attributes);
    
        } else {
            Log.d("Record", "setAudioStreamType()");
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        }
    
        mMediaPlayer.setDataSource(String.format("http://www.server.com/mp3/%s.mp3", mp3File));
        mMediaPlayer.prepareAsync();
        mMediaPlayer.start();
    
    } catch (IOException e) {
        Log.d("Record", "error playing mp3");
        e.printStackTrace();
    }
    }
    
    

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



from Android MediaPlayer won't play music on API 26+

No comments:

Post a Comment