开源库MusicPlayManager - 封装StarrySky音乐库
Posted 雪の星空朝酱
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了开源库MusicPlayManager - 封装StarrySky音乐库相关的知识,希望对你有一定的参考价值。
开源库MusicPlayManager - 封装StarrySky音乐库
关于
MusicPlayManager地址,这是我最近重构简易音乐的时候看了一下StarrySky的使用,19年还是00年那会就想着要基于StarrySky做一个单例一样的管理类,后面不了了之了,现在因为重构项目,想着可以基于StarrySky来做一个用户几乎不需要改动的封装,来实现自定义音乐通知栏、只需要输入一个target页面来接收页面跳转即可,用户可以在使用之前再进行初始化。
仓库里面有简单的使用实例,支持java、kotlin。
MusicPlayManager提供音乐播放封装、自定义通知栏及圆角、日志功能、权限申请、无入侵获取上下文以及防恶意点击等。
效果图
使用
首先添加需要的权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
添加引用:
工程build添加如下:
repositories
maven url 'https://jitpack.io'
项目的build里添加如下
implementation 'com.github.Tobeyr1:MusicPlayManager:1.0.2-alpha'
Java版初始化
private final String[] APP_PERMISSIONS = new ArrayList<String>()
add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
add(Manifest.permission.READ_EXTERNAL_STORAGE);
add(Manifest.permission.RECORD_AUDIO);
add(Manifest.permission.READ_PHONE_STATE);
.toArray(new String[0]);
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_java);
checks = new PermissionChecks(this);
checks.requestPermissions(APP_PERMISSIONS, it ->
if (it)
// MusicPlay.initConfig(this,new PlayConfig());
else
return null;
);
Kotlin版初始化
private var checks: PermissionChecks? = null
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
checks = PermissionChecks(this)
checks!!.requestPermissions(arrayOf(
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO
))
if (it)
MusicPlay.initConfig(this, PlayConfig(
notificationClass = "com.tobery.app.JavaActivity" //设置target页面
,
defaultNotificationConfig = MusicNotificationConfig.create
targetClass "com.tobery.musicplay.NotificationReceiver"
targetClassBundle
val bundle = Bundle()
bundle.putString("title", "我是点击通知栏转跳带的参数")
bundle.putString("targetClass", "com.tobery.app.JavaActivity")
return@targetClassBundle bundle
smallIconRes //自定义通知栏小图标
R.drawable.ic_music_cover
))
java版权限检查工具使用方法:
private final String[] APP_PERMISSIONS = new ArrayList<String>()
add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
add(Manifest.permission.READ_EXTERNAL_STORAGE);
add(Manifest.permission.RECORD_AUDIO);
add(Manifest.permission.READ_PHONE_STATE);
.toArray(new String[0]);
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_java);
//必须要在oncreate里面注册,否则会崩溃提示异常
checks = new PermissionChecks(this);
//然后在需要使用的地方调用即可
checks.requestPermissions(APP_PERMISSIONS, it ->
if (it) //权限全部通过
else
return null;
);
//调用单个权限检查
checks.requestPermission(Manifest.permission.READ_PHONE_STATE,it ->
return null;
);
kotlin权限检查工具使用方法:
private var checks: PermissionChecks? = null
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//必须要在oncreate里面注册,否则会崩溃提示异常
checks = PermissionChecks(this)
//调用单个权限检查
checks!!.requestPermission(Manifest.permission.READ_PHONE_STATE)
if (it)
checks!!.requestPermissions(arrayOf(
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO
))
if (it)//lambda表达式
java使用打印日志
//可以打印任何对象
ViewExtensionKt.printLog(playbackStage.getSongInfo().getSongUrl());
kotlin使用打印日志
"是否有下一首$hasNextSong".printLog()
xxx.printLog()
java防恶意点击
ViewExtensionKt.setOnSingleClickListener(binding.btnPhoneLogin,view ->
Intent intent = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
);
kotlin防恶意点击
binding.btGetGeneralSetting.setOnSingleClickListener
使用
MusicPlay.playMusicById() | 通过songId播放 |
---|---|
MusicPlay.playMusicByUrl() | 通过歌曲url播放 |
– | – |
MusicPlay.playMusicByInfo() | 通过歌曲信息播放 |
– | – |
MusicPlay.playMusicByList(songList,0) | 播放歌曲列表,并从指定下标开始 |
– | – |
MusicPlay.pauseMusic() | 暂停 |
– | – |
MusicPlay.restoreMusic() | 恢复播放 |
– | – |
MusicPlay.setRepeatMode() | 设置播放模式以及是否循环 |
– | – |
MusicPlay.getRepeatMode() | 获取当前播放模式 |
– | – |
MusicPlay.skipToNext() | 下一首 |
– | – |
MusicPlay.skipToPrevious() | 上一首 |
– | – |
MusicPlay.stopMusic() | 停止 |
– | – |
MusicPlay.isPlaying() | 是否播放中 |
– | – |
MusicPlay.onPlayProgressListener() | 进度监听 |
– | – |
MusicPlay.onPlayStateListener() | 状态监听 |
– | – |
MusicPlay.seekTo() | 跳至指定进度,并可以觉得是否播放 |
– | – |
MusicPlay.addPlayList() | 添加播放队列 |
– | – |
MusicPlay.clearPlayList() | 清空播放队列 |
– | – |
还有好对方法,如设置音量大小、倍速、更新播放队列、开关通知栏等等。
例如下:
MusicInfo songInfo = new MusicInfo();
songInfo.setSongId("33894312");
songInfo.setSongUrl("http://music.163.com/song/media/outer/url?id=33894312");
songInfo.setArtist("歌手");
songInfo.setSongName("海阔天空");
songInfo.setSongCover("http://p3.music.126.net/Uyj-KRGb9ZnwuPLYEe739Q==/109951167614293336.jpg");
songList.add(songInfo);
MusicInfo songInfo1 = new MusicInfo();
songInfo1.setSongId("1960605228");
songInfo1.setSongUrl("http://music.163.com/song/media/outer/url?id=1960605228");
songInfo1.setArtist("龚明威");
songInfo1.setSongName("起风了");
songInfo1.setSongCover("http://p3.music.126.net/EI-1VBiCIJ7lF9R7sxFNJA==/109951167611502432.jpg");
songList.add(songInfo1);
MusicInfo songInfo2 = new MusicInfo();
songInfo2.setSongId("1960903012");
songInfo2.setSongName("黑本子(Black Benz)");
songInfo2.setSongUrl("http://music.163.com/song/media/outer/url?id=1960903012");
songInfo2.setArtist("KEY.L刘聪");
songInfo2.setSongCover("http://p4.music.126.net/yRqakJ-0o6ZN6T-CaU4XvA==/109951167619824931.jpg");
songList.add(songInfo2);
MusicInfo songInfo3 = new MusicInfo();
songInfo3.setSongId("3727");
songInfo3.setArtist("flc");
songInfo3.setSongName("测试");
songInfo3.setSongUrl("http://m8.music.126.net/20220706153844/41cc70c399540e0ff8cf639114e157ce/ymusic/9429/7fbc/3727/d0647c73bcc77006b94ce6ad9ae620f7.flac");
songInfo3.setSongCover("http://p3.music.126.net/EI-1VBiCIJ7lF9R7sxFNJA==/109951167611502432.jpg");
songList.add(songInfo3);
MusicPlay.playMusicByList(songList,0);
//播放状态监听
MusicPlay.onPlayStateListener(this, new OnMusicPlayStateListener()
@Override
public void onPlayState(@NonNull PlayManger playbackStage)
switch (playbackStage.getStage())
case PlayManger.IDLE:
ViewExtensionKt.printLog("空闲");
break;
case PlayManger.PLAYING:
ViewExtensionKt.printLog("播放");
break;
case PlayManger.BUFFERING:
ViewExtensionKt.printLog("缓冲");
break;
case PlayManger.PAUSE:
ViewExtensionKt.printLog("暂停");
break;
case PlayManger.SWITCH:
ViewExtensionKt.printLog("切歌"+playbackStage.getSongInfo().getSongUrl());
ViewExtensionKt.printLog("上一首"+playbackStage.getLastSongInfo().getSongName());
break;
);
MusicPlay.onPlayProgressListener( new OnMusicPlayProgressListener()
@Override
public void onPlayProgress(long currPos, long duration)
ViewExtensionKt.printLog("当前进度"+currPos);
);
目前存在问题
- 目前traget版本30 即Android11,因为Android12的行为变更暂未适配
以上是关于开源库MusicPlayManager - 封装StarrySky音乐库的主要内容,如果未能解决你的问题,请参考以下文章
基于OkHttp和RxJava封装的Socket长连接开源库