Sunday, October 13, 2013

Android Network Video



Network Video Playback

     Android supports HTTP and RTSP video playback in all three video playback methods. Using either the built-in Media Player activity via an intent or the VideoView class to play either form of network video requires no source code changes.Simply use the HTTP or RTSP URL as the video Uri, and it will work as long as the format is supported.

Download Source Code Click Here

Network Video Player Using VideoView

Here is the activity example uses a VideoView with an RTSP URL to a video from YouTube’s mobile site. The only change is the string passed in to construct the videoUri.



MediaPlayer Network Video Player (Using SurfaceView)

Download Source Code Click Here

Working with the MediaPlayer for network video playback is similar to the MediaPlayer and MediaController code.


The StreamingVideoPlayer activity implements many of the available listener and callback abstract classes from MediaPlayer, SurfaceHolder, and MediaController. The OnBufferingUpdateListener is particularly useful when dealing with network delivered media. This class specifies an onBufferingUpdate method that is repeatedly called while the media is buffering, allowing us to keep track of how full the buffer is.


In this code, we’ll use a TextView called statusView to display status messages to the user. The reason we’ll do so is that loading a video for playback via the Internet can take quite a bit of time, and without some sort of status message, the user may think the application has hung.


Among the list of MediaPlayer event listeners, our activity implements and is registered to be the OnBufferingUpdateListener.



Instead of playing back a file from the SD card, we’ll be playing a file served from an RTSP server. The URL to the file is specified in the following String, filePath. We’ll then use the MediaPlayer’s setDataSource method, passing in the filePath String. The MediaPlayer knows how to handle loading and playing data from an RTSP server, so we don’t have to do anything else different to handle it.



We’ll use the MediaPlayer’s prepareAsync method instead of prepare. The prepareAsync method does the preparation in the background on a separate thread. This makes it so that the user interface doesn’t hang. This would allow the user to perform other actions or allow us as the developer to display a loading animation or something similar.


}

Since our activity implements the OnBufferingUpdateListener and is registered to be the listener for the MediaPlayer, the following method will be called periodically as media is downloaded and buffered. The buffering will occur during the preparation stage (after onPrepareAsync or onPrepare is called).

public void onBufferingUpdate(MediaPlayer mp, int bufferedPercent) {
      statusView.setText("MediaPlayer Buffering: " + bufferedPercent + "%");
      Log.v(LOGTAG, "MediaPlayer Buffering: " + bufferedPercent + "%");

No comments:

Post a Comment