亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android基于service實(shí)現(xiàn)音樂(lè)的后臺(tái)播放功能示例

 更新時(shí)間:2016年10月24日 11:12:17   作者:pku_android  
這篇文章主要介紹了Android基于service實(shí)現(xiàn)音樂(lè)的后臺(tái)播放功能,結(jié)合實(shí)例形式分析了Android基于Service組件實(shí)現(xiàn)多媒體音頻播放功能的步驟與相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Android基于service實(shí)現(xiàn)音樂(lè)的后臺(tái)播放功能。分享給大家供大家參考,具體如下:

Service是一個(gè)生命周期長(zhǎng)且沒(méi)有用戶界面的程序,當(dāng)程序在各個(gè)activity中切換的時(shí)候,我們可以利用service來(lái)實(shí)現(xiàn)背景音樂(lè)的播放,即使當(dāng)程序退出到后臺(tái)的時(shí)候,音樂(lè)依然在播放。下面我們給出具體例子的實(shí)現(xiàn):

當(dāng)然,首先要在資源文件夾中添加一首MP3歌曲:

要實(shí)現(xiàn)音樂(lè)的播放,需要在界面中放置兩個(gè)按鈕,用來(lái)控制音樂(lè)的播放和停止,通過(guò)使用startService和stopService來(lái)實(shí)現(xiàn)這兩個(gè)功能:

修改src下的ServiceDemoAvtivity.Java代碼添加如下按鈕事件的代碼:

Button start = (Button)findViewById(R.id.start);
Button stop = (Button)findViewById(R.id.stop);
Button.OnClickListener listener = new Button.OnClickListener(){
  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(getApplicationContext(),MusicService.class);
    switch(v.getId()){
      case R.id.start: startService(intent);break;
      case R.id.stop: stopService(intent);break;
    }
  }
};
start.setOnClickListener(listener);
stop.setOnClickListener(listener);

下面是更為重要的service部分。創(chuàng)建一個(gè)MusicService繼承于Service,然后通過(guò)start和stop方法來(lái)控制音樂(lè)的播放。下面是MusicService.java中的關(guān)鍵代碼:

public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);
    Toast.makeText(this, "onStart", Toast.LENGTH_LONG).show();
    player = MediaPlayer.create(this, R.raw.eason);
    player.setLooping(true);
    player.start();
}
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    Toast.makeText(this, "onDestroy", Toast.LENGTH_LONG).show();
    player.stop();
}

當(dāng)然還需要在AndroidMainfest中聲明MusicService:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
      android:name=".ServiceDemoActivity"
      android:label="@string/app_name" >
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <service android:name="MusicService" />
</application>

整個(gè)例子就構(gòu)造完成了,部署到模擬器或者手機(jī)上就可以實(shí)現(xiàn)后臺(tái)播放啦。

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android Service組件使用技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android開(kāi)發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Android使用SmsManager實(shí)現(xiàn)短信發(fā)送功能

    Android使用SmsManager實(shí)現(xiàn)短信發(fā)送功能

    這篇文章主要為大家詳細(xì)介紹了Android使用SmsManager實(shí)現(xiàn)短信發(fā)送功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • View中如何進(jìn)行手勢(shì)識(shí)別onFling動(dòng)作實(shí)現(xiàn)介紹

    View中如何進(jìn)行手勢(shì)識(shí)別onFling動(dòng)作實(shí)現(xiàn)介紹

    下面我們就以實(shí)現(xiàn)手勢(shì)識(shí)別的onFling動(dòng)作,在CwjView中我們從View類繼承,當(dāng)然大家可以從TextView等更高層的界面中實(shí)現(xiàn)觸控,感興趣的朋友可以了解下哈
    2013-06-06
  • Android權(quán)限HaloPermission詳細(xì)使用

    Android權(quán)限HaloPermission詳細(xì)使用

    這篇文章主要介紹了Android權(quán)限HaloPermission詳細(xì)使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • Kotlin Flow常見(jiàn)場(chǎng)景下的使用實(shí)例

    Kotlin Flow常見(jiàn)場(chǎng)景下的使用實(shí)例

    這篇文章主要為大家介紹了Kotlin Flow常見(jiàn)場(chǎng)景下的使用實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Android對(duì)話框AlertDialog.Builder使用方法詳解

    Android對(duì)話框AlertDialog.Builder使用方法詳解

    這篇文章主要介紹了Android對(duì)話框AlertDialog.Builder使用方法詳解的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • Android實(shí)現(xiàn)輪播圖無(wú)限循環(huán)效果

    Android實(shí)現(xiàn)輪播圖無(wú)限循環(huán)效果

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)輪播圖無(wú)限循環(huán)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • 最新評(píng)論