First, attach the effect after the program is running, as shown in:
One. Deploy a Web project to the Tomcat server:
1. This applet is a combination of the network to play a piece of music, first of all, we do a good Web project in the Tomcat installation directory under the WebApps directory, where the Web project named music, the directory structure is as follows:
Where our music is placed under the following directory:
2. Then we turned on the Tomcat 7.0 server, I installed tomcat in EXE format, so I have a Tomcat 7.0 service under the computer-management-services and applications-services, as shown in the following:
Note: If you install Tomcat in the format of a compressed package, you can open the Startup.bat batch file in the Bin directory after the Tomcat decompression directory to start the service.
3. After the tomcat server is turned on, the music Web project is deployed to the Tomcat server, and you can start writing our Android code now.
Two. Before we write the Android code, we need a small step, that is, how we can access the project that we deployed on the Tomcat server, in the Web project, we access a webpage to enter in the Address bar: http://localhost:8083/music/ Index.jsp this to visit the index.jsp page, where localhost can also be changed to 127.0.0.1, which represents the native, where 8083 is a port number, but there are some differences in the wirelessly access to a Web project, which have the following small steps:
1. First, you must know the IP address on our own computer, we can open the CMD Command Line window, enter the command ipconfig, as shown in:
Select the IPV4 address of the network you have connected to, and this IP address is required for the network connection between Android and Web projects.
2. Next, we have to know what the port number is on our Tomcat server, and my 8083, how do we look at the Tomcat port number, open the Server.xml file under the Conf directory in the Tomcat installation directory, and look at the lower number, as shown in:
If the port number is occupied, you can modify the port number by modifying the Server.xml file, which is the port value in the Red box in the field.
3. At this time, in our Android to access the files deployed on the Tomcat server in the Web project, you can use the following address: http://192.168.1.1:8083/BBS/ index.jsp, so you can access the Web project, where 192.168.1.1 is the native IP address, 8083 is the server's port number, where we can directly enter the browser http://192.168.1.1:8083/BBS/ index.jsp visit this index.jsp page.
Three. You can then write the Android code:
1. First, create a new Android project and open the Activity_main.xml layout file under the layout directory with the following code:
<linearlayout 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:orien tation= "vertical" > <relativelayout android:layout_width= "match_parent" android:layout_height= "44DP "Android:background=" @drawable/title_bar1 "> <imageview android:id=" @+id/finish " Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:onclick= "click" android:src= "@drawable/finish1"/> <textview android:id= "@+id/music_name" and Roid:layout_width= "Match_parent" android:layout_height= "wrap_content" android:gravity= "center" android:padding= "5DP" android:text= "@string/text" android:textsize= "23sp"/> </relati Velayout> <imageview Android:laYout_width= "Match_parent" android:layout_height= "wrap_content" android:layout_gravity= "center" Androi d:layout_weight= "1.05" android:src= "@drawable/img2"/> <relativelayout android:layout_width= "Match_ Parent "android:layout_height=" wrap_content "android:gravity=" Center_vertical|center_horizontal "> <button android:id= "@+id/play" android:layout_width= "Wrap_content" Android:layout_hei ght= "Wrap_content" android:onclick= "click" android:text= "@string/play"/> <button Android:id= "@+id/pause" android:layout_width= "wrap_content" android:layout_height= "Wrap_conten T "android:layout_torightof=" @id/play "android:onclick=" click "android:text=" @string/paus E "/> <button android:id=" @+id/stop "android:layout_width=" Wrap_content "and roid:layout_height= "Wrap_content "android:layout_torightof=" @id/pause "android:onclick=" click "android:text=" @str Ing/stop "/> </RelativeLayout></LinearLayout>
One of the ImageView (picture view) and the bottom three button (button) has a Android:onclick property, which is click, so the Mainactivity class is going to write a click () method, This method is triggered when the ImageView and button are clicked, thus reducing the amount of code, without using Findviewbyid and setting the event listener.
2. Where the text required for the Activity_main.xml layout file is placed in the Strings.xml file, the code is as follows:
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" > Play music over the network </ string> <string name= "Hello_world" >hello world!</string> <string name= "Menu_settings" >settings</string><string name= "text" > Golden Sea Heart-so proud </string><string name= "Play" > Play </ string><string name= "Pause" > Pause </string><string name= "Stop" > Stop </string><string name= "Exit" > Exit </string></resources>
3. Then attach the code for the Mainactivity.java file, as follows:
package com.example.android_intent_playmusic;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.view.keyevent;import Android.view.View;public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main);} public void Click (View v) {Intent Intent = new Intent (mainactivity.this, musicservice.class);//Instantiate a Intent object int op = -1;// Set intermediate variable Opswitch (V.getid ()) {case r.id.play://when clicked for Play button op = 1;break;case r.id.pause://When the pause button is clicked op = 2;break;case r.id.stop://When the Stop button is clicked op = 3;break;case r.id.finish://when clicked back to the Picture button finish ();//returns to the previous interface Break;default:break;} Bundle bundle = new bundle ();//Instantiate a Bundle object Bundle.putint ("MSG", op);//Put the OP value into the Bundle object Intent.putextras (bundle);// Then put the bundle object into the intent object StartService (intent);//Open the service}}
The switch case statement in the Click () method determines which button is clicked and passes the intermediate variable of op to the Musicservice service class, which opens the Musicservice service in mainactivity.
4. Next, attach Musicservice, which inherits the service class with the following code:
Package Com.example.android_intent_playmusic;import Java.io.ioexception;import Android.app.service;import Android.content.intent;import Android.media.mediaplayer;import Android.net.uri;import Android.os.Bundle;import Android.os.ibinder;public class Musicservice extends Service {private MediaPlayer player;// Declares a MediaPlayer object @overridepublic ibinder onbind (Intent arg0) {//TODO auto-generated method stub return null;} Create service @overridepublic void OnCreate () {//When the Player object is empty if (player = = null) {player = Mediaplayer.create (musicservice.this , Uri.parse ("Http://192.168.1.110:8083/music/music/music2.mp3"));//Instantiate the object by playing a music player.setlooping on the native server (False );//set does not loop playback}super.oncreate ();} Destroy service @overridepublic void OnDestroy () {//When the object is not empty if (player! = null) {player.stop ();//Stop playing player.release ();// Release resource player = null;//Set the Player object to Null}super.ondestroy ();} Start Service @overridepublic int Onstartcommand (Intent Intent, int flags, int startid) {//TODO auto-generated method stub bundle B = Intent.getext RAS ();//Gets the bundle object passed from the Mainactivity class int op = b.geTInt ("msg");//Retrieve the value of op in the Mainactivity Class switch (OP) {case 1://when OP is 1 o'clock, that is, play () when the play button is clicked,//Call Play () method Break;case 2://When OP is 2 o'clock, That is, pause () When the pause button is clicked,//Call the Pause () method Break;case 3://When OP is 3 o'clock, that is, stop () when the button is clicked, or//Call the Stop () method break;default:break;} Return Super.onstartcommand (Intent, flags, Startid);} Stop playing music method private void Stop () {//When the Player object is not empty if (player! = null) {player.seekto (0);//Set start from scratch Player.stop ();//Stop play try { Player.prepare ();//Pre-load music} catch (IllegalStateException e) {//TODO auto-generated catch block E.printstacktrace ();} catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();}}} Pause Music method private void Pause () {//When the Player object is playing and the player object is not empty if (player.isplaying () && player! = null) {player . pause ();//Pause Music}}//play Music method private void Play () {//When the Player object is not empty and the player is not playing if (player! = NULL &&!) Player.isplaying ()) {Player.start ();//Start Playing music}}}
This is done by the following code:
Player = Mediaplayer.create (Musicservice.this, Uri.parse ("<a target=_blank href=" Http://192.168.1.115:8083/music /music/music2.mp3 ">http://192.168.1.115:8083/music/music/music2.mp3</a>"));
This allows access to the mp3 file under the music project deployed on the Tomcat server.
5. Finally, don't forget two things, first, we have to declare this service in the Androidmanifest.xml file for the Musicservice class. Second, because we are accessing Web projects on the local server through the network, we must specify a permission in the Androidmanifest.xml file to allow access to network resources.
(1). Declare the service, put under the <application></application> tag, the code is as follows:
<service android:name= ". Musicservice "></service>
(2). This program requires access to network resources, so you need to specify permissions to allow access to network resources in the Androidmanifest.xml file, as follows:
<uses-permission android:name= "Android.permission.INTERNET"/>
Four. Run this Android project with the effect as above. Click the Play button to start playing the music, click the Pause button, pause the play, click the Play button again, continue to play, click the Stop button, stop playing, click the Play button again, start from the beginning, click on the back of the image to return to the previous page.
Where the service is not closed in the code above, you can call StopService (intent) If you want to stop the service, where intent refers to the intent object that is instantiated in the Mainactivity class.
Above code: Source download
Five. The above content is only for your study reference, write not good, please forgive me, if there are errors, please point out, thank you!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android plays a simple piece of music over the web