播放音樂案例,

來源:互聯網
上載者:User

播放音樂案例,
播放音樂案例

 

分析:

和上一篇文章的結構是一樣的,只不過我們需要在這裡裡面加上播放音樂的一些操作:

其實也就是調用系統的播放音樂的API而已,寫在服務裡面就好,

//媒體播放器
private MediaPlayer player;

 1     public void onCreate() { 2         File file=new File(Environment.getExternalStorageDirectory(),"a.mp3"); 3         player =new MediaPlayer(); 4         try { 5             //設定播放源 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             //設定準備資源監聽器,當資源準備完畢,回調監聽器的onPrepared函數 5             player.setOnPreparedListener(new OnPreparedListener() { 6                 @Override 7                 //準備資源準備好了會調用這個 8                 public void onPrepared(MediaPlayer arg0) { 9                     //播放音樂10                     player.start();11                 }12             });13             14             //準備資源,好來播放音樂15             //非同步函數,這個函數內部會開啟一個子線程16             player.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         //結束音樂3         player.stop();4         //釋放資源,如果播放下一首的話,就用不著釋放資源5         player.release();6         Log.d("fanfan", "onDestroy");7         super.onDestroy();8     }

 

第一步,照樣找個類來繼承服務類

 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     //媒體播放器17     private MediaPlayer player;18     /**19      * 當綁定這個服務的時候調用20      */21     @Override22     public IBinder onBind(Intent arg0) {23         Log.d("fanfan", "onBind");24         return null;25     }26     /**27      * service被建立後調用28      */29     @Override30     public void onCreate() {31         File file=new File(Environment.getExternalStorageDirectory(),"a.mp3");32         player =new MediaPlayer();33         try {34             //設定播放源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      * service被start後調用45      */46     @Override47     public int onStartCommand(Intent intent, int flags, int startId) {48         49         try {50             //設定準備資源監聽器,當資源準備完畢,回調監聽器的onPrepared函數51             player.setOnPreparedListener(new OnPreparedListener() {52                 @Override53                 //準備資源準備好了會調用這個54                 public void onPrepared(MediaPlayer arg0) {55                     //播放音樂56                     player.start();57                 }58             });59             60             //準備資源,好來播放音樂61             //非同步函數,這個函數內部會開啟一個子線程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      * service被停止後調用74      */75     @Override76     public void onDestroy() {77         //結束音樂78         player.stop();79         //釋放資源,如果播放下一首的話,就用不著釋放資源80         player.release();81         Log.d("fanfan", "onDestroy");82         super.onDestroy();83     }84 85 }

 

第二步,該配置的監聽服務也是要配置的

 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>

 

第三步,播放或者結束音樂

 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://播放音樂,啟動服務23             startService(intent);24             break;25         case R.id.btn_stop://結束音樂,停止服務26             stopService(intent);27             break;28         }29     }30 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.