Experiment 6 play audio and video in the app

Source: Internet
Author: User

Lab Report

Course Name

Mobile Internet development based on Android platform

Date of experiment

April 15

Name of the experimental project

Play audio and video in the app

Location of the experiment

S3002

Type of experiment

-Verification Type √ design type-comprehensive type

Hours

The purpose and requirements of the experiment (the knowledge points which are involved in this experiment and are required to be mastered)

Enables processing of audio and video in an application.

Requirements

1) Realize playback audio, audio playback control;

2) realize playback video, video playback control;

3) Use service services to play music from the project source file.

second, the experimental environment (the hardware equipment and related software used in this experiment)

(1) PC Machine

(2) Operating system: Windows XP

(3) Software: Eclipse, jdk1.6,android Sdk,adt

Third, the contents and steps of the experiment

1) New Project

2) Modify the layout file main.xml

3) Perfect activity class

/***

* This class is mainly used to test the function of calling the functions of the Andriod.media.MediaPlayer package to implement music playback.

* The music files in the resource files in the project, the files in the local file system, and the music on the network are played separately;

* video playback function, using the videoview control;

* The code specifies the path of the audio and video files in the local file system, before testing the program, please follow the path in the code and the name of the audio and video files to add files to the phone;

(4) Create a new service class that uses service services to play the music in the project source file, enabling the background to continue to play audio.

Four, the experimental results (the experimental source program list and operating results or experimental conclusions, experimental design drawings)

Code:

1.MainActivity

Package com.example.mediapiayer;

Import Java.io.File;

Import java.io.IOException;

Import Android.media.MediaPlayer;

Import Android.media.MediaPlayer.OnCompletionListener;

Import Android.net.Uri;

Import Android.os.Bundle;

Import android.os.Environment;

Import android.app.Activity;

Import android.content.Intent;

Import Android.view.Menu;

Import Android.view.View;

Import Android.view.View.OnClickListener;

Import Android.widget.Button;

Import Android.widget.TextView;

public class Mainactivity extends Activity {

Public MediaPlayer myPlayer1 = new MediaPlayer ();

Private Button bt1, BT2, BT3, Bt4, Bt5, BT6;

Private TextView MTextView1;

Private String uri = "http://bd.kuwo.cn/yinyue/2324240";

@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.activity_main);

MTextView1 = (TextView) Findviewbyid (R.ID.TEXTVIEW1);

BT1 = (Button) Findviewbyid (R.id.button1);

BT2 = (Button) Findviewbyid (R.id.button2);

BT3 = (Button) Findviewbyid (R.id.button3);

BT4 = (Button) Findviewbyid (R.ID.BUTTON4);

BT5 = (Button) Findviewbyid (R.ID.BUTTON5);

BT6 = (Button) Findviewbyid (R.ID.BUTTON6);

Bt1.setonclicklistener (New Onclicklistener () {

@Override

public void OnClick (View arg0) {

TODO auto-generated Method Stub

try {

if (myplayer1.isplaying () = = True) {

Myplayer1.reset ();

}

MyPlayer1 = Mediaplayer.create (Mainactivity.this,

R.raw.song);

Myplayer1.start ();

Mtextview1.settext (R.string.str_start);

} catch (IllegalStateException e) {

Mtextview1.settext (R.string.str_stop);

E.printstacktrace ();

}

}

});

Bt2.setonclicklistener (New Onclicklistener () {

String sdcard = Environment.getexternalstoragedirectory (). GetPath ();

@Override

public void OnClick (View arg0) {

TODO auto-generated Method Stub

try {

if (myplayer1.isplaying () = = True) {

Myplayer1.reset ();

}

Myplayer1.setdatasource (SDcard + file.separator

+ "F.I.R.-Yueyawan.mp3");

Myplayer1.prepare ();

Myplayer1.start ();

Mtextview1.settext (R.string.str_tishi);

} catch (IllegalStateException e) {

Mtextview1.settext (R.string.str_stop);

E.printstacktrace ();

} catch (IllegalArgumentException e) {

TODO auto-generated Catch block

E.printstacktrace ();

} catch (SecurityException e) {

TODO auto-generated Catch block

E.printstacktrace ();

} catch (IOException e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

}

});

Bt3.setonclicklistener (New Onclicklistener () {

@Override

public void OnClick (View arg0) {

TODO auto-generated Method Stub

Myplayer1.reset ();

try {

Myplayer1.setdatasource (URI);

}

catch (IllegalArgumentException e) {

TODO auto-generated Catch block

E.printstacktrace ();

} catch (SecurityException e) {

TODO auto-generated Catch block

E.printstacktrace ();

} catch (IOException e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

Intent Intent = new Intent (Intent.action_view, Uri.parse (Uri));

StartActivity (Intent);

Mtextview1.settext (R.string.str_wangluo);

}

});

Bt4.setonclicklistener (New Onclicklistener () {

@Override

public void OnClick (View arg0) {

TODO auto-generated Method Stub

Myplayer1.stop ();

Mtextview1.settext (r.string.str_finished);

}

});

Bt5.setonclicklistener (New Onclicklistener () {

@Override

public void OnClick (View arg0) {

TODO auto-generated Method Stub

}

});

Bt6.setonclicklistener (New Onclicklistener () {

@Override

public void OnClick (View arg0) {

TODO auto-generated Method Stub

Myplayer1.stop ();

Mtextview1.settext (R.string.str_jieshu);

}

});

}

@Override

public boolean Oncreateoptionsmenu (Menu menu) {

Inflate the menu; This adds items to the action bar if it is present.

Getmenuinflater (). Inflate (R.menu.main, menu);

return true;

}

}

2.activity_main.xml

<relativelayout xmlns: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"

android:paddingbottom="@dimen/activity_vertical_margin"

android:paddingleft="@dimen/activity_horizontal_margin"

android:paddingright="@dimen/activity_horizontal_margin"

android:paddingtop="@dimen/activity_vertical_margin"

android:background="@drawable/u311593540665946679fm15gp0"

Tools:context=". Mainactivity " >

<textview

android:id="@+id/textview1"

Android:layout_width="Wrap_content"

android:layout_height="Wrap_content"

android:layout_alignparenttop="true"

Android:layout_centerhorizontal="true"

Android:text = "Test multimedia playback" />

<button

android:id="@+id/button1"

Android:layout_width="Wrap_content"

android:layout_height="Wrap_content"

android:layout_alignleft="@+id/button2"

android:layout_alignright="@+id/button2"

android:layout_below="@+id/textview1"

android:layout_margintop="26DP"

Android:text = "Play music from the source file" />

<button

android:id="@+id/button3"

Android:layout_width="Wrap_content"

android:layout_height="Wrap_content"

android:layout_alignleft="@+id/button2"

android:layout_alignright="@+id/button2"

android:layout_below="@+id/button2"

Android:text = "Play Music on the network" />

<button

android:id="@+id/button2"

Android:layout_width="Wrap_content"

android:layout_height="Wrap_content"

android:layout_below="@+id/button1"

Android:layout_centerhorizontal="true"

Android:text = "Play music in the local file system" />

<button

android:id="@+id/button4"

Android:layout_width="Wrap_content"

android:layout_height="Wrap_content"

android:layout_alignbaseline="@+id/button5"

android:layout_alignbottom="@+id/button5"

android:layout_alignleft="@+id/button3"

Android:text = "Stop playing" />

<button

android:id="@+id/button5"

Android:layout_width="Wrap_content"

android:layout_height="Wrap_content"

android:layout_below="@+id/button3"

android:layout_torightof="@+id/button4"

Android:text = "Play video" />

<button

android:id="@+id/button6"

Android:layout_width="Wrap_content"

android:layout_height="Wrap_content"

android:layout_below="@+id/button3"

android:layout_torightof="@+id/button5"

Android:text = "Exit" />

</RelativeLayout>

Run Result: ()

1.

2.

Five, Experimental summary (analysis of the results of the experiment, experience and improvement of experimental ideas)

Through experiments, Android learns to control the playback of media files by invoking functions inside the Andriod.media.MediaPlayer package, enabling audio playback on selected files, local SD cards, and the network. Deepen your understanding of Android development.

Experimental reviews

Experimental results

instructing teachers to sign: year -month Day

Experiment 6 play audio and video in an application

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.