Over 10 seconds to load an audiostream with MediaPlayer on Samsung Galaxy S5 Android 5.0

被刻印的时光 ゝ 提交于 2019-12-11 01:52:52

问题


Since the update to Android 5.0 MediaPlayer does not work properly on the Samsung Galaxy S5. The loading time is over 10 seconds after you start an audio stream.

Samplecode:

MediaPlayer mPlayer new MediaPlayer();
String url = "http://149.13.0.80:80/radio1.ogg";
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(url);

mPlayer.prepare();

mPlayer.start();

Same problem if I use MediaPlayer in a service/with prepareAsync()/other audiostreams. Nexus 4 with Android 5 has not such problems. Any solutions?


回答1:


First, your code should be surrounded in try-catch. So you might want to do an update such as

MediaPlayer mPlayer = new MediaPlayer();
        String url = "http://149.13.0.80:80/radio1.ogg";
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mPlayer.setDataSource(url);
            mPlayer.prepare();
            mPlayer.start();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Secondly, if your prepare() is stalling you might consider using prepare() wrapped in a thread instead of prepareAsync(). That device might be doing some behaviors that are hanging, and trying to stop the MP. Adding an actionCancel might be useful.

private void actionCancel(){ 
            try { 
                mp.setDataSource(new String());
            } catch (Exception e) {
                e.printStackTrace();
                android.util.Log.d(TAG,"actionCancel(): mp.setDataSource() exception");
                mp.reset(); 
            } 
} 


来源:https://stackoverflow.com/questions/29023132/over-10-seconds-to-load-an-audiostream-with-mediaplayer-on-samsung-galaxy-s5-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!