my android app streams a video online in a VideoView
. When playing a video from a file, it works fine, or even streaming live (an m3u8
); It's always streaming from the same source, and when I use an external player/browser, it likewise streams fine (so I don't think this is an issue with the source, which is a variation of a file like this: https://publish.dvlabs.com/democracynow/360/dn2016-0810.mp4
The 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 when it crashes:
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 be more precise about my question:
- I'd like to know what this error,
E/MediaPlayer: Error (1,-1004)
is (as I haven't found any info on the web about it). - If it is what I suspect, basically as end of file/stream error, then I am hoping to get some help buffering or otherwise loading the video in such a way to avoid this?
I've seen this question, Android Streaming with MediaPlayer: Error(1, -1004) and 3GPP video, but the answers aren't much help.
I've found a function, MediaPlayer.prepareAsync()
, here https://developer.android.com/reference/android/media/MediaPlayer.html#prepareAsync(), this is automatically called when a VideoView
opens a video, but this doesn't seem to work.
Edit
So the solution brought me to Google's ExoPlayer, which was pretty easy to swap in for my VideoView, and it works like a charm.
- Add ExoPlayer as dependency
- Change view in layout to
SimpleExoPlayerView
- Initialize
SimpleExoPlayer
in Activity - Initialize
MediaSource
and attach to player - Remember to
release()
when no longer needed.
And with that, streaming works seamlessly.