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.

- 5,578
- 7
- 31
- 55
-
setVideoURI seems to call mMediaPlayer.prepareAsync() internally, so it should just return without blocking but it doesn't. So switching to MediaPlayer from VideoView would not be helpful, too. – Micky Jul 09 '14 at 09:55
-
Have you tried to put your code inside an asynctask? – Jul 09 '14 at 21:02
-
3Does this happen every time you call `setVideoUri()`? Or only from the second time onwards? – matiash Jul 10 '14 at 03:33
-
I am using AsyncTask, but it does not have any effect. I don't know if it happens the first time because the first time I call it, there's also other stuff going on. But I am making multiple subsequent calls quite rapidly, so maybe it's related to that? I think it's getting worse with every call, then the UI blocks for some time and then it's better again. – Micky Jul 10 '14 at 08:08
-
Hye Kim are you able to solve this issue ? – nilkash Oct 06 '14 at 08:20
-
Hi, no I couldn't solve it. This seems to be a problem of the SDK. – Micky Oct 06 '14 at 09:38
4 Answers
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

- 31
- 3
-
Actually I experience that when the internet connection is slow, the blocking becomes much more severe. That would speak against the decoding being the only source of the delays. Or is there a explanation why media decoding would block more on a bad connection? – Micky Jul 18 '14 at 08:24
-
I am really not sure, that depends on what are you doing in source, but if the connection is slow, then decoder has more staff to do, because repeatedly calculates and do operations which usually don't because the buffer is cleared between waiting on packets – Stanojevic Dalibor Jul 20 '14 at 12:59
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

- 49
- 2
- 5
-
This doesn't seem to help. It allows the UI thread to progress further, so it has the appearance of working, but as soon as the handler runs the UI thread is blocked while the media is decoding – MrK Sep 04 '20 at 21:07
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.

- 36
- 3
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

- 54,791
- 16
- 125
- 154

- 292
- 2
- 13
-
1In what way is that supposed to help me? I am already using the setVideoURI call and that is the cause of my performance problems – Micky Jul 02 '14 at 12:10