Android Video Player (i): Play video with Videoview

Source: Internet
Author: User

First, Introduction

As Android development, we inevitably will be exposed to video playback, videoview as the simplest player, we should not be.

Here is a brief introduction to Videoview:

    • Videoview is the use of MediaPlayer to control video files.
    • Videoview only supports MP4, AVI, 3GP format video, support format is relatively single, Videoview supported format can refer to MediaPlayer.
    • Videoview can play network video, the Protocol for the supported network Video is: HTTP protocol and RTSP protocol two.
Ii. Common methods of Videoview

The Android Videoview class provides us with a very handy video playback API with the following main methods:

    • Setvideopath: Set the location of the video file to play
    • Start: Starts or resumes playing the video
    • Pause: Pauses video playback
    • Resume: Play the video from the beginning
    • Seekto: Start video playback from the specified position
    • IsPlaying: Determine if the video is currently playing
    • GetCurrentPosition: Gets the current playing position
    • Getduration: Gets the length of the loaded video file
    • Setvideopath (String Path): Sets the Videoview playback video source as a file path
    • Setvideouri (URI uri): Sets the video source as a URI, either a network URI or a local URI
    • Setmediacontroller (Mediacontroller Controller): Set up Mediacontroller controllers
    • Setoncompletionlistener (Mediaplayer.oncompletionlistener L): Listen for events that have finished playing
    • Setonerrorlistener (Mediaplayer.onerrorlistener L): Listen for events when an error occurs
    • Setonpreparedlistener (Mediaplayer.onpreparedlistener L): Monitor video loading completed events

In addition to simple operations and rich APIs, we do not need to encode when loading video, and we do not need to manually recycle resources after playback.

In addition to playing the video, the Media controller class provides us with a suspended action bar that includes play, pause, fast forward, rewind, previous, and next function keys. Even dragging the progress bar to a certain point of playback has been implemented. Before using Videoview and Medicontroller, you need to specify the controls with each other. The built-in methods are as follows:

    • boolean isShowing:当前悬浮控制栏是否显示
    • setMediaPlayer(MediaController.MediaPlayerControl player):设置控制的组件
    • setPrevNextListeners(View.OnClickListener next,View.OnClickListener prev):设置上一个视频、下一个视频的切换事件。

By default, the Media controller hovers over 3s and hides, touching the videoview of the response. By default, the Next button is hidden.

third, the use of Videoview to achieve video playback

Below we will show how to use Videoview to play the video stream of Hong Kong satellite TV:

1. 声明权限

需要在AndroidManifest.xml添加两条权限。

<uses-permission android:name="android.permission.INTERNET" />< Uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"

Two permissions are: Network access rights, access to the file system permissions.

2. Writing videoview layouts
<?XML version= "1.0" encoding= "Utf-8"?><Android.support.constraint.ConstraintLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context=". Mainactivity ">    <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content">        <VideoviewAndroid:id= "@+id/video_view"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" />    </LinearLayout></Android.support.constraint.ConstraintLayout>
3. Writing playback Video code
Videoview Videoview = Findviewbyid (r.id.video_view); Videoview.setvideopath ("http://live.hkstv.hk.lxdns.com/live/hks/playlist.m3u8");   New Mediacontroller (this); Videoview.setmediacontroller (Mediacontroller); Mediacontroller.setmediaplayer (videoview);
4.

After completing the code writing work, launch the app should be able to be like the video content of Hong Kong TV. is not feeling very simple ah, that is because Videoview encapsulated MediaPlayer and Surfaceview, all the life cycle is packaged, so the use of course is simple. Let's analyze videoview to understand what's behind it.

Iv. Videoview Playback Status Description

The following code is the state of the player declaration declared by Videoview:

//All possible internal statesPrivate Static Final intState_error =-1; Private Static Final intState_idle = 0; Private Static Final intstate_preparing = 1; Private Static Final intstate_prepared = 2; Private Static Final intstate_playing = 3;Private Static Final intstate_paused = 4;Private Static Final intstate_playback_completed = 5;

Why declare these states, this will have to mention the life cycle of MediaPlayer, see, should be a little understanding of it:

Here we do not specifically for this diagram on the MediaPlayer life cycle of content. Just to prove that the life of the Videoview state is based.

Private int mcurrentstate = state_idle; Private int mtargetstate = state_idle;

The above code declares two state variables, namely the current state and the target State. With these two variables combined with the constants mentioned above, we can clearly know that the Videoview class is around these two states to do the corresponding operation. And we're dealing with some callbacks that are designed based on the changes between these states, such as:

    • Oncompletionlistener--state_playback_completed
    • Onpreparedlistener--state_prepared
    • Onerrorlistener--State_error
V. Possible problems and solutions for using Videoview 1. Video playback will have a short black screen time: the cause:

It takes time for video files to load into memory, and this time may import videoview All Black.

Workaround:

(1): Add Mediaplayer.onpreparedlistener Listener event to Videoview, play video in its onprepared (MediaPlayer MP) method callback this time the video has finished loading.
(2): When the Videoview executes the start () method The preview of the video does not disappear immediately but disappears after a delay of hundreds of milliseconds, this is the video has been loaded, of course, some bad experience.

2. Jump to another interface back to the video playback area black screen: Cause:

Videoview was recycled, and he didn't do videoview. Save processing

Workaround:

Handle Videoview video playback and pauses in the activity or fragment life cycle where Videoview is located. (View Videoview's life cycle related methods)

Android Video Player (i): Play video with Videoview

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.