Course Name |
Mobile Internet development based on Android platform |
Date of experiment |
2015.4.16 |
Name of the experimental project |
Experiment 6 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) New Project 2) Modify the layout file Main.xml 3) Perfect Activity class |
Four, the experimental results (the experimental source program list and operating results or experimental conclusions, experimental design drawings) |
Code: Package Com.example.yemediapalyer;import Java.io.file;import android.media.mediaplayer;import Android.media.mediaplayer.oncompletionlistener;import Android.net.uri;import Android.os.Bundle;import Android.app.activity;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.imagebutton;import Android.widget.mediacontroller;import Android.widget.textview;import Android.widget.toast;import Android.widget.VideoView; Public classMainactivity extends Activity {//private Videoview video= (videoview) Findviewbyid (r.id.videoview1); PrivateMediaPlayer player;//MediaPlayer Object PrivateFile file;//the audio file to play PrivateTextView hint;//a text box that declares a message that displays a hint PrivateMediaPlayer MediaPlayer =NewMediaPlayer ();//private MediaPlayer Player1 = Mediaplayer.create (This, Uri.parse ("Http://www.mingribook.com/soung/bg.mp3"));@Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Final Button button1= (Button) Findviewbyid (R.id.button2);//Get the play button in SDFinal Button pausebtn = (Button) Findviewbyid (R.ID.PAUSEBTN);//Get the play button in the project//final Button button4 = (Button) Findviewbyid (r.id.button4);//Get play Video//final Button Button3 = (Button) Findviewbyid (r.id.button3);//Get the play button on the networkFinal Button stopbtn = (Button) Findviewbyid (R.ID.STOPBTN);//Get the Stop buttonhint = (TextView) Findviewbyid (r.id.hint);//Gets the text box for the user to display the prompt informationFile =NewFile ("/sdcard/ding.wav");//get the text to play in SDPausebtn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method StubMediaplayer=mediaplayer.create (mainactivity. This, R.raw.chimes); Mediaplayer.start (); Hint.settext ("playing music in engineering"); } }); if(File.exists ()) {MediaPlayer= Mediaplayer.create (mainactivity. This, Uri.parse (File.getabsolutepath ())); } Else{Hint.settext ("the file to be played does not exist! "); } if(File.exists ()) {//If the file existsPlayer =MediaPlayer. Create ( This, Uri.parse (File.getabsolutepath ()));//Create a MediaPlayer object}Else{Hint.settext ("The audio file you want to play does not exist! "); Button1.setenabled (false); return; } //to add a complete event listener for a MediaPlayer objectPlayer.setoncompletionlistener (NewOncompletionlistener () {@Override Public voidOncompletion (MediaPlayer MP) {Try{player.reset (); Player.setdatasource (File.getabsolutepath ()); //Reset the audio to playPlayer.prepare ();//Pre-load audioPlayer.start ();//Start PlaybackHint.settext ("playing audio in SD ..."); } Catch(Exception e) {e.printstacktrace ();//Output Exception Information}//Start playing music } }); Button1.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Try{player.reset (); Player.setdatasource (File.getabsolutepath ()); //Reset the audio to playPlayer.prepare ();//Pre-load audioPlayer.start ();//Start PlaybackHint.settext ("playing audio in SD ..."); } Catch(Exception e) {e.printstacktrace ();//Output Exception Information}//Start playing music } }); //Add a Click event listener for the play button//Button4.setonclicklistener (New Onclicklistener () {////@Override//Public void OnClick (View v) {//file File1=new file ("/sdcard/1.wmv"); //get the files to play on the SD card//mediacontroller mc=new Mediacontroller (mainactivity.this);//if (file1.exists ()) {//determine if the video file you want to play exists//Video.setvideopath (File1.getabsolutepath ()); //Specify the video to play//Video.setmediacontroller (MC); //setting Videoview associated with Mediacontroller//Video.requestfocus (); //let Videoview get the focus//try {//Video.start (); //start playing video//} catch (Exception e) {//E.printstacktrace (); //Output Exception Information// }// //Add complete event listener for Videoview//Video.setoncompletionlistener (New Oncompletionlistener () {// //@Override//Public void Oncompletion (MediaPlayer MP) {//Toast.maketext (mainactivity.this, "video playback is complete! ", Toast.length_short). Show (); //pop-up message box shows playback complete// }// });//}else{////Toast.maketext (This, "the video file to play does not exist", toast.length_short). Show (); Popup message prompt file does not exist// }}// });//Button3.setonclicklistener (New Onclicklistener () {////@Override//Public void OnClick (View v) {//try {//Player1.reset ();//Player1.setdatasource (File.getabsolutepath ());//Reset the audio to play//Player1.prepare ();//Pre-load audio//Player1.start ();//Start Playback//Hint.settext ("Playing audio in Network ...");//} catch (Exception e) {//e.printstacktrace ();//Output Exception Information// }//Start playing music//// }// }); //Add a Click event listener for the stop buttonStopbtn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {player.stop ();//stop playing;Mediaplayer.pause (); Hint.settext ("Stop playing audio ..."); } }); } @Overrideprotected voidOnDestroy () {if(Player.isplaying ()) {player.stop ();//stop playback of audio} player.release (); //Freeing ResourcesSuper.ondestroy (); }} Run: |
Five, Experimental summary (analysis of the results of the experiment, experience and improvement of experimental ideas) |
The experiment learned how to play video and music in the Android project, knowing that many of the animated animations can be displayed directly in the video. |
Experimental reviews |
|
Experimental results |
|
Guide Teacher Signature: Date |
|
|
|
|
|
|