Video summary for VideoView development and video Development

Source: Internet
Author: User

Video summary for VideoView development and video Development
I. Summary of VideoView and Its Related Components

In Android, you can play a video in two ways. The first method is to use MediaPlayer and SurfaceView to play the video. MediaPlayer is used to control the playback, pause, and progress of the video, surfaceView is used to display the video content. The second method is to use VideoView for playback. This class actually inherits the SurfaceView class and implements MediaController. mediaPlayerController is an interface used to control media playback. In addition, it also has a panel on VideoView for controlling media playback, including fast forward, fast return, play, pause button, and a progress bar. The advantage of using VideoView to play a video is that it has helped us implement SurfaceView and control methods. You only need to use it directly during the development process, but its disadvantage is that it is not flexible enough. Instead, if you use MediaPlayer and SurfaceView to play a video, you can customize the video more flexibly, but the disadvantage is that it is more difficult.

1.1 To play a video through VideoView, follow these steps:

1. Define the VideoView component in the interface layout file, or create a VideoView component in the program

2. Get the components in the layout file in the Activity and set the listener accordingly;

3. Call the following two methods of VideoView to load the specified video:

(1) setVideoPath (String path): loads the video represented by the path file.

(2) setVideoURI: loads the video corresponding to the URI

You must set the network video path before using the setVideoURI (URI uri) method.

4. Call the start (), pause (), resume (), and stopPlayback () Methods of VideoView to control video playback (start, pause, continue, and stop). getDuration () of VideoView () you can return the length of the video. You can use the VideoView's resume () method for memory playback and cache playback.

1.2 common VideoView Methods

The Android VideoView class provides a very convenient video playback API. The main methods include:

 

Method Name

Description

Void start ()

Start playing

Void stopPlayback ()

Stop playing

Void pause ()

Pause

Void resume ()

Replay

Void seekTo (int msec)

Starts playing in milliseconds.

Int getCurrentPosition ()

Obtains the current playback position.

Int getDuration ()

Obtains the total length of the current video.

Boolean isPlaying ()

Whether the current VideoView is playing a video

Void setVideoPath (String path)

Set the video source played by VideoView as the file path

Void setVideoURI (URI uri)

Set the video source as Uri, which can be a network Uri or a local Uri.

Setmediaco Troller (MediaController controller)

Set MediaController Controller

 

SetOnCompletionListener (MediaPlayer. onCompletionListener l)

Listen to events that have been played

SetOnErrorListener (MediaPlayer. OnErrorListener l)

Listen for events in case of playback errors

SetOnPreparedListener (MediaPlayer. OnPreparedListener l)

Listen to events completed by video Loading

 

1.3 Media Controller class

The Media Controller class provides a floating operation bar, which includes function keys such as playing, pausing, fast forward, fast forward, fast return, last, and next. You can also drag the progress bar to a specific place. Before use, VideoView and MeidaController must specify controls for each other.

 

The Mdedia Controller class contains the following built-in methods:

 

 

Method Name

Description

Boolean isShowing ()

Whether the current floating control bar is displayed.

Void setMediaPlayer (MediaController. MediaPlayerControl player)

Set control components

Void hide ()

Hide MeidaController

Void show ()

Show MeidaController

Void show (int timeout)

Set the display time of MidiaController in milliseconds. If it is set to 0, it is hidden until the hide () method is called.

Void setPrevNextListeners (View. OnClickListener next, View. OnClickListener prev)

Set the next video switching event of the previous video.

 

By default, the Media Controller is hidden after being suspended for 3 seconds, and the VideoView of the response is called out. The previous button is hidden by default.

1.4 video format1.4.1 video encoding formats supported by Android

1.4.2 video formats supported by VideoView

 

During the actual test, some Toast warnings sent by VideoView were found, with the content being written that the video format is not supported and cannot be played. VideoView can only play progressive streamable (progressive stream) movies. Generally, mp4 or 3gp videos that are converted to files in progressive streamable mode cannot be played by VideoView over the network, however, if it is stored on a local side (such as a storage card), this limit is not applied.

 

