Android播放音樂案例分享
本文實(shí)例為大家分享了Android播放音樂案例的具體實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
效果:

分析:
和上一篇文章的結(jié)構(gòu)是一樣的,只不過我們需要在這里里面加上播放音樂的一些操作:
其實(shí)也就是調(diào)用系統(tǒng)的播放音樂的API而已,寫在服務(wù)里面就好,
//媒體播放器
private MediaPlayer player;
public void onCreate() {
File file=new File(Environment.getExternalStorageDirectory(),"a.mp3");
player =new MediaPlayer();
try {
//設(shè)置播放源
player.setDataSource(file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
Log.d("fanfan", "onCreate");
super.onCreate();
}
public int onStartCommand(Intent intent, int flags, int startId) {
try {
//設(shè)置準(zhǔn)備資源監(jiān)聽器,當(dāng)資源準(zhǔn)備完畢,回調(diào)監(jiān)聽器的onPrepared函數(shù)
player.setOnPreparedListener(new OnPreparedListener() {
@Override
//準(zhǔn)備資源準(zhǔn)備好了會(huì)調(diào)用這個(gè)
public void onPrepared(MediaPlayer arg0) {
//播放音樂
player.start();
}
});
//準(zhǔn)備資源,好來播放音樂
//異步函數(shù),這個(gè)函數(shù)內(nèi)部會(huì)開啟一個(gè)子線程
player.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
}
Log.d("fanfan", "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
public void onDestroy() {
//結(jié)束音樂
player.stop();
//釋放資源,如果播放下一首的話,就用不著釋放資源
player.release();
Log.d("fanfan", "onDestroy");
super.onDestroy();
}
第一步,照樣找個(gè)類來繼承服務(wù)類
package fry;
import java.io.File;
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
public class myService extends Service{
//媒體播放器
private MediaPlayer player;
/**
* 當(dāng)綁定這個(gè)服務(wù)的時(shí)候調(diào)用
*/
@Override
public IBinder onBind(Intent arg0) {
Log.d("fanfan", "onBind");
return null;
}
/**
* service被創(chuàng)建后調(diào)用
*/
@Override
public void onCreate() {
File file=new File(Environment.getExternalStorageDirectory(),"a.mp3");
player =new MediaPlayer();
try {
//設(shè)置播放源
player.setDataSource(file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
Log.d("fanfan", "onCreate");
super.onCreate();
}
/**
* service被start后調(diào)用
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
//設(shè)置準(zhǔn)備資源監(jiān)聽器,當(dāng)資源準(zhǔn)備完畢,回調(diào)監(jiān)聽器的onPrepared函數(shù)
player.setOnPreparedListener(new OnPreparedListener() {
@Override
//準(zhǔn)備資源準(zhǔn)備好了會(huì)調(diào)用這個(gè)
public void onPrepared(MediaPlayer arg0) {
//播放音樂
player.start();
}
});
//準(zhǔn)備資源,好來播放音樂
//異步函數(shù),這個(gè)函數(shù)內(nèi)部會(huì)開啟一個(gè)子線程
player.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
}
Log.d("fanfan", "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
/**
* service被停止后調(diào)用
*/
@Override
public void onDestroy() {
//結(jié)束音樂
player.stop();
//釋放資源,如果播放下一首的話,就用不著釋放資源
player.release();
Log.d("fanfan", "onDestroy");
super.onDestroy();
}
}
第二步,該配置的監(jiān)聽服務(wù)也是要配置的
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.playMusic"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="fry.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="fry.Activity01" android:exported="true"></activity>
<service android:name="fry.myService">
</service>
</application>
</manifest>
第三步,播放或者結(jié)束音樂
package fry;
import com.example.playMusic.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class Activity01 extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity01);
}
public void onClick(View view){
Intent intent=new Intent();
intent.setClass(this, myService.class);
switch(view.getId()){
case R.id.btn_start://播放音樂,啟動(dòng)服務(wù)
startService(intent);
break;
case R.id.btn_stop://結(jié)束音樂,停止服務(wù)
stopService(intent);
break;
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何設(shè)置Android studio 3.0顯示光標(biāo)返回上一次瀏覽位置的箭頭圖標(biāo)
這篇文章主要介紹了如何設(shè)置Android studio 3.0顯示光標(biāo)返回上一次瀏覽位置的箭頭圖標(biāo) 很多朋友反映剛升級(jí)了Android studio 3.0,發(fā)現(xiàn)光標(biāo)返回上一次瀏覽位置的箭頭圖標(biāo)沒有了,下文給大家介紹的非常詳細(xì),需要的朋友可以參考下2017-11-11
Android自定義AvatarImageView實(shí)現(xiàn)頭像顯示效果
這篇文章主要為大家詳細(xì)介紹了Android自定義AvatarImageView實(shí)現(xiàn)頭像顯示效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Android的EditText字?jǐn)?shù)檢測(cè)和限制解決辦法
這篇文章主要介紹了Android的EditText字?jǐn)?shù)檢測(cè)和限制解決辦法的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android變形(Transform)之Camera使用介紹
Camera主要實(shí)現(xiàn)3D的變形,有轉(zhuǎn)動(dòng),旋轉(zhuǎn)等,Camera的源碼是由Native(本地代碼)實(shí)現(xiàn),提供的接口也比較簡(jiǎn)單,感興趣的朋友可以參考下,或許對(duì)你學(xué)習(xí)有所幫助2013-02-02
詳談自定義View之GridView單選 金額選擇Layout-ChooseMoneyLayout
下面小編就為大家?guī)硪黄斦勛远xView之GridView單選 金額選擇Layout-ChooseMoneyLayout。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
舉例講解Android應(yīng)用開發(fā)中OTTO框架的基本使用
這篇文章主要介紹了Android應(yīng)用開發(fā)中OTTO框架的基本使用講解,文中舉了創(chuàng)建一個(gè)單例模式的應(yīng)用例子,需要的朋友可以參考下2016-02-02
5個(gè)Android開發(fā)中比較常見的內(nèi)存泄漏問題及解決辦法
本文主要介紹了5個(gè)Android開發(fā)中比較常見的內(nèi)存泄漏問題及解決辦法,具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02
Android 判斷屏幕開關(guān)狀態(tài)方式總結(jié)
這篇文章主要介紹了Android 判斷屏幕開關(guān)狀態(tài)方式總結(jié)的相關(guān)資料,需要的朋友可以參考下2016-10-10
詳解Android中實(shí)現(xiàn)ListView左右滑動(dòng)刪除條目的方法
這篇文章主要介紹了Android中實(shí)現(xiàn)ListView左右滑動(dòng)刪除條目的方法,文中分別展示了通過Scroller和NineOldAndroids來實(shí)現(xiàn)的例子,需要的朋友可以參考下2016-04-04

