Experiment six play audio and video in the app

Source: Internet
Author: User

Course Name

Mobile Internet development based on Android platform

Date of experiment

2016-4-21

Name of the experimental project

Play audio and video in the app

Location of the experiment

S3010

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)

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. Import Project

2. Add Tmediaplayer, MyService, Video class, and MediaPlayer, video layout files.

3. Improve the activity class.

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

Code:

One

1. Play music from the local file system

Musicbutton.setonclicklistener (new Onclicklistener () {

@Override

Public void OnClick (View arg0) {

TODO auto-generated Method stub

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

Try {

Mediaplayer.setdatasource (sdcard+file. Separator+ "Music.mp3");

} catch (IllegalArgumentException e) {

TODO auto-generated Catch block

E.printstacktrace ();

} catch (SecurityException e) {

TODO auto-generated Catch block

E.printstacktrace ();

} catch (IllegalStateException e) {

TODO auto-generated Catch block

E.printstacktrace ();

} catch (IOException e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

Try {

Mediaplayer.prepare ();

} catch (IllegalStateException e) {

TODO auto-generated Catch block

E.printstacktrace ();

} catch (IOException e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

Mediaplayer.start ();

Toast. Maketext (Tmediaplayer. This, "playing music in the local file system", Toast. length_short). Show ();

}

});

2. Play music from the project source file using service services

Public class MyService extends Service {

Private MediaPlayer MP;

@Override

Public IBinder Onbind (Intent arg0) {

TODO auto-generated Method stub

return null;

}

@Override

Public void onCreate () {

Super. OnCreate ();

Mp=mediaplayer. Create (this, R.raw. AA);

Log. I ("Aaaaaaaaa", "Musicservice OnCreate is run");

}

Public int Onstartcommand (Intent Intent, int flags, int startid) {

TODO auto-generated Method stub

Mp.start ();

Log. I ("Gggggggggggg", "Musicservice Onstartcommand is run");

Toast. Maketext (MyService. This, "playing music from source file ~", Toast. length_short). Show ();

return Super. Onstartcommand (Intent, flags, Startid);

}

3. Play Video

File file=new file ("/sdcard/movies/shudu.mp4");

Log. I ("VVVVVVVVVVVVV", "33333333333");

mccontroller=New Mediacontroller (Video. this);

if (File.exists ()) {

Videoview.setvideopath (File.getabsolutepath ());

Videoview.setmediacontroller (Mccontroller);

Videoview.requestfocus ();

Try {

Videoview.start ();

}catch(Exception e) {

E.printstacktrace ();

}

Log. I ("Ffffffff", "33333333333");

Videoview.setoncompletionlistener (new Oncompletionlistener () {

@Override

Public void oncompletion (MediaPlayer arg0) {

TODO auto-generated Method stub

}

});

4. Stop Music playback

Pausebtn.setonclicklistener (new Onclicklistener () {

@Override

Public void OnClick (View arg0) {

TODO auto-generated Method stub

StopService (Intent);

Mediaplayer.stop ();

Toast. Maketext (Tmediaplayer. This, "Stop playing", Toast. length_short). Show ();

}

});

5. Exit

Exitbtn.setonclicklistener (new Onclicklistener () {

@Override

Public void OnClick (View arg0) {

TODO auto-generated Method stub

StopService (Intent);

Intent.setclass (Tmediaplayer. this, loginactivity. class);

StartActivity (Intent);

}

});

Run Result: ()

First, play local file music

Second, play the source file music

Third, stop playing music

Iv. playing video

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

Through this experiment, basically understand the way to play audio and video, but the process encountered a lot of problems. For example, creating a class with the same name as the definition object interferes with the recognition of the object and directly errors. Also video playback failed to implement, after clicking the Play Video button, jump to play the video after the page, pop "can ' t play this video" error, has not been able to find the reason.

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.