最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android基于Service的音樂播放器

 更新時間:2021年03月31日 09:54:44   投稿:lijiao  
這篇文章主要為大家詳細介紹了Android基于Service的音樂播放器,本文開發(fā)一個基于Service的音樂播放器,音樂由后臺運行的Service負責播放,感興趣的小伙伴們可以參考一下

本文開發(fā)一個基于Service的音樂播放器,音樂由后臺運行的Service負責播放,當后臺的播放狀態(tài)發(fā)生變化時,程序?qū)ㄟ^發(fā)送廣播通知前臺Activity更新界面;當點擊Activity的界面按鈕時,系統(tǒng)將通過發(fā)送廣播通知后臺Service來改變播放狀態(tài)。

前臺Activity界面有兩個按鈕,分別用于控制播放/暫停、停止,另外還有兩個文本框,用于顯示正在播放的歌曲名、歌手名。前臺Activity的代碼如下:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

 private ImageButton mStart;
 private ImageButton mStop;
 private TextView mMusicName;
 private TextView mSongerName;
 private ActivityReceiver mActivityReceiver;
 public static final String CTL_ACTION = "com.trampcr.action.CTL_ACTION";
 public static final String UPDATE_ACTION = "com.trampcr.action.UPDATE_ACTION";

 //定義音樂播放狀態(tài),0x11代表沒有播放,0x12代表正在播放,0x13代表暫停
 int status = 0x11;
 String[] musicNames = new String[]{"完美生活", "那一年", "故鄉(xiāng)"};
 String[] songerNames = new String[]{"許巍", "許巍", "許巍"};


 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  mStart = (ImageButton) findViewById(R.id.start);
  mStop = (ImageButton) findViewById(R.id.stop);
  mMusicName = (TextView) findViewById(R.id.music_name);
  mSongerName = (TextView) findViewById(R.id.songer_name);

  mStart.setOnClickListener(this);
  mStop.setOnClickListener(this);

  mActivityReceiver = new ActivityReceiver();
  //創(chuàng)建IntentFilter
  IntentFilter filter = new IntentFilter();
  //指定BroadcastReceiver監(jiān)聽的Action
  filter.addAction(UPDATE_ACTION);
  //注冊BroadcastReceiver
  registerReceiver(mActivityReceiver, filter);

  Intent intent = new Intent(MainActivity.this, MusicService.class);
  //啟動后臺Service
  startService(intent);
 }

 public class ActivityReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
   //獲取Intent中的update消息,update代表播放狀態(tài)
   int update = intent.getIntExtra("update", -1);
   //獲取Intent中的current消息,current代表當前正在播放的歌曲
   int current = intent.getIntExtra("current", -1);
   if (current >= 0){
    mMusicName.setText(musicNames[current]);
    mSongerName.setText(songerNames[current]);
   }
   switch (update){
    case 0x11:
     mStart.setBackgroundResource(R.drawable.play);
     status = 0x11;
     break;
    //控制系統(tǒng)進入播放狀態(tài)
    case 0x12:
     //在播放狀態(tài)下設(shè)置使用暫停圖標
     mStart.setBackgroundResource(R.drawable.pause);
     status = 0x12;
     break;
    case 0x13:
     //在暫停狀態(tài)下設(shè)置使用播放圖標
     mStart.setBackgroundResource(R.drawable.play);
     status = 0x13;
     break;
   }
  }
 }

 @Override
 public void onClick(View v) {
  Intent intent = new Intent(CTL_ACTION);
  switch (v.getId()){
   case R.id.start:
    intent.putExtra("control", 1);
    break;
   case R.id.stop:
    intent.putExtra("control", 2);
    break;
  }
  //發(fā)送廣播,將被Service中的BroadcastReceiver接收到
  sendBroadcast(intent);
 }
}

ActivityReceiver()用于響應后臺Service所發(fā)出的廣播,該程序?qū)鶕?jù)廣播Intent里的消息來改變播放狀態(tài),并更新程序界面中按鈕的圖標。

onClick中根據(jù)點擊的按鈕發(fā)送廣播,發(fā)送廣播時會把所按下的按鈕標識發(fā)送出來。

接下來是后臺Service,會在播放狀態(tài)發(fā)生改變時對外發(fā)送廣播。代碼如下:

public class MusicService extends Service {

 MyReceiver serviceReceiver;
 AssetManager mAssetManager;
 String[] musics = new String[]{"prefectLife.mp3", "thatYear.mp3", "country.mp3"};
 MediaPlayer mMediaPlayer;
 int status = 0x11;
 int current = 0; // 記錄當前正在播放的音樂

 @Nullable
 @Override
 public IBinder onBind(Intent intent) {
  return null;
 }

