Playing music case,
Playing music
Analysis:
The structure is the same as that in the previous article, but we need to add some operations for playing music here:
In fact, it is just calling the system's API for playing music. It is good to write it in the service,
// Media Player
Private MediaPlayer player;
1 public void onCreate () {2 File file = new File (Environment. getExternalStorageDirectory (), "a.mp3"); 3 player = new MediaPlayer (); 4 try {5 // set playing source 6 player. setDataSource (file. getAbsolutePath (); 7} catch (Exception e) {8 e. printStackTrace (); 9} 10 Log. d ("fanfan", "onCreate"); 11 super. onCreate (); 12}
1 public int onStartCommand (Intent intent, int flags, int startId) {2 3 try {4 // You can call back the listener's onPrepared function 5 player when the resource preparation is complete. setOnPreparedListener (new OnPreparedListener () {6 @ Override 7 // when resources are ready, this 8 public void onPrepared (MediaPlayer arg0) {9 // play music 10 player. start (); 11} 12}); 13 14 // prepare resources for playing music. 15 // asynchronous function. A subthread 16 player is enabled in this function. prepareAsync (); 17 18} catch (Exception e) {19 e. printStackTrace (); 20} 21 22 Log. d ("fanfan", "onStartCommand"); 23 return super. onStartCommand (intent, flags, startId); 24}
1 public void onDestroy () {2 // end music 3 player. stop (); 4 // release the resource. If you play the next one, you do not need to release the resource 5 player. release (); 6 Log. d ("fanfan", "onDestroy"); 7 super. onDestroy (); 8}
The first step is to find a class to inherit the service class.
1 package fry; 2 3 import java. io. file; 4 import java. io. IOException; 5 6 import android. app. service; 7 import android. content. intent; 8 import android. media. mediaPlayer; 9 import android. media. mediaPlayer. onPreparedListener; 10 import android. OS. environment; 11 import android. OS. IBinder; 12 import android. util. log; 13 14 public class myService extends Service {15 16 // Media player 17 private MediaPlayer player; 18/** 19 * When binding this service, call 20 */21 @ Override22 public IBinder onBind (Intent arg0) {23 Log. d ("fanfan", "onBind"); 24 return null; 25} 26/** 27 * after the service is created, call 28 */29 @ Override30 public void onCreate () {31 File file = new File (Environment. getExternalStorageDirectory (), "a.mp3"); 32 player = new MediaPlayer (); 33 try {34 // set the playback source 35 player. setDataSource (file. getAbsolutePath (); 36} catch (Exception e) {37 e. printStackTrace (); 38} 39 Log. d ("fanfan", "onCreate"); 40 super. onCreate (); 41} 42 43/** 44 * after the service is started, call 45 */46 @ Override47 public int onStartCommand (Intent intent, int flags, int startId) {48 49 try {50 // set the prepare resource listener. When the resource preparation is complete, the onPrepared function 51 player of the listener is called back. setOnPreparedListener (new OnPreparedListener () {52 @ Override53 // when resources are ready, the 54 public void onPrepared (MediaPlayer arg0) {55 // play 56 player music. start (); 57} 58}); 59 60 // prepare resources for playing music 61 // asynchronous function. This function starts a subthread 62 player. prepareAsync (); 63 64} catch (Exception e) {65 e. printStackTrace (); 66} 67 68 Log. d ("fanfan", "onStartCommand"); 69 return super. onStartCommand (intent, flags, startId); 70} 71 72/** 73 * after the service is stopped, call 74 */75 @ Override76 public void onDestroy () {77 // end music 78 player. stop (); 79 // release the resource. If you play the next one, you do not need to release the resource 80 player. release (); 81 Log. d ("fanfan", "onDestroy"); 82 super. onDestroy (); 83} 84 85}
Step 2: The Listener service for this configuration also needs to be configured
1 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2 package="com.example.playMusic" 3 android:versionCode="1" 4 android:versionName="1.0" > 5 6 <uses-sdk 7 android:minSdkVersion="8" 8 android:targetSdkVersion="19" /> 9 10 <application11 android:allowBackup="true"12 android:icon="@drawable/ic_launcher"13 android:label="@string/app_name"14 android:theme="@style/AppTheme" >15 <activity16 android:name="fry.MainActivity"17 android:label="@string/app_name" >18 <intent-filter>19 <action android:name="android.intent.action.MAIN" />20 21 <category android:name="android.intent.category.LAUNCHER" />22 </intent-filter>23 </activity>24 <activity android:name="fry.Activity01" android:exported="true"></activity>25 26 <service android:name="fry.myService">27 28 </service>29 30 </application>31 32 </manifest>
Step 3: Play or end the music
1 package fry; 2 3 import com. example. playMusic. r; 4 5 import android. app. activity; 6 import android. content. intent; 7 import android. OS. bundle; 8 import android. view. view; 9 10 public class Activity01 extends Activity {11 @ Override12 protected void onCreate (Bundle savedInstanceState) {13 // TODO Auto-generated method stub14 super. onCreate (savedInstanceState); 15 setContentView (R. layout. activity01); 16} 17 18 public void onClick (View view) {19 Intent intent = new Intent (); 20 intent. setClass (this, myService. class); 21 switch (view. getId () {22 case R. id. btn_start: // play music, start service 23 startService (intent); 24 break; 25 case R. id. btn_stop: // stop music, stop service 26 stopService (intent); 27 break; 28} 29} 30}