1.5 VideoView monitoring

There are three VideoView listeners used during development: setOnCompletionListener (MediaPlayer. onCompletionListener l), setOnErrorListener (MediaPlayer. OnErrorListener l), listener (MediaPlayer. OnPreparedListener l)

1.5.1SetOnCompletionListener (MediaPlayer. onCompletionListener l)

SetOnCompletionListener (MediaPlayer. onCompletionListener l) is used to listen to events after video playback is complete.

1.5.2SetOnErrorListener (MediaPlayer. OnErrorListener l)

 SetOnErrorListener (MediaPlayer. OnErrorListener l) is used to listen to various errors that occur during VideoView usage.

Error constant:

  • MEDIA_ERROR_IO
  • The file does not exist or is incorrect, or the network is inaccessible.
  • Value:-1004 (0xfffffc14)
  • MEDIA_ERROR_MALFORMED
  • The stream does not comply with the relevant standards or file encoding specifications
  • Value:-1007 (0xfffffc11)
  • MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK
  • Video Streams and their containers are not applicable to the continuous playback of video indicators (such as MOOV atoms) not at the beginning of the file.
  • Value: 200 (0x000000c8)
  • MEDIA_ERROR_SERVER_DIED
  • The Media Server has crashed. In this case, the program must release the MediaPlayer object and create a new one.
  • Value: 100 (0x00000064)
  • MEDIA_ERROR_TIMED_OUT
  • Some operations take a long time, that is, timeout, usually over 3-5 seconds.
  • Value:-110 (0xffffff92)
  • MEDIA_ERROR_UNKNOWN
  • Unknown error
  • Value: 1 (0x00000001)
  • MEDIA_ERROR_UNSUPPORTED
  • The bit stream meets the encoding standards or file specifications, but the media framework does not support this function.
  • Value:-1010 (0xfffffc0e)
  • What int: indicates the Error Type:
  • MEDIA_ERROR_UNKNOWN
  • MEDIA_ERROR_SERVER_DIED
  • Extra int: indicates the error type.
  • MEDIA_ERROR_IO
  • MEDIA_ERROR_MALFORMED
  • MEDIA_ERROR_UNSUPPORTED
  • MEDIA_ERROR_TIMED_OUT
  • MEDIA_ERROR_SYSTEM (-2147483648)-low-level system error.

1.5.3SetOnPreparedListener (MediaPlayer. OnPreparedListener l)

SetOnPreparedListener (MediaPlayer. OnPreparedListener l) is used to listen to events after a video is installed.

Ii. Video Rotation

I have found many methods about screen rotation on the Internet. In summary, there are four types. Before introducing it, you must first understand the Android screen rotation mechanism by default (of course, it can be implemented only when the screen rotation function of the mobile phone itself is enabled, during the development process, I found that the development video cannot be rotated along with the mobile phone. After several hours, I found that the rotation button of my Android app was not enabled ):

By default, when the gravity sensor is enabled, the orientation of the screen rotation will cause onDestory-> onCreate in the current activity, which will re-construct the current activity and interface layout, if it is on the Camera interface, it will be stuck or black screen for a period of time. If you want to support screen rotation well in the horizontal and vertical UI design, we recommend that you create the layout-land and layport folders in res, place the files on the horizontal and vertical screens in the corresponding layout folder.

2.1 AndroidMainfest. xml settings

If you only want to set a landscape or portrait screen, you only need to add the landscape code:

Advantage: Actiity does not recreate onCreate even if the screen is rotated.

Disadvantage: the screen has only one direction.

2.2 code dynamic settings

To dynamically change the Screen Settings, you only need to call the setRequestedOrientation () function in the Code:

Advantage: it can be set dynamically at will to meet people's requirements for changing the landscape and landscape screen UI and different design requirements.

Disadvantage: If you change the settings, the Activity will be destroyed and re-built, that is, re-onCreate;

