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 21, 2016

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

2

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

  1. Enables processing of audio and video in an application.
  2. Realize playback audio, audio playback control;
  3. Realize playback video, video playback control;
  4. Use a service service 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:

Mainactivity:

 PackageCom.example.mediaplayer;ImportJava.io.File;Importjava.io.IOException;ImportAndroid.media.MediaPlayer;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.annotation.SuppressLint;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.util.Log;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.MediaController;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast;ImportAndroid.widget.VideoView; Public classMainactivityextendsActivity {Button source,local,network,stop,video,exit; PrivateMediaPlayer mediaplayer=NewMediaPlayer (); Privatefile file; PrivateTextView TV; PrivateString uri= "http://y.qq.com/#type =song&mid=003e0ozz0wfbrx&tpl=yqq_song_detail"; PrivateVideoview Videoview;    Mediacontroller MC; protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); TV=(TextView) Findviewbyid (R.ID.TEXTVIEW1); SOURCE=(Button) Findviewbyid (R.id.button1); Local=(Button) Findviewbyid (R.id.button2); Network=(Button) Findviewbyid (R.id.button3); Stop=(Button) Findviewbyid (R.ID.BUTTON4); Video=(Button) Findviewbyid (R.ID.BUTTON5); Videoview=(Videoview) Findviewbyid (R.ID.VIDEOVIEW1); File File=NewFile ("/sdcard/bluevird.mp4"); MC=NewMediacontroller (mainactivity. This); Source.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View arg0) {//TODO auto-generated Method StubMediaplayer=mediaplayer.create (mainactivity. This, R.raw.what);                Mediaplayer.start (); Toast.maketext (mainactivity. This, "Playing the music", Toast.length_short). Show ();         }         }); Stop.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View arg0) {//TODO auto-generated Method Stub                if(Mediaplayer.isplaying ()) {mediaplayer.stop (); Toast.maketext (mainactivity. This, "Stop", Toast.length_short). Show ();         }            }         }); Local.setonclicklistener (NewOnclicklistener () {@SuppressLint ("Sdcardpath") @Override Public voidOnClick (View arg0) {//TODO auto-generated Method Stub                 Try{mediaplayer.reset (); Mediaplayer.setdatasource ("/sdcard/louis armstrong-what A Wonderful World.mp3");                    Mediaplayer.prepare ();                    Mediaplayer.start (); Toast.maketext (mainactivity. This, "Playing the SDcard music", Toast.length_short). Show (); } Catch(IllegalStateException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace ();          }               }          }); Network.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method StubMediaplayer.reset (); Try{Mediaplayer.setdatasource (URI); }Catch(IllegalStateException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } toast.maketext (mainactivity. This, "Network Music", Toast.length_short). Show (); Intent Intent=NewIntent (Intent.action_view,uri.parse (Uri));          StartActivity (Intent);          }                       }); Video.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method StubFile file=NewFile ("/sdcard/bluebird.mp4");                Videoview.setvideopath (File.getabsolutepath ());                Videoview.setmediacontroller (MC); Try{Videoview.start (); } Catch(Exception e) {e.printstacktrace (); } toast.maketext (mainactivity. This, "Playing the video", Toast.length_short). Show ();             }        }); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }    }

Main layout:

<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"Tools:context=". Mainactivity "> <TextView Android:id= "@+id/textview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_centerhorizontal= "true"Android:text= "Mediaplayertest"android:textsize= "35px"/> <Button Android:id= "@+id/button1"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_below= "@+id/textview1"Android:layout_centerhorizontal= "true"Android:text= "source file Music"/> <Button Android:id= "@+id/button2"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_alignright= "@+id/button1"Android:layout_below= "@+id/button1"Android:text= "Local Music"/> <Button Android:id= "@+id/button3"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_alignleft= "@+id/button2"Android:layout_below= "@+id/button2"Android:text= "Network Music"/> <Button Android:id= "@+id/button4"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_alignleft= "@+id/button3"Android:layout_below= "@+id/button3"Android:text= "Stop"/> <Button Android:id= "@+id/button5"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_alignright= "@+id/button4"Android:layout_below= "@+id/button4"Android:text= "Video"/> <Videoview Android:id= "@+id/videoview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentright= "true"Android:layout_below= "@+id/button5"/></relativelayout>

Run Result: ()

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

This experiment found out how to put the file into the SD card in the simulator and how to use it from the SD card.

Experiment 6 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.