问题
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