2.3 override onCongigurationChanged

This method can be used to prevent the Activity from being constantly onCreate when the screen is rotated (in this case, the screen switching is often choppy ).

First, add configChanges in AndroidMainfest. xml:

Note: keyboardHidden indicates that the keyboard auxiliary function is hidden. If your development API level is equal to or higher than 13, you also need to set the screenSize because the screenSize will change when the screen is rotated.

Then, override the onConfigurationChanged method in the Activity. This method will be listened to when the screen rotation changes:

Advantage: You can monitor screen rotation changes at any time and perform corresponding operations

Disadvantage: it can only Rotate 90 ° at a time. If the rotation is 180 ° at a time, the onConfigurationChanged function will not be called.

2.4 Custom rotation listening settings with OrientationEventListener

This method is not summarized here because the custom interface is not clear for the moment.

Refer to the Website: http://www.jb51.net/article/64735.htm

Iii. incorrect return operation

To Prevent Users From mistakenly pressing the return key and exiting playback, you can rewrite it in the program.OnKeyDownMethod. In this method, the user clicks the return key. For example, the user only clicks the return key twice in a short time to exit playing.

Iv. Set full screen for mobile phones4.1 The most common method for setting full screen

 

@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // you can specify the value of requestWindowFeature (Window. FEATURE_NO_TITLE); // sets full screen getWindow (). setFlags (WindowManager. layoutParams. FLAG_FULLSCREEN, WindowManager. layoutParams. FLAG_FULLSCREEN); setContentView (R. layout. activity_main );}

 

Note: The requestWindowFeature () method must be prior to setContentView (); otherwise, an error is returned;

4.2 configure in AndroidManifest

 

You can use the built-in Theme provided by Android.

5. Set the screen display direction

 

When the screen is not defined, the app will change with the mobile phone's switching settings, and we must do something for the screen switching, you can do the following:

 

If (getResources (). getConfiguration (). orientation = ActivityInfo. SCREEN_ORIENTATION_PORTRAIT) {// process what needs to be done on the horizontal screen} else {// process what needs to be done on the portrait screen}

 

In this video software, the above code is used to enable the mobile phone to rotate by clicking a button:

 

6. How to cancel full screen

GetWindow (). clearFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN );

 

7. built-in Android Style

Android: theme = "@ android: style/Theme. Dialog" shows an Activity as a Dialog box.
Android: theme = "@ android: style/Theme. NoTitleBar" does not display the application title bar
Android: theme = "@ android: style/Theme. NoTitleBar. Fullscreen" does not display the application title bar, and is displayed in full screen.
Android: theme = "Theme. Light": white background
Android: theme = "Theme. Light. NoTitleBar" the white background does not have a title bar.
Android: theme = "Theme. Light. NoTitleBar. Fullscreen" white background, no title bar, full screen
Android: theme = "Theme. Black" background Black
Android: theme = "Theme. Black. NoTitleBar" the Black background does not have a title bar.
Android: theme = "Theme. Black. NoTitleBar. Fullscreen" Black background, no title bar, full screen
Android: theme = "Theme. Wallpaper" uses the system desktop as the background of the application
Android: theme = "Theme. Wallpaper. NoTitleBar" uses the system desktop as the background of the application without a title bar.
Android: theme = "Theme. Wallpaper. NoTitleBar. Fullscreen" uses the system desktop as the background of the application. It has no title bar and is full screen.
Android: theme = "Translucent" transparent background
Android: theme = "Theme. Translucent. NoTitleBar" transparent background with no title
Android: theme = "Theme. Translucent. NoTitleBar. Fullscreen" transparent background with no title, full screen
Android: theme = "Theme. Panel" Panel style display
Android: theme = "Theme. Light. Panel" flat display

VIII. Summary:

Because I have not been familiar with Android development for a long time, many Android development things have been forgotten. The specific methods in the Code are not summarized here, but described in another document.

 

 

 

 

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.