前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android实现简单音乐播放控件

Android实现简单音乐播放控件

作者头像
砸漏
发布2020-11-05 14:41:21
1K0
发布2020-11-05 14:41:21
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

之前看到网页版的网易音乐播放控件, 正好在一个开源学习项目中需要简单的音乐播放功能。所以想是不是可以封装一个音乐播放控件,提供一个类似网易播放控件的默认界面,而且提供更换界面的功能。使用时,只需要去设计界面, 而不用再去管音乐播放的逻辑,所以就实现了一个简单的音乐播放控件。

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

1.使用默认的界面

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

代码语言:javascript
复制
<com.hrb.library.MiniMusicView
  android:id="@+id/mmv_music"
  app:isLoadLayout="true"
  android:layout_width="match_parent"
  android:layout_height="match_parent" / 

(2) 设置音乐地址并播放音乐

代码语言:javascript
复制
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) 停止音乐播放

代码语言:javascript
复制
@Override
 protected void onDestroy() {
  mMusicView.stopPlayMusic();
  super.onDestroy();
 }

效果图如下:

2.使用自定义布局

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

代码语言:javascript
复制
<com.hrb.library.MiniMusicView
  android:id="@+id/mmv_music"
  android:layout_width="match_parent"
  android:layout_height="match_parent" / 

(2) 设置自定义布局,设置音乐地址,播放音乐

代码语言:javascript
复制
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 还提供音乐状态的回调监听接口

代码语言:javascript
复制
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");
   }
  });

你可以在相应的监听中去完成需要的行为, 例如要实现当耳机拔出,实现音乐播放停止, 可以在onHeadsetPullOut()接口中调用mMusicView.pausePlayMusic() 暂停音乐播放。

另外,MiniMusicView如何在工程中使用和源码可以从这里获取,大家可以根据需要进行修改, 如果使用中有bug请留言,不胜感激.

以上就是本文的全部内容,希望对大家的学习有所帮助。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档