基于
MediaPlayer
的音频播放器简单封装了一下,内部已处理音频焦点,一行代码就能播!还可以关联
SeekBar
,不用再处理拖动事件啦,只需要在进度回调里更新就行:smile:
Step 1: 项目根目录的build.gradle添加如下配置:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2: app添加依赖:
dependencies {
implementation 'com.github.zaaach:AudioPlayerHelper:x.y'
}
1、播放
playerHelper = new AudioPlayerHelper(context)
.attachSeekBar(seekBar)//关联SeekBar
.setLooping(true)
.setInterval(500)//进度更新的间隔时间
.setDebug(true)
.setOnAudioPlayStateChangeListener(this)//播放器回调
.setDataSource("http://....")
.playOrPause();
//切换音乐
playerHelper.play("http://....");
2、播放器回调
@Override
public void onPreparing(MediaPlayer player) {
}
@Override
public void onPrepared(MediaPlayer player, int duration) {
}
@Override
public void onPlaying(MediaPlayer player) {
}
@Override
public void onProgress(MediaPlayer player, @Nullable SeekBar seekBar, boolean isDragging, int progress, int duration) {
}
@Override
public void onPlayPaused(MediaPlayer player) {
}
@Override
public void onPlayStop(MediaPlayer player) {
}
@Override
public void onPlayComplete(MediaPlayer player) {
}
@Override
public void onPlayError(String msg) {
}
3、生命周期
@Override
protected void onPause() {
super.onPause();
playerHelper.pause();//暂停
}
@Override
protected void onResume() {
super.onResume();
playerHelper.restore();//恢复播放
}
@Override
protected void onDestroy() {
super.onDestroy();
playerHelper.destroy();//释放资源
}
😉Good luck!!!
掘金: https://juejin.im/user/56f3dfe8efa6310055ac719f
简书: https://www.jianshu.com/u/913a8bb93d12
淘宝店: LEON家居生活馆 (动漫摆件)
😉淘宝店求个关注😉
Copyright (c) 2020 zaaach
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.