Example of playing video in Android

Source: Internet
Author: User

1 XML file name: Videoplayer

Main activity naming: videoactivity

Put the video file on the phone sdcard/ceshi.3gp note the video name and format

<?xml version= "1.0" encoding= "Utf-8"?>

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"

androidrientation= "Vertical"

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent" >

<textview

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content"

android:text= "@string/hello"

/>



<videoview

Android:id= "@+id/video_view"

Android:layout_width= "Fill_parent"

android:layout_height= "300px"

/>



<button android:id= "@+id/cmd_load"

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content"

android:text= "Load"

/>

<button android:id= "@+id/cmd_play"

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content"

Android:text= "This is a video"

/>

<button android:id= "@+id/cmd_pause"

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content"

android:text= "Pause"

/>



</LinearLayout>

2, Java code package com.eoeandroid.demo.VideoPlayer;



Import android.app.Activity;

Import Android.os.Bundle;

Import Android.util.Log;

Import Android.view.View;

Import Android.view.View.OnClickListener;

Import Android.widget.Button;

Import Android.widget.VideoView;



public class Videoactivity extends Activity {

private static final String TAG = "videoactivity";



public void OnCreate (Bundle icicle) {

Super.oncreate (Icicle);

Settitle ("videoactivity");

LOG.V (TAG, "onCreate: ===>");

Setcontentview (R.layout.videoplayer);

Final Videoview view = (videoview) Findviewbyid (R.id.video_view);



Button Cmdload = (button) This.findviewbyid (r.id.cmd_load);

Cmdload.setonclicklistener (New Onclicklistener () {

public void OnClick (View arg0) {

TODO auto-generated Method Stub

LOG.V (TAG, "Setvideopath: ===>");

View.setvideopath ("/sdcard/ceshiu.3gp");

}



});



Button Cmdplay = (button) This.findviewbyid (R.id.cmd_play);

Cmdplay.setonclicklistener (New Onclicklistener () {

public void OnClick (View arg0) {

TODO auto-generated Method Stub

LOG.V (TAG, "Start: ===>");

View.start ();

}

});



Button Cmdpause = (button) This.findviewbyid (R.id.cmd_pause);

Cmdpause.setonclicklistener (New Onclicklistener () {

public void OnClick (View arg0) {

TODO auto-generated Method Stub

LOG.V (TAG, "pause: ===>");

View.pause ();

}

});

}

}

Copy the code is also relatively simple that, get videoview, and then use a few buttons to demonstrate the load video and play, pause action.

It is important to note that:
The video file at the end is not too big, I used a 6M, loaded up can also.
Finally, the video file is put on the SD card, do not pack together.

On this basis, you can add some control buttons such as fast forward and rewind.

Example of playing video in Android

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.