Android Play a simple music on the Internet and android
First, attach the effect after the program is run, as shown in:
1. Deploy a web project to the tomcat server:
1. this applet is used to play a piece of music through the network. First, place a well-developed web project under the webapps directory under the tomcat installation directory. The web project is named music, the directory structure is as follows:
In this example, our music is stored in the following directory:
2. then we start the tomcat 7.0 server. The tomcat I installed is in the exe format, so I have a tomcat 7.0 Service under the computer-management-service and application-service, as shown in figure:
Note: If you install tomcat in a compressed package format, you can open the startup. bat batch file in the bin directory after extracting tomcat to enable this service.
3. After the tomcat server is enabled, deploy the music web project to the tomcat server. Now, you can write our android code.
II. before writing Android code, we also need a small step, that is, how Android accesses the project we deploy on the tomcat server, in the web Project, to access a webpage, enter http: // localhost: 8083/music/index in the address bar. jsp to access index. on the jsp page, the localhost can also be changed to 127.0.0.1, which indicates the local machine, where 8083 is a port number, but there are some differences when accessing a web project in Android, which include the following steps:
1. First, you must know the IP address on your computer. You can open the cmd command line window and enter the command ipconfig, as shown in:
Select the IPv4 address of your connected network. This IP address is required for network connection between Android and web projects.
2. next, we must know the port number on our tomcat server, and my port number is 8083. How can we check the tomcat port number, you can open the server in the conf directory under the tomcat installation directory. xml file. Check the port number, as shown in:
If the port number is occupied, modify the server. xml file to modify the port number, that is, the port value in the red box.
3. in this case, to access the files in the web project deployed on the tomcat server in Android, you can use the following address: http: // 192.168.1.1: 8083/BBS/index. jsp. In this way, you can access the web project. 192.168.1.1 is the IP address of the local machine, and 8083 is the port number of the server. You can directly enter http: // 192.168.1.1 in the browser: 8083/BBS/index. jsp accesses this index. jsp page.
3. Now you can write the android code:
1. First, create an android project and open the activity_main.xml layout file under the layout directory. The Code is as follows:
<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:orientation="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" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="5dp" android:text="@string/text" android:textSize="23sp" /> </RelativeLayout> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android: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_height="wrap_content" android:onClick="click" android:text="@string/play" /> <Button android:id="@+id/pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/play" android:onClick="click" android:text="@string/pause" /> <Button android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/pause" android:onClick="click" android:text="@string/stop" /> </RelativeLayout></LinearLayout>
The ImageView (Image view) with the id of finish and the three buttons at the bottom have an android: onClick attribute, therefore, you need to write a click () method in the MainActivity class. This method is triggered when ImageView and Button are clicked. This reduces the amount of code and eliminates the need to use findViewById and set event listening.
2. I put all the text required by the activity_main.xml layout file 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"> Jin Haixin-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. Add the code of the MainActivity. java file. The Code is 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 an Intent object int op =-1; // set the intermediate variable opswitch (v. getId () {case R. id. play: // when you click the 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: // click the return image button to finish (); // return the previous interface break; default: break;} Bundle bundle = new Bundle (); // instantiate a Bundle object bundle. putInt ("msg", op); // puts the op value into the intent of the bundle object. putExtras (bundle); // Add the bundle object to the intent object startService (intent); // enable this service }}
The switch case statement in the click () method is used to determine which button is clicked, and the intermediate variable op is passed to the MusicService service class to enable the MusicService service in MainActivity.
4. Next, attach the MusicService Service class. This class inherits the Service class and the code is as follows:
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; // declare a MediaPlayer object @ Overridepublic IBinder onBind (Intent arg0) {// method automatically generated by TODO to return null ;}/ /Create a service @ Overridepublic void onCreate () {// if (player = null) {player = MediaPlayer when the player Object is empty. create (MusicService. this, Uri. parse ("http: // 192.168.1.110: 8083/music/music2.mp3"); // instantiate an object by playing a music player on the server. setLooping (false); // set non-loop playback} super. onCreate ();} // destroy service @ Overridepublic void onDestroy () {// if (player! = Null) {player. stop (); // stop the player. release (); // release resource player = null; // set the player Object to null} super. onDestroy ();} // start the service @ Overridepublic int onStartCommand (Intent intent, int flags, int startId) {// method stub automatically generated by TODO Bundle B = intent. getExtras (); // get the Bundle object int op = B passed in from the MainActivity class. getInt ("msg"); // obtain the value of op in the MainActivity class switch (op) {case 1: // when op is 1, play () when the play button is clicked; // call the play () method break; case 2: // when the op is 2, that is, when the pause button is clicked, p Ause (); // call the pause () method break; case 3: // when op is 3, stop () when the stop button is clicked; // call stop () method break; default: break;} return super. onStartCommand (intent, flags, startId);} // method of stopping playing music private void stop () {// if (player! = Null) {player. seekTo (0); // set player from the beginning. stop (); // stop playing try {player. prepare (); // pre-loaded music} catch (IllegalStateException e) {// catch Block e automatically generated by TODO. printStackTrace ();} catch (IOException e) {// catch Block e automatically generated by TODO. printStackTrace () ;}}// method of pausing the playing of music private void pause () {// if (player. isPlaying () & player! = Null) {player. pause (); // pause playing music} // method of playing music private void play () {// if (player! = Null &&! Player. isPlaying () {player. start (); // start playing music }}}
Use 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>"));
In this way, you can access the mp3 files under the music project deployed on the tomcat server.
5. Finally, do not forget two things. First, we must declare the MusicService class in the AndroidManifest. xml file. Second, because we access web projects on the local server through the network, we must specify a permission to allow access to network resources in the AndroidManifest. xml file.
(1) Declare the service and put it under the <application> </application> mark. The Code is as follows:
<service android:name=".MusicService"></service>
(2) This program needs to access network resources, so you need to specify the permission to access network resources in the AndroidManifest. xml file. The Code is as follows:
<uses-permission android:name="android.permission.INTERNET"/>
4. Run this android project. The effect is shown above. Click the play button to start playing the music. Click the pause button to pause the playing. Click the play button again to continue playing. Click the stop button to stop the playing. Click the play button again to start playing from the beginning; click the returned image above to return to the previous page.
The above Code does not close the service. If you want to stop the service, you can call the stopService (intent) method. intent indicates the Intent object instantiated in the MainActivity class.
The above code: Download the source code
5. The above content is for your reference only. It is not well written. Please forgive me. If there is any error, please point it out. Thank you!
Zookeeper
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.