Monday, March 11, 2019

Android开发笔记-ch3.4.9.3 Developing Android streaming Apps


3.4.9.3 Developing Android streaming Apps

Ref YouTube Android Player API. Developer Guide mentioned video file formats supported include 3GP, MP4, WEBM, and MKV, depending on the format used and on which platform level the user has installed. tutsplus tutorial for developing streaming app is using VideoView and MediaPlayer classes. Refer to this SO, VideoView is a wrapper of MediaPlayer, SurfaceView and can attach a MediaController. As described in the tutorial, the simple playback a video stream from archive is like this: create a simple android app, add VideoView to the layout, and add this code in OnCreate():
VideoView vidView = (VideoView)findViewById(R.id.myVideo); //myVideo is the VideoView ID
String vidAddress = "https://archive.org/download/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4";
Uri vidUri = Uri.parse(vidAddress);
vidView.setVideoURI(vidUri);
vidView.start();
To add a playback control, just add 3 more lines before vidView.start() call:
MediaController vidControl = new MediaController(this);
vidControl.setAnchorView(vidView);
vidView.setMediaController(vidControl);
As mentioned in the tutor, alternatively, using MediaPlayer directly, also refer to Media player guide: to keep the screen from dimming or the processor from sleeping, uses the MediaPlayer.setScreenOnWhilePlaying() or MediaPlayer.setWakeMode() methods, you must request this permission. <uses-permission android:name="android.permission.WAKE_LOCK" />
Playing from a remote URL via HTTP streaming looks like this:
String url = "http://........"; // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();
 
The tutor code of MediaPlayer is using SurfaceView. Note the state is important, doing a pause when play not started or doing start while not prepared will lead to error Mediaplayer error (-38, 0) which means wrong state. For other Mediaplayer error, refer to MediaPlayer.
I also noticed doing a release after reset will lead to exception. Common error/code as below table:
MEDIA_ERROR_IO
File or network related operation errors.
-1004
MEDIA_ERROR_MALFORMED
Bitstream is not conforming to the related coding standard or file spec
-1007
MEDIA_ERROR_SERVER_DIED
Media server died. In this case, the application must release the MediaPlayer object and instantiate a new one.
100
MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK
The video is streamed and its container is not valid for progressive playback i.e the video's index (e.g moov atom) is not at the start of the file.
200
MEDIA_ERROR_TIMED_OUT
Some operation takes too long to complete, usually more than 3-5 seconds
-110
MEDIA_ERROR_UNKNOWN
Unspecified media player error
1
MEDIA_ERROR_UNSUPPORTED
Bitstream is conforming to the related coding standard or file spec, but the media framework does not support the feature
-1010
MEDIA_INFO_VIDEO_RENDERING_START
The player just pushed the very first video frame for rendering
3
MEDIA_INFO_BAD_INTERLEAVING
Bad interleaving means that a media has been improperly interleaved or not interleaved at all, e.g has all the video samples first then all the audio ones. Video is playing but a lot of disk seeks may be happening.
800
MEDIA_INFO_NOT_SEEKABLE
The media cannot be seeked (e.g live stream)
801
MEDIA_INFO_METADATA_UPDATE
A new set of metadata is available
802






MEDIA_INFO_AUDIO_NOT_PLAYING
Informs that audio is not playing. Note that playback of the video is not interrupted
804
MEDIA_INFO_VIDEO_NOT_PLAYING
Informs that video is not playing. Note that playback of the audio is not interrupted
805
MEDIA_INFO_VIDEO_TRACK_LAGGING
he video is too complex for the decoder: it can't decode frames fast enough. Possibly only the audio plays fine at this stage
700
MEDIA_INFO_BUFFERING_START
MediaPlayer is temporarily pausing playback internally in order to buffer more data.
701
MEDIA_INFO_BUFFERING_END
MediaPlayer is resuming playback after filling buffers.
702
MEDIA_INFO_SUBTITLE_TIMED_OUT
Reading the subtitle track takes too long
902
For VideoView, here is another good example code. There is some discussion about pause and resume, refer to this SO: several suggest:
public void onPause() {
    Log.d(TAG, "onPause called");
    super.onPause();
    stopPosition = videoView.getCurrentPosition(); //stopPosition is an int
    videoView.pause();
}
public void onResume() {
    super.onResume();
    Log.d(TAG, "onResume called");
    videoView.seekTo(stopPosition);
    videoView.start();
}
One suggests not do videoView.pause() and videoView.start(), instead, get the wrapped mediaplayer mp, and do mp.pause() and mp.start()
A good explanation of accurate seek. videoView.resume() works after videoView.pause(), but not after videoView.seekTo().

0 Comments:

Post a Comment