VideoView streaming is not buffered enough

my Android app directs videos online through VideoView . When playing video from a file, it works fine or even streaming live ( m3u8 ); It is always transmitted from one source, and when I use an external player / browser, it also streams perfectly (so I don’t think this is a problem with the source, which is a variant of a file like this: https://publish.dvlabs.com /democracynow/360/dn2016-0810.mp4

Android Monitor logs this just before the crash:

 10-13 12:02:56.204 32460-32748/com.workingagenda.democracydroid D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 6.0.1) 10-13 12:02:56.205 32460-32472/com.workingagenda.democracydroid D/MediaHTTPConnection: proxy null port 0 10-13 12:02:57.904 32460-32460/com.workingagenda.democracydroid D/MediaPlayer: getMetadata 10-13 12:02:58.438 32460-377/com.workingagenda.democracydroid W/MediaPlayer: info/warning (3, 0) 

and then I get these logs on failure:

 10-13 12:05:33.812 32460-32472/com.workingagenda.democracydroid W/MediaHTTPConnection: readAt 26869519 / 241 => java.net.ProtocolException: unexpected end of stream 10-13 12:08:32.480 32460-3546/com.workingagenda.democracydroid E/MediaPlayer: error (1, -1004) 10-13 12:08:32.480 32460-32460/com.workingagenda.democracydroid E/MediaPlayer: Error (1,-1004) 10-13 12:08:32.481 32460-32460/com.workingagenda.democracydroid D/VideoView: Error: 1,-1004 [ 10-13 12:08:32.512 5066: 453 E/ ] Destroy C2D instance [ 10-13 12:08:32.512 5066: 453 E/ ] Destroy C2D instance 10-13 12:08:32.635 32460-32472/com.workingagenda.democracydroid E/MediaPlayer: error (1, -1004) 10-13 12:08:32.668 32460-32460/com.workingagenda.democracydroid E/MediaPlayer: Error (1,-1004) 10-13 12:08:32.668 32460-32460/com.workingagenda.democracydroid D/VideoView: Error: 1,-1004 

To clarify my question:

  • I would like to know what this error is, E/MediaPlayer: Error (1,-1004) (since I did not find any information on the Internet about this).
  • If this is what I suspect mainly as the end of a file / stream error, then I hope to get some help with buffering or otherwise load the video in such a way as to avoid this?

I saw this question, Android Streaming with MediaPlayer: Error (1, -1004) and 3GPP video , but the answers do not help much.

I found the function MediaPlayer.prepareAsync() , here https://developer.android.com/reference/android/media/MediaPlayer.html#prepareAsync () , this is automatically called when VideoView opens the video, but this does not seem to work .

Edit

So, the solution brought me to Google ExoPlayer , which was pretty easy to replace for my VideoView, and it works like a charm.

  • Add ExoPlayer as a dependency
  • Change layout view to SimpleExoPlayerView
  • Initialize SimpleExoPlayer in action
  • Initialize MediaSource and connect to the player
  • Remember release() when you no longer need to.

And with this, streaming works without problems.

+5
source share
3 answers

The Android MediaPlayer class does not provide access to lower-level settings, such as buffer size.

The form log -1004 means: public static final int MEDIA_ERROR_IO

For me, this code works fine:

 try{ MediaController mediaController = new MediaController(this); Uri video = Uri.parse(url); mediaController.setAnchorView(videoView); videoView.requestFocus(); videoView.setMediaController(mediaController); videoView.setVideoURI(video); videoView.setOnPreparedListener(new OnPreparedListener() { @Override public void onPrepared(MediaPlayer arg0) { videoView.start(); } }); }catch (Exception e) { e.printStackTrace(); } 

It seemed like it was something related to the source of the stream, because some sources ended up playing on the device, but the one we needed never played.

If you can see, then you know some problems with your log stream.

 MediaHTTPConnection: readAt 26869519 / 241 => java.net.ProtocolException: unexpected end of stream 

This exception is thrown by FixedLengthInputStream when the expected number of bytes (usually specified in the response content length header) is greater than the actual data in the response. Verify that the content header is correct. (If you use your value for the length of the content, make sure it is correct.)

For details, see in this post the unexpected end of a stream error.

Other it will also help you, please check the link to create a long buffer for MediaPlayer

+1
source

VideoView on Android is not the best, if you use VLC, you will get much smoother video buffering.

Here are instructions for compiling VLC from Android source code: https://wiki.videolan.org/AndroidCompile/

0
source

Using VideoView in streaming video can cause some problem. I also ran into many problems.

Use the JieCaoVideoPlayer library for the Video stream. This allows you to use more options.

https://github.com/lipangit/JieCaoVideoPlayer

Just import it

 compile 'fm.jiecao:jiecaovideoplayer:5.3' 

Add to your layout as usual

 <fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard android:id="@+id/videoplayer" android:layout_width="match_parent" android:layout_height="200dp"/> 

Set the URL you want to play

 JCVideoPlayerStandard jcVideoPlayerStandard = (JCVideoPlayerStandard) findViewById(R.id.videoplayer); jcVideoPlayerStandard.setUp("http://2449.vod.myqcloud.com/2449_22ca37a6ea9011e5acaaf51d105342e3.f20.mp4" , JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, "Video"); jcVideoPlayerStandard.thumbImageView.setImage("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640"); 
0
source

Source: https://habr.com/ru/post/1258159/


All Articles