Experiment six 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, 2016

Name of the experimental project

Play audio and video in the app

Location of the experiment

S30010

Type of experiment

-Verification Type √ design type-comprehensive type

Hours

2

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

1. Implement audio and video processing in the application.

2. Realize playback audio, audio playback control;

3. Realize playback video, video playback control;

4. 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. Create a new project named MediaPlayer

2. Modify the layout file Main.xml

3. Improve Activity Class

4. Create a new service class that uses the service service 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:

Mainactivity.java class:

Public class Mainactivity extends Activity {

Private MediaPlayer mediaplayer=New MediaPlayer ();

Private TextView TView;

Private Button startsourcebtn,startlocalbtn,startinternetbtn,startvideobtn,stopbtn,pausebtn,exitbtn;

Private File file;

@Override

protected void onCreate (Bundle savedinstancestate) {

Super. OnCreate (savedinstancestate);

Setcontentview (r.layout. Activity_main);

tview= (TextView) Findviewbyid (r.id. TextView1);

Startsourcebtn= (Button) Findviewbyid (r.id. Startsourcebtn);

Startlocalbtn= (Button) Findviewbyid (r.id. Startlocalbtn);

Startinternetbtn= (Button) Findviewbyid (r.id. Startinternetbtn);

Startvideobtn= (Button) Findviewbyid (r.id. Startvidiobtn);

Stopbtn= (Button) Findviewbyid (r.id. Stopbtn);

Pausebtn= (Button) Findviewbyid (r.id. Pausebtn);

Exitbtn= (Button) Findviewbyid (r.id. Exitbtn);

file=New File (environment. getExternalStorageDirectory(). GetPath () + "/nan.mp3");

if (File.exists ()) {

Mediaplayer=mediaplayer. Create (Mainactivity. this, Uri. Parse (File.getabsolutepath ()));

}Else{

Toast. Maketext (Mainactivity. This, "The file does not exist!" ", Toast. Length_long). Show ();

}

Startsourcebtn.setonclicklistener (new Onclicklistener () {

@Override

Public void OnClick (View arg0) {

TODO auto-generated Method stub

Mediaplayer=mediaplayer. Create (Mainactivity. this, R.raw. nan);

Mediaplayer.start ();

}

});

Startlocalbtn.setonclicklistener (new Onclicklistener () {

@Override

Public void OnClick (View arg0) {

TODO auto-generated Method stub

Mediaplayer.reset ();

Try {

Mediaplayer.setdatasource (File.getabsolutepath ());

Mediaplayer.prepare ();

Mediaplayer.start ();

}catch (IllegalArgumentException e) {

TODO: Handle exception

E.printstacktrace ();

}catch (SecurityException e) {

TODO: Handle exception

E.printstacktrace ();

}catch (IllegalStateException e) {

TODO: Handle exception

E.printstacktrace ();

}catch (IOException e) {

TODO: Handle exception

E.printstacktrace ();

}

}

});

Startinternetbtn.setonclicklistener (new Onclicklistener () {

@Override

Public void OnClick (View arg0) {

TODO auto-generated Method stub

}

});

Startvideobtn.setonclicklistener (new Onclicklistener () {

@Override

Public void OnClick (View arg0) {

TODO auto-generated Method stub

Intent intent=New Intent ();

Intent.setclass (mainactivity. this, videoactivity. class);

StartActivity (Intent);

}

}); Stopbtn.setonclicklistener (new Onclicklistener () {

@Override

Public void OnClick (View arg0) {

TODO auto-generated Method stub

if (Mediaplayer.isplaying ()) {

Mediaplayer.stop ();

}

}

});

Pausebtn.setonclicklistener (new Onclicklistener () {

@Override

Public void OnClick (View arg0) {

TODO auto-generated Method stub

if (Mediaplayer.isplaying ()) {

Mediaplayer.pause ();

}Else {

Mediaplayer.start ();

}

}

});

Exitbtn.setonclicklistener (new Onclicklistener () {

@Override

Public void OnClick (View arg0) {

TODO auto-generated Method stub

if (Mediaplayer.isplaying ()) {

Mediaplayer.release ();

}

}

});

}

@Override

protected void OnDestroy () {

TODO auto-generated Method stub

if (Mediaplayer.isplaying ()) {

Mediaplayer.stop ();

}

Mediaplayer.release ();

Super. OnDestroy ();

}

@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;

}

}

Videoactivity,java class:

Public class Videoactivity extends activity{

Private Videoview video;

Private File file;

@Override

protected void onCreate (Bundle savedinstancestate) {

TODO auto-generated Method stub

Super. OnCreate (savedinstancestate);

Setcontentview (r.layout. Video);

video= (Videoview) Findviewbyid (r.id. Video);

file=New File ("/");

Mediacontroller mc=New Mediacontroller (videoactivity. this);

if (File.exists ()) {

Video.setvideopath (File.getabsolutepath ());

Video. Setmediacontroller (MC);

Video.requestfocus ();

Try {

Video.start ();

}catch (Exception e) {

E.printstacktrace ();

}

Video.setoncompletionlistener (new Oncompletionlistener () {

@Override

Public void oncompletion (MediaPlayer arg0) {

TODO auto-generated Method stub

Toast. Maketext (Videoactivity. This, "video playback finished!" ", Toast. length_short). Show ();

}

});

}Else{

Toast. Maketext (This, "the video to be played does not exist!") ", Toast. length_short). Show ();

}

}

}

Run Result: ()

Run the initial interface: Play the video:

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

The experimental process is more painful, because do not know why, in the open DDNS found storage This folder, the project re-import, found in the open DDNS can find storage this folder. This is the time to import audio files and run programs that can play music from the source files and the local file system. I can't play the video and I don't know where the problem appears.

Experimental reviews

Experimental results

Guide Teacher Signature: Date

Experiment six play audio and video in the app

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.