Android VideoView setVideoURI blocks UI thread

耗尽温柔 提交于 2019-12-10 14:26:21

问题


The setVideoURI method of VideoView in Android seems to be blocking the UI thread. As soon as I call this method, the UI get's laggy, even on fast devices. Is there a way to improve performance here? The only other thread with that topic I could find here:
https://groups.google.com/forum/#!topic/android-developers/eAAEAEDcksM
but it's quite old and doesn't have a satisfying answer.


回答1:


What i did was place the setVideoUri() method into a handler with a looper i.e.

new Handler(Looper.myLooper()).post(new Runnable(){
    @Override
    public void run(){
        videoview.setVideoUri("uri");
    }
});

this runs the code outside the main UI thread, but keeps it in a looper so the code can be executed without throwing an exception




回答2:


VideoView.setVideoURI() starts a new thread for media playback, but it is the media decoding part which causes extra delay.The only solution that might be ok for you is using some NDK hacks, but doesn't worths for me




回答3:


I have found that VideoView not blocking UI thread. Actually in the receiver of "android.media.VOLUME_CHANGED_ACTION" blocking UI thread.

My problem code:

public void setVolume(int volume) {
    try {
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
        ivVoice.setTag(volume != 0);
        updateVoiceImage();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

setVolume called from volume receiver , it has called 1000+ times when playing a 5s mp4.

audioManager.setStreamVolume takes too long in main thread.

solution code:

public void setVolume(int volume) {
    try {
        //if volume not changed , do nothing.
        if(volume != lastVolume){
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,volume,0);
            ivVoice.setTag(volume != 0);
            updateVoiceImage();
            lastVolume = volume;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

So, please check your volume receiver. Maybe it can helps you.




回答4:


VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
myVideoView.setVideoURI(Uri.parse(url));
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();

i used this code may it will help you



来源:https://stackoverflow.com/questions/24529090/android-videoview-setvideouri-blocks-ui-thread

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