欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)簡單音樂播放控件

 更新時間:2018年04月28日 15:06:36   作者:lbcab  
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)簡單音樂播放控件,提供一個類似網(wǎng)易播放控件的默認(rèn)界面,具有一定的參考價值,感興趣的小伙伴們可以參考一下

之前看到網(wǎng)頁版的網(wǎng)易音樂播放控件, 正好在一個開源學(xué)習(xí)項目中需要簡單的音樂播放功能。所以想是不是可以封裝一個音樂播放控件,提供一個類似網(wǎng)易播放控件的默認(rèn)界面,而且提供更換界面的功能。使用時,只需要去設(shè)計界面, 而不用再去管音樂播放的邏輯,所以就實現(xiàn)了一個簡單的音樂播放控件。

音樂播放控件(MiniMusicView) 使用方法:

1.使用默認(rèn)的界面

(1) 在你的布局中加入 MiniMusicView

<com.hrb.library.MiniMusicView
  android:id="@+id/mmv_music"
  app:isLoadLayout="true"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />

(2) 設(shè)置音樂地址并播放音樂

mMusicView = (MiniMusicView) findViewById(R.id.mmv_music);
 mMusicView.setTitleText("music name");
 mMusicView.setAuthor("singer name");
 mMusicView.startPlayMusic("music url");

 // Or through the new way to create view object
 // mMusicView = new MiniMusicView(this);
 // mMusicView.initDefaultView();
 // mMusicView.setTitleText("music name");
 // mMusicView.startPlayMusic("music url");

(3) 停止音樂播放

@Override
 protected void onDestroy() {
  mMusicView.stopPlayMusic();
  super.onDestroy();
 }

效果圖如下:

這里寫圖片描述

2.使用自定義布局

(1) 在你的布局中加入 MiniMusicView

<com.hrb.library.MiniMusicView
  android:id="@+id/mmv_music"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />

(2) 設(shè)置自定義布局,設(shè)置音樂地址,播放音樂

mMusicView = (MiniMusicView) findViewById(R.id.mmv_music);
 View view = View.inflate(CustomActivity.this, R.layout.layout_custom_music, null);
 TextView title = (TextView) view.findViewById(R.id.tv_music_play_title);
 title.setText("music name");
 mMusicView.addView(view);
 mMusicView.startPlayMusic("music url");
 // Or through the new way to create view object
 // mMusicView = new MiniMusicView(this);
 // mMusicView.addView(view);
 // mMusicView.startPlayMusic("music url");

效果圖如下:

這里寫圖片描述

(3) MiniMusicView 還提供音樂狀態(tài)的回調(diào)監(jiān)聽接口

mMusicView.setOnMusicStateListener(new MiniMusicView.OnMusicStateListener() {
   @Override
   public void onPrepared(int duration) {
    Log.i(TAG, "start prepare play music");
   }

   @Override
   public void onError() {
    Log.i(TAG, "start play music error");
   }

   @Override
   public void onInfo(int what, int extra) {
    Log.i(TAG, "start play_mini_music music info");
   }

   @Override
   public void onMusicPlayComplete() {
    Log.i(TAG, "start play music completed");
   }

   @Override
   public void onSeekComplete() {
    Log.i(TAG, "seek play music completed");
   }

   @Override
   public void onProgressUpdate(int duration, int currentPos) {
    Log.i(TAG, "play music progress update");
   }

   @Override
   public void onHeadsetPullOut() {
    Log.i(TAG, "headset pull out");
   }
  });

你可以在相應(yīng)的監(jiān)聽中去完成需要的行為, 例如要實現(xiàn)當(dāng)耳機拔出,實現(xiàn)音樂播放停止, 可以在onHeadsetPullOut()接口中調(diào)用mMusicView.pausePlayMusic() 暫停音樂播放。

另外,MiniMusicView如何在工程中使用和源碼可以從這里獲取,大家可以根據(jù)需要進行修改, 如果使用中有bug請留言,不勝感激.

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

相關(guān)文章

最新評論