代码地址:https://download.csdn.net/download/qq_14931305/10803169
Bass库官网:http://www.un4seen.com/
Bass库参考文档:http://www.un4seen.com/doc/#bass/bass.html
集成请参考我之前的博客:https://blog.csdn.net/qq_14931305/article/details/84374301
直接上代码:BassMusicPlayInterface接口:
package com.gamestar.soundenginelib;
/**
* Created by yelei on 2018/11/20.
*/
public interface BassMusicPlayInterface {
boolean Load(String pathMusic);
void Play();
void Seek(float second);
void Pause();
void Resume();
void Stop();
float GetCurrentTime();
boolean IsPlaying();
void SetVolume(float volume);
}
BassMusicPlay实现音频播放类:
package com.gamestar.soundenginelib;
import android.util.Log;
import com.un4seen.bass.BASS;
/**
* Created by yelei on 2018/11/20.
*/
public class BassMusicPlay implements BassMusicPlayInterface {
static private BassMusicPlay instance = null;
public static BassMusicPlay getInstance() {
if(instance == null) {
instance = new BassMusicPlay();
}
return instance;
}
private int chan;
@Override
public boolean Load(String pathMusic) {
// setup recording device (using default device)
if (!BASS.BASS_Init(-1,44100,0)) {
Error("Can't initialize device");
return false;
}else {
chan=BASS.BASS_StreamCreateFile(pathMusic,0L,0L,BASS.BASS_SAMPLE_FLOAT);
if (chan == 0){
Error("Can't initialize BASS_StreamCreateFile");
return false;
}
return true;
}
}
@Override
public boolean IsPlaying() {
return (BASS.BASS_ChannelIsActive(chan) == BASS.BASS_ACTIVE_PLAYING);
}
@Override
public void Play() {
BASS.BASS_ChannelPlay(chan, true); // play the recorded data
}
@Override
public void Seek(float second) {
if (BASS.BASS_ChannelIsActive(chan) != BASS.BASS_ACTIVE_PLAYING){
Resume();
}
long postion =BASS.BASS_ChannelSeconds2Bytes(chan, second);
BASS.BASS_ChannelSetPosition(chan,postion,BASS.BASS_POS_BYTE);
}
@Override
public void SetVolume(float volume) {
if (volume>1) return;
BASS.BASS_SetVolume(volume);
Log.e("UnityAndroidPlugin","volume= "+BASS.BASS_GetVolume());
}
@Override
public void Pause() {
BASS.BASS_ChannelPause(chan);
}
@Override
public void Resume() {
BASS.BASS_ChannelPlay(chan, false); // play the recorded data
}
@Override
public void Stop() {
BASS.BASS_ChannelStop(chan);
BASS.BASS_StreamFree(chan);
BASS.BASS_Stop();
BASS.BASS_Free();
}
@Override
public float GetCurrentTime() {
return (float) BASS.BASS_ChannelBytes2Seconds(chan,
BASS.BASS_ChannelGetPosition(chan,BASS.BASS_POS_BYTE));
}
// display error messages
void Error(String es) {
// get error code in current thread for display in UI thread
String s=String.format("%s\n(error code: %d)", es, BASS.BASS_ErrorGetCode());
Log.e("UnityAndroidPlugin",s);
}
}
UnityAndroidPlugin类:
package com.gamestar.soundenginelib;
/**
* Created by yelei on 16/10/23.
*/
public class UnityAndroidPlugin {
public static boolean Load(String pathMusic){
return BassMusicPlay.getInstance().Load(pathMusic);
}
public static void Play(){
BassMusicPlay.getInstance().Play();
}
public static void Pause(){
BassMusicPlay.getInstance().Pause();
}
public static void Resume(){
BassMusicPlay.getInstance().Resume();
}
public static void Stop(){
BassMusicPlay.getInstance().Stop();
}
public static float GetCurrentTime(){
return BassMusicPlay.getInstance().GetCurrentTime();
}
public static void Seek(float second){
BassMusicPlay.getInstance().Seek(second);
}
public static void SetVolume(float volume){
BassMusicPlay.getInstance().SetVolume(volume);
}
public static boolean IsPlaying() {
return BassMusicPlay.getInstance().IsPlaying();
}
}
MainActivity直接调用:
//加载歌曲
UnityAndroidPlugin.Load(pathMusic);
//播放
UnityAndroidPlugin.Play();
//暂停
UnityAndroidPlugin.Pause();
//恢复播放
UnityAndroidPlugin.Resume();
//停止播放
UnityAndroidPlugin.Stop();
//设置音量,0-1.0之间
UnityAndroidPlugin.SetVolume(volume);
//播放位置
UnityAndroidPlugin.Seek(60);
//获取歌曲当前播放时间,单位毫秒
UnityAndroidPlugin.GetCurrentTime();