Android video playback

Source: Internet
Author: User
Tags file url

Today, the content is to add a video to the game for playing the opening company logo.

Requirements:

1. Do not display the player-type "start", "pause", "Fast forward", and "Quick Return" buttons.

2. After playing the video, the event listener must remove the video.

First, you can play videos in Android in the 3GP and MP4 formats by default. If you want to support videos in other formats, you must decode files in other formats.

Because I am not a player, I only need to play the video at the beginning of the game, so here I use MP4 format.

Then the API options include mediaplayer and videoview.

Use mediaplayer to write the response code by yourself. If you are familiar with mediaplayer, it is a little more complicated.

Videoview is a view encapsulated by Android. It inherits from surfaceview and implements the mediaplayercontrol interface.

I thought for a moment and did not hesitate to select videoview.

First, rewrite videoview because it is not full screen by default. I need full screen

package ss.ss;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.VideoView;

public class MyVideoView extends VideoView {
public static int WIDTH;
public static int HEIGHT;
public MyVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = getDefaultSize(WIDTH, widthMeasureSpec);

int height = getDefaultSize(HEIGHT, heightMeasureSpec);

setMeasuredDimension(width,height);

}
}

XML code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ss.ss.MyVideoView
android:id="@+id/surface_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

</LinearLayout>

Note that

The setting of fill_parent or wrap_content is not affected. It will not enable full-screen video playback because you set it to fill_parent. If you are interested, you can check videoview. onmeasure () source code, video size from

This method controls the display according to the proportion, so I need to overwrite videoview. onmeasure ();

Then let's take a look at the activity code:

/*
* Copyright (c) 2009 the android open source project
*
* Licensed under the Apache license, version 2.0 (the "License ");
* You may not use this file before t in compliance with the license.
* You may obtain a copy of the license
*
* Http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* Distributed under the license is distributed on an "as is" basis,
* Without warranties or conditions of any kind, either express or implied.
* See the license for the specific language governing permissions and
* Limitations under the license.
*/
Package ss. SS;



Import Android. App. activity;
Import Android. Media. mediaplayer;
Import Android. Media. mediaplayer. oncompletionlistener;
Import android.net. Uri;
Import Android. OS. Bundle;
Import Android. util. displaymetrics;
Import Android. View. window;
Import Android. View. windowmanager;
Import Android. widget. Toast;

Public class videoviewdemo extends activity implements oncompletionlistener {

/**
* Todo: Set the PATH variable to a streaming video URL or a local media
* File path.
*/
Private string Path = "/sdcard/main.mp4 ";
Private myvideoview mvideoview;

@ Override
Public void oncreate (bundle icicle ){
Super. oncreate (icicle );
Requestwindowfeature (window. feature_no_title );
Getwindow (). addflags (windowmanager. layoutparams. flag_fullscreen );
Setcontentview (R. layout. videoview );
Mvideoview = (myvideoview) findviewbyid (R. Id. surface_view );

Mvideoview. setoncompletionlistener (this );
Displaymetrics dm = new displaymetrics ();
This. getwindowmanager (). getdefaultdisplay (). getmetrics (DM );
Myvideoview. width = DM. widthpixels;
Myvideoview. Height = DM. heightpixels;
If (Path = ""){
// Tell the user to provide a Media File URL/path.
Toast. maketext (
Videoviewdemo. This,
"Please edit videoviewdemo activity, and set path"
+ "Variable to your Media File URL/path ",
Toast. length_long). Show ();

} Else {

/*
* Alternatively, for streaming media you can use
* Mvideoview. setvideouri (URI. parse (urlstring ));
*/
// Mvideoview. setvideopath (PATH );
Mvideoview. setvideouri (URI. parse ("android. Resource: // ss. SS/" + R. Raw. Main ));

// Uncomment the comment if you need to add a control bar
// Mvideoview. setmediacontroller (New mediacontroller (this ));
// The playback code for testing automatic playback in 1.6 does not need to be. Start ()
// Test the playback code of automatic playback in 2.1
// Mvideoview. Start ();
// For compatibility, choose mvideoview. Start () to ensure that all versions are automatically played at the start.
Mvideoview. Start ();
}
}
@ Override
Protected void ondestroy (){
Super. ondestroy ();
System. Exit (0 );
}
@ Override
Public void oncompletion (mediaplayer MP ){
System. Out. println ("playback completed ");
}

}

 

 

Most of these are the source code in the API demo, which has been slightly modified.

1. to play a video in a project resource, use mvideoview. setvideouri (URI. parse ("android. resource: // ss. SS/"+ R. raw. main ));

2. If you need to play the files in the SD card, please refer to the code above

Private string Path = "/sdcard/main.mp4 ";

// Mvideoview. setvideopath (PATH );

3. event processing after playback is complete

@ Override
Public void oncompletion (mediaplayer MP ){
System. Out. println ("playback completed ");
}

Implement implements oncompletionlistener and set mvideoview. setoncompletionlistener (this );

4. Transfer the resolution of the Bar Device to videoview for devices with different resolutions

Summary:

I encountered the following problems when playing a video:

1. The onmeasure

2. Some versions will play automatically, and some will not. For uniformity, use videoview. Start (). Because you do not set videoview. Start () in 1.6, it will also play automatically, but it must be specified in the 2.1 test.

 

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.