Android Aircraft Wars (eight) Add video files

Source: Internet
Author: User

To add a video file to your Android app, you'll need to add a folder called Raw (not scrambled) to the res to hold the video file you want to play


The code is as follows:

1. Add a videoview in the Lanyout

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Android:background= "#FFFFFF"
android:orientation= "Vertical" >

<videoview
Android:id= "@+id/video1"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"/>

</LinearLayout>




2. In the mainactivity:


Import android.app.Activity;
Import android.content.Intent;
Import Android.media.MediaPlayer;
Import Android.media.MediaPlayer.OnCompletionListener;
Import Android.net.Uri;
Import Android.os.Bundle;
Import android.view.KeyEvent;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.Toast;
Import Android.widget.VideoView;

public class Mainactivity extends Activity {


Private Videoview video;
Private Button button1;
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.begin_video);

video= (Videoview) Findviewbyid (R.ID.VIDEO1);
button1= (Button) Findviewbyid (R.id.button1);
Uri Uri=uri.parse ("android.resource://com.example.agame/" +r.raw.startvideo);//Get URI Video.setvideouri (URI); Specify the video to play

Video.requestfocus (); Let video get focus
try{
Video.start ();//Start video playback
}catch (Exception e) {
E.printstacktrace ();
}
Video.setoncompletionlistener (New Oncompletionlistener () {


public void Oncompletion (MediaPlayer arg0) {

Finish ();//End
}
});

}



So you can play the video!!!

This article is from the "Software Learning Summary" blog, be sure to keep this source http://bigcrab.blog.51cto.com/10626858/1685045

Android Aircraft Wars (eight) Add video files

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.