 @Override
 public void onCreate() {
  super.onCreate();
  mAssetManager = getAssets();
  serviceReceiver = new MyReceiver();
  //創(chuàng)建IntentFilter
  IntentFilter filter = new IntentFilter();
  filter.addAction(MainActivity.CTL_ACTION);
  registerReceiver(serviceReceiver, filter);
  //創(chuàng)建MediaPlayer
  mMediaPlayer = new MediaPlayer();
  //為MediaPlayer播放完成事件綁定監(jiān)聽器
  mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
   @Override
   public void onCompletion(MediaPlayer mp) {
    current++;
    if (current >= 3) {
     current = 0;
    }
    //發(fā)送廣播通知Activity更改文本框
    Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
    sendIntent.putExtra("current", current);
    //發(fā)送廣播,將被Activity中的BroadcastReceiver接收到
    sendBroadcast(sendIntent);
    //準備并播放音樂
    prepareAndPlay(musics[current]);
   }
  });
 }

 public class MyReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
   int control = intent.getIntExtra("control", -1);
   switch (control){
    case 1: // 播放或暫停
     //原來處于沒有播放狀態(tài)
     if (status ==0x11){
      //準備播放音樂
      prepareAndPlay(musics[current]);
      status = 0x12;
     }
     //原來處于播放狀態(tài)
     else if (status == 0x12){
      //暫停
      mMediaPlayer.pause();
      status = 0x13; // 改變?yōu)闀和顟B(tài)
     }
     //原來處于暫停狀態(tài)
     else if (status == 0x13){
      //播放
      mMediaPlayer.start();
      status = 0x12; // 改變狀態(tài)
     }
     break;
    //停止聲音
    case 2:
     //如果原來正在播放或暫停
     if (status == 0x12 || status == 0x13){
      //停止播放
      mMediaPlayer.stop();
      status = 0x11;
     }
   }
   //廣播通知Activity更改圖標、文本框
   Intent sendIntent = new Intent(MainActivity.UPDATE_ACTION);
   sendIntent.putExtra("update", status);
   sendIntent.putExtra("current", current);
   //發(fā)送廣播,將被Activity中的BroadcastReceiver接收到
   sendBroadcast(sendIntent);
  }
 }

 private void prepareAndPlay(String music) {
  try {
   //打開指定的音樂文件
   AssetFileDescriptor assetFileDescriptor = mAssetManager.openFd(music);
   mMediaPlayer.reset();
   //使用MediaPlayer加載指定的聲音文件
   mMediaPlayer.setDataSource(assetFileDescriptor.getFileDescriptor(), assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength());
   mMediaPlayer.prepare(); // 準備聲音
   mMediaPlayer.start(); // 播放
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

MyReceiver用于接收前臺Activity所發(fā)出的廣播,并根據(jù)廣播的消息內(nèi)容改變Service的播放狀態(tài),當播放狀態(tài)改變時,該Service對外發(fā)送一條廣播,廣播消息將會被前臺Activity接收,前臺Activity將會根據(jù)廣播消息更新界面。

為了讓該音樂播放器能按順序依次播放歌曲,程序為MediaPlayer增加了OnCompletionListener監(jiān)聽器,當MediaPlayer播放完成后將自動播放下一首歌曲。

運行程序,效果圖如下:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android Retrofit的使用詳解

    Android Retrofit的使用詳解

    本文介紹了Android Retrofit的使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • Android使用CountDownTimer模擬短信驗證倒計時

    Android使用CountDownTimer模擬短信驗證倒計時

    這篇文章主要為大家詳細介紹了Android使用CountDownTimer模擬短信驗證倒計時,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Android SharedPreferences實現(xiàn)數(shù)據(jù)存儲功能

    Android SharedPreferences實現(xiàn)數(shù)據(jù)存儲功能

    這篇文章主要為大家詳細介紹了Android SharedPreferences實現(xiàn)數(shù)據(jù)存儲功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Android中獲取apk安裝包信息的方法

    Android中獲取apk安裝包信息的方法

    這篇文章主要介紹了Android中獲取apk安裝包信息的方法,如圖標、應用包名、版本、安裝路徑等,需要的朋友可以參考下
    2014-05-05
  • Android實現(xiàn)返回拍攝的圖片功能實例

    Android實現(xiàn)返回拍攝的圖片功能實例

    這篇文章主要介紹了Android實現(xiàn)返回拍攝的圖片功能,以實例形式較為詳細的分析了Android返回拍攝圖片功能的具體步驟與實現(xiàn)方法,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • Android自定義控件實現(xiàn)底部菜單(上)

    Android自定義控件實現(xiàn)底部菜單(上)

    這篇文章主要為大家詳細介紹了Android自定義控件實現(xiàn)底部菜單的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Android Studio使用USB真機調(diào)試詳解

    Android Studio使用USB真機調(diào)試詳解

    這篇文章主要為大家詳細介紹了Android Studio使用USB真機調(diào)試的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android 中ListView點擊Item無響應問題的解決辦法

    Android 中ListView點擊Item無響應問題的解決辦法

    如果listitem里面包括button或者checkbox等控件,默認情況下listitem會失去焦點,導致無法響應item的事件,怎么解決呢?下面小編給大家分享下listview點擊item無響應的解決辦法
    2016-12-12
  • Flutter自定義底部導航欄的方法

    Flutter自定義底部導航欄的方法

    這篇文章主要為大家詳細介紹了Flutter自定義底部導航欄的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • Flutter啟動頁(閃屏頁)的具體實現(xiàn)及原理詳析

    Flutter啟動頁(閃屏頁)的具體實現(xiàn)及原理詳析

    這篇文章主要給大家介紹了關(guān)于Flutter啟動頁(閃屏頁)的具體實現(xiàn)及原理的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Flutter具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-04-04

最新評論

克什克腾旗| 岳普湖县| 咸丰县| 平谷区| 汝城县| 瑞金市| 从化市| 庄浪县| 东乡县| 丹江口市| 义马市| 安溪县| 东海县| 正镶白旗| 额尔古纳市| 罗平县| 德州市| 左云县| 文山县| 兴化市| 太和县| 仁怀市| 永定县| 济阳县| 北川| 县级市| 赣州市| 洛南县| 蕲春县| 同德县| 郎溪县| 新余市| 邛崃市| 阿克| 合肥市| 南澳县| 兴文县| 黔南| 铁岭县| 扎鲁特旗| 扶余县|