合并1
This commit is contained in:
@@ -9,6 +9,7 @@ import static io.agora.rtc2.video.VideoEncoderConfiguration.ORIENTATION_MODE.*;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.hardware.SensorManager;
|
||||
@@ -57,6 +58,9 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import io.agora.mediaplayer.IMediaPlayerObserver;
|
||||
import io.agora.mediaplayer.data.CacheStatistics;
|
||||
@@ -83,9 +87,9 @@ import io.agora.rtc2.video.VideoEncoderConfiguration;
|
||||
public class AgoraManager {
|
||||
|
||||
private static volatile AgoraManager instance;
|
||||
public static RtcEngineEx rtcEngine;
|
||||
public RtcEngineEx rtcEngine;
|
||||
// private RtcEngineEx rtcEngineEx;
|
||||
private static Context context;
|
||||
private Context context;
|
||||
private boolean isLocalAudioEnabled = true; // 默认开启麦克风
|
||||
// private final List<IRtcEngineEventHandler> eventHandlers = new ArrayList<>();
|
||||
private static IAgoraMusicContentCenter musicContentCenter;
|
||||
@@ -102,16 +106,14 @@ public class AgoraManager {
|
||||
private String pkRoomId = "";
|
||||
private static String lastRoomId;
|
||||
|
||||
// 用于跟踪所有Handler,便于清理
|
||||
private static List<Handler> handlers = new ArrayList<>();
|
||||
|
||||
// 后台任务线程池,用于并行处理音乐相关任务
|
||||
private static ExecutorService executorService = Executors.newFixedThreadPool(3);
|
||||
|
||||
public int pkUserId;
|
||||
|
||||
public int getPkUserId() {
|
||||
return pkUserId;
|
||||
}
|
||||
|
||||
public void setPkUserId(int pkUserId) {
|
||||
this.pkUserId = pkUserId;
|
||||
}
|
||||
|
||||
public void setLastRoomId(String value) {
|
||||
lastRoomId = value;
|
||||
}
|
||||
@@ -122,27 +124,22 @@ public class AgoraManager {
|
||||
}
|
||||
|
||||
private AgoraManager() {
|
||||
// this.context = context.getApplicationContext();
|
||||
// init(CommonAppContext.getInstance().getCurrentEnvironment().getSwSdkAppId());
|
||||
|
||||
}
|
||||
|
||||
public static AgoraManager getInstance(Context con) {
|
||||
public static AgoraManager getInstance() {
|
||||
if (instance == null) {
|
||||
synchronized (AgoraManager.class) {
|
||||
if (instance == null) {
|
||||
instance = new AgoraManager();
|
||||
context = con.getApplicationContext(); // 使用ApplicationContext避免内存泄漏
|
||||
// init(CommonAppContext.getInstance().getCurrentEnvironment().getSwSdkAppId());
|
||||
// init(CommonAppContext.getInstance().getCurrentEnvironment().getSwSdkAppId());
|
||||
instance.context = CommonAppContext.getInstance(); // 使用ApplicationContext避免内存泄漏
|
||||
}
|
||||
}
|
||||
}
|
||||
// 确保引擎已初始化
|
||||
if (rtcEngine == null) {
|
||||
if (instance.rtcEngine == null) {
|
||||
synchronized (AgoraManager.class) {
|
||||
if (rtcEngine == null) {
|
||||
init(CommonAppContext.getInstance().getCurrentEnvironment().getSwSdkAppId());
|
||||
if (instance.rtcEngine == null) {
|
||||
instance.init(CommonAppContext.getInstance().getCurrentEnvironment().getSwSdkAppId());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -168,11 +165,10 @@ public class AgoraManager {
|
||||
/**
|
||||
* 初始化 Agora 引擎
|
||||
*/
|
||||
public static void init(String appId) {
|
||||
public void init(String appId) {
|
||||
if (rtcEngine != null) {
|
||||
return;
|
||||
}
|
||||
;
|
||||
|
||||
synchronized (AgoraManager.class) {
|
||||
if (rtcEngine != null) {
|
||||
@@ -282,6 +278,8 @@ public class AgoraManager {
|
||||
*/
|
||||
public void cleanup() {
|
||||
try {
|
||||
// 清理所有Handler,防止内存泄漏
|
||||
clearAllHandlers();
|
||||
|
||||
if (rtcEngine != null) {
|
||||
// // 离开频道
|
||||
@@ -316,6 +314,54 @@ public class AgoraManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理所有Handler,防止内存泄漏
|
||||
*/
|
||||
private void clearAllHandlers() {
|
||||
synchronized (handlers) {
|
||||
for (Handler handler : handlers) {
|
||||
if (handler != null) {
|
||||
handler.removeCallbacksAndMessages(null);
|
||||
}
|
||||
}
|
||||
handlers.clear();
|
||||
}
|
||||
|
||||
// 关闭线程池
|
||||
if (executorService != null && !executorService.isShutdown()) {
|
||||
executorService.shutdown();
|
||||
try {
|
||||
if (!executorService.awaitTermination(1, TimeUnit.SECONDS)) {
|
||||
executorService.shutdownNow();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
executorService.shutdownNow();
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
executorService = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建并跟踪Handler
|
||||
*/
|
||||
private Handler createTrackedHandler(Looper looper) {
|
||||
Handler handler = new Handler(looper);
|
||||
synchronized (handlers) {
|
||||
handlers.add(handler);
|
||||
}
|
||||
return handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在后台线程池中执行任务
|
||||
*/
|
||||
private void executeInBackground(Runnable task) {
|
||||
if (executorService != null && !executorService.isShutdown()) {
|
||||
executorService.execute(task);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isOnJoin = false;
|
||||
|
||||
public boolean isOnJoin() {
|
||||
@@ -329,7 +375,7 @@ public class AgoraManager {
|
||||
/**
|
||||
* 默认事件处理器,转发给所有监听器
|
||||
*/
|
||||
private static IRtcEngineEventHandler getDefaultEventHandler() {
|
||||
private IRtcEngineEventHandler getDefaultEventHandler() {
|
||||
return new IRtcEngineEventHandler() {
|
||||
|
||||
@Override
|
||||
@@ -362,46 +408,6 @@ public class AgoraManager {
|
||||
// 填入用于鉴权的 Token
|
||||
contentCenterConfiguration.token = SpUtil.getRtmToken();
|
||||
|
||||
// 创建 IAgoraMusicContentCenterEventHandler,用于 SDK 向客户端发送音乐内容中心事件通知
|
||||
// contentCenterConfiguration.eventHandler = new IMusicContentCenterEventHandler() {
|
||||
// @Override
|
||||
// public void onPreLoadEvent(String requestId, long songCode, int percent, String lyricUrl, int status, int errorCode) {
|
||||
// LogUtils.e("@@@1", "requestId: " + requestId + ", songCode: " + songCode + ", percent: " + percent + ", lyricUrl: " + lyricUrl + ", status: " + status + ", errorCode: " + errorCode);
|
||||
// if (!lyricUrl.isEmpty() && percent == 100 && !isBjMusic) {
|
||||
// getLyricsInstance(lyricUrl);
|
||||
// }
|
||||
//// musicPlayer.open(songCode, 0);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onMusicCollectionResult(String requestId, int page, int pageSize, int total, Music[] list, int errorCode) {
|
||||
// LogUtils.e("@@@2", "requestId: " + requestId + ", page: " + page + ", pageSize: " + pageSize + ", total: " + total);
|
||||
// MusicBean musicBean = new MusicBean();
|
||||
// musicBean.setMusicList(Arrays.asList(list));
|
||||
// musicBean.setPage(page);
|
||||
// EventBus.getDefault().post(musicBean);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onMusicChartsResult(String requestId, MusicChartInfo[] list, int errorCode) {
|
||||
// LogUtils.e("@@@", "requestId: " + requestId);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onLyricResult(String requestId, long songCode, String lyricUrl, int errorCode) {
|
||||
// LogUtils.e("@@@22", "requestId: " + requestId + ", songCode: " + songCode + ", lyricUrl: " + lyricUrl + ", errorCode: " + errorCode);
|
||||
// if (lyricUrl != null && !isBjMusic) {
|
||||
// getLyricsInstance(lyricUrl);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onSongSimpleInfoResult(String requestId, long songCode, String simpleInfo, int errorCode) {
|
||||
// LogUtils.e("@@@", "requestId: " + requestId + ", songCode: " + songCode + ", simpleInfo: " + simpleInfo + ", errorCode: " + errorCode);
|
||||
// }
|
||||
// };
|
||||
// 初始化 IAgoraMusicContentCenter
|
||||
|
||||
musicContentCenter.initialize(contentCenterConfiguration);
|
||||
if (musicPlayer != null) {
|
||||
musicPlayer = null;
|
||||
@@ -473,12 +479,6 @@ public class AgoraManager {
|
||||
|
||||
@Override
|
||||
public void onUserJoined(int uid, int elapsed) {//远端用户加入频道
|
||||
// for (IRtcEngineEventHandler handler : eventHandlers) {
|
||||
// if (handler != null) {
|
||||
// handler.onUserJoined(uid, elapsed);
|
||||
//
|
||||
// }
|
||||
// }
|
||||
|
||||
for (SoundLevelUpdateListener listener : soundLevelUpdateListeners) {
|
||||
if (listener != null) {
|
||||
@@ -500,19 +500,13 @@ public class AgoraManager {
|
||||
|
||||
@Override
|
||||
public void onUserOffline(int uid, int reason) {//远端用户离开频道
|
||||
// for (IRtcEngineEventHandler handler : eventHandlers) {
|
||||
// if (handler != null) {
|
||||
// handler.onUserOffline(uid, reason);
|
||||
// }
|
||||
for (SoundLevelUpdateListener listener : soundLevelUpdateListeners) {
|
||||
if (listener != null) {
|
||||
ThreadUtils.runOnUiThread(() -> {
|
||||
// 远程用户音量变化
|
||||
listener.userOffline(uid, reason);
|
||||
// }
|
||||
});
|
||||
}
|
||||
// }
|
||||
}
|
||||
SurfaceView renderView = null;
|
||||
rtcEngine.setupRemoteVideo(new VideoCanvas(null, Constants.RENDER_MODE_FIT, uid));
|
||||
@@ -535,7 +529,6 @@ public class AgoraManager {
|
||||
ThreadUtils.runOnUiThread(() -> {
|
||||
// 远程用户音量变化
|
||||
listener.onRemoteSoundLevelUpdate(uid > 0 ? String.valueOf(uid) : SpUtil.getUserId() + "", volume);
|
||||
// }
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -597,7 +590,7 @@ public class AgoraManager {
|
||||
/**
|
||||
* 创建并返回 IMediaPlayerObserver 实例
|
||||
*/
|
||||
private static IMediaPlayerObserver createMediaPlayerObserver() {
|
||||
private IMediaPlayerObserver createMediaPlayerObserver() {
|
||||
return new IMediaPlayerObserver() {
|
||||
@Override
|
||||
public void onPlayerStateChanged(io.agora.mediaplayer.Constants.MediaPlayerState state, io.agora.mediaplayer.Constants.MediaPlayerReason reason) {
|
||||
@@ -720,10 +713,6 @@ public class AgoraManager {
|
||||
connection.localUid = SpUtil.getUserId();
|
||||
pkRoomId = channelId;
|
||||
pkUserId = Integer.parseInt(pkUserIds);
|
||||
// rtcEngine.joinChannelEx(token, connection, options, getDefaultEventHandler());
|
||||
// muteAllRemoteAudioStreamsEx(true);
|
||||
// muteAllRemoteAudioStreamsExUserId(false);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -732,24 +721,12 @@ public class AgoraManager {
|
||||
RtcConnection connection = new RtcConnection();
|
||||
connection.channelId = mRoomId;
|
||||
connection.localUid = uid;
|
||||
// rtcEngine.leaveChannelEx(connection);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateChannelMediaOptions() {
|
||||
|
||||
if (rtcEngine != null) {
|
||||
options.clientRoleType = Constants.CLIENT_ROLE_BROADCASTER;
|
||||
// options.autoSubscribeVideo = true;
|
||||
// options.autoSubscribeAudio = true;
|
||||
//// 不发布摄像头采集的视频
|
||||
// options.publishCameraTrack = false;
|
||||
//// 不发布麦克风采集的视频
|
||||
// options.publishMicrophoneTrack = false;
|
||||
//// 在频道中发布屏幕采集的视频
|
||||
// options.publishScreenCaptureVideo = true;
|
||||
//// 在频道中发布屏幕采集的音频
|
||||
// options.publishScreenCaptureAudio = true;
|
||||
rtcEngine.setScreenCaptureScenario(Constants.ScreenScenarioType.SCREEN_SCENARIO_VIDEO);
|
||||
rtcEngine.updateChannelMediaOptions(options);
|
||||
}
|
||||
@@ -757,30 +734,7 @@ public class AgoraManager {
|
||||
|
||||
public void post() {
|
||||
if (rtcEngine != null) {
|
||||
// rtcEngine.setParameters("{\"che.video.mobile_1080p\":true}");
|
||||
// rtcEngine.setClientRole(Constants.CLIENT_ROLE_BROADCASTER);
|
||||
//
|
||||
// /*Enable video module*/
|
||||
// rtcEngine.enableVideo();
|
||||
// // Setup video encoding configs
|
||||
// rtcEngine.setVideoEncoderConfiguration(new VideoEncoderConfiguration(
|
||||
// VD_640x360,
|
||||
// FRAME_RATE_FPS_15,
|
||||
// STANDARD_BITRATE,
|
||||
// ORIENTATION_MODE_ADAPTIVE
|
||||
// ));
|
||||
// /*Set up to play remote sound with receiver*/
|
||||
// rtcEngine.setDefaultAudioRoutetoSpeakerphone(true);
|
||||
|
||||
ScreenCaptureParameters screenCaptureParameters = new ScreenCaptureParameters();
|
||||
// screenCaptureParameters.captureVideo = true;
|
||||
// screenCaptureParameters.videoCaptureParameters.width = 1440;
|
||||
// screenCaptureParameters.videoCaptureParameters.height = 1940;
|
||||
// screenCaptureParameters.videoCaptureParameters.framerate = 15;
|
||||
// screenCaptureParameters.captureAudio = true;
|
||||
// screenCaptureParameters.audioCaptureParameters.captureSignalVolume = 50;
|
||||
//// screenCaptureParameters.videoCaptureParameters.bitrate = 500;
|
||||
// rtcEngine.startScreenCapture(screenCaptureParameters);
|
||||
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
||||
Display display = manager.getDefaultDisplay();
|
||||
|
||||
@@ -1059,31 +1013,38 @@ public class AgoraManager {
|
||||
if (type == 1) {
|
||||
if (code < 0) {
|
||||
String result = musicContentCenter.preload(mSongCode);
|
||||
// 创建定时器,3秒后再次检查
|
||||
new Handler(Looper.getMainLooper()).postDelayed(() -> {
|
||||
// 使用后台线程池处理预加载检查,避免阻塞UI线程
|
||||
executeInBackground(() -> {
|
||||
try {
|
||||
Thread.sleep(3000); // 3秒延迟
|
||||
Log.d("AgoraManager", "Preload result: " + result);
|
||||
isPreload(mSongCode, type);
|
||||
// 切换回主线程执行后续操作
|
||||
createTrackedHandler(Looper.getMainLooper()).post(() -> {
|
||||
isPreload(mSongCode, type);
|
||||
});
|
||||
} catch (Exception e) {
|
||||
Log.e("AgoraManager", "Preload song failed: songCode=" + mSongCode, e);
|
||||
}
|
||||
}, 3000); // 3秒延迟
|
||||
});
|
||||
} else {
|
||||
handleMusicPlayerState(songCode);
|
||||
}
|
||||
} else if (type == 2) {
|
||||
if (code < 0) {
|
||||
String result = musicContentCenter.preload(mSongCode);
|
||||
// 创建定时器,3秒后再次检查
|
||||
new Handler(Looper.getMainLooper()).postDelayed(() -> {
|
||||
// 使用后台线程池处理预加载检查,避免阻塞UI线程
|
||||
executeInBackground(() -> {
|
||||
try {
|
||||
|
||||
Thread.sleep(3000); // 3秒延迟
|
||||
Log.d("AgoraManager", "Preload result: " + result);
|
||||
isPreload(mSongCode, type);
|
||||
// 切换回主线程执行后续操作
|
||||
createTrackedHandler(Looper.getMainLooper()).post(() -> {
|
||||
isPreload(mSongCode, type);
|
||||
});
|
||||
} catch (Exception e) {
|
||||
Log.e("AgoraManager", "Preload song failed: songCode=" + mSongCode, e);
|
||||
}
|
||||
}, 3000); // 3秒延迟
|
||||
});
|
||||
} else {
|
||||
try {
|
||||
musicPlayer.open(mSongCode, 0);
|
||||
@@ -1111,9 +1072,18 @@ public class AgoraManager {
|
||||
case PLAYER_STATE_IDLE:
|
||||
LogUtils.e("lxj", "空闲");
|
||||
musicPlayer.open(songCode, 0);
|
||||
new Handler(Looper.getMainLooper()).postDelayed(() -> {
|
||||
isPreload(songCode, 1);
|
||||
}, 2000); // 2秒延迟
|
||||
// 使用后台线程处理延迟任务,避免阻塞UI线程
|
||||
executeInBackground(() -> {
|
||||
try {
|
||||
Thread.sleep(2000); // 2秒延迟
|
||||
// 切换回主线程执行后续操作
|
||||
createTrackedHandler(Looper.getMainLooper()).post(() -> {
|
||||
isPreload(songCode, 1);
|
||||
});
|
||||
} catch (Exception e) {
|
||||
Log.e("AgoraManager", "Error in idle state handling", e);
|
||||
}
|
||||
});
|
||||
break;
|
||||
case PLAYER_STATE_OPEN_COMPLETED:
|
||||
LogUtils.e("lxj", "播放");
|
||||
@@ -1151,143 +1121,7 @@ public class AgoraManager {
|
||||
}
|
||||
}
|
||||
|
||||
public static void isPreload2(long songCode, int type) {
|
||||
mSongCode = songCode;
|
||||
|
||||
// 确保 rtcEngine 已初始化
|
||||
if (rtcEngine == null) {
|
||||
Log.e("AgoraManager", "RtcEngine not initialized, cannot preload music");
|
||||
return;
|
||||
}
|
||||
|
||||
// 初始化 musicContentCenter 如果为 null
|
||||
if (musicContentCenter == null) {
|
||||
try {
|
||||
musicContentCenter = IAgoraMusicContentCenter.create(rtcEngine);
|
||||
if (musicContentCenter == null) {
|
||||
Log.e("AgoraManager", "Failed to create musicContentCenter");
|
||||
return;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e("AgoraManager", "Exception creating musicContentCenter", e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化 musicPlayer 如果为 null
|
||||
if (musicPlayer == null) {
|
||||
try {
|
||||
musicPlayer = musicContentCenter.createMusicPlayer();
|
||||
if (musicPlayer == null) {
|
||||
Log.e("AgoraManager", "Failed to create musicPlayer");
|
||||
return;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e("AgoraManager", "Exception creating musicPlayer", e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
int code;
|
||||
try {
|
||||
code = musicContentCenter.isPreloaded(mSongCode);
|
||||
} catch (Exception e) {
|
||||
Log.e("AgoraManager", "Exception checking if song is preloaded", e);
|
||||
return;
|
||||
}
|
||||
|
||||
if (type == 1) {
|
||||
if (code < 0) {
|
||||
// 创建定时器,3秒后再次检查
|
||||
new Handler(Looper.getMainLooper()).postDelayed(() -> {
|
||||
try {
|
||||
String result = musicContentCenter.preload(mSongCode);
|
||||
Log.d("AgoraManager", "Preload result: " + result);
|
||||
isPreload2(mSongCode, type);
|
||||
} catch (Exception e) {
|
||||
Log.e("AgoraManager", "Preload song failed: songCode=" + mSongCode, e);
|
||||
}
|
||||
}, 3000); // 3秒延迟
|
||||
} else {
|
||||
handleMusicPlayerState2(songCode);
|
||||
}
|
||||
} else if (type == 2) {
|
||||
if (code < 0) {
|
||||
// 创建定时器,3秒后再次检查
|
||||
new Handler(Looper.getMainLooper()).postDelayed(() -> {
|
||||
try {
|
||||
String result = musicContentCenter.preload(mSongCode);
|
||||
Log.d("AgoraManager", "Preload result: " + result);
|
||||
isPreload2(mSongCode, type);
|
||||
} catch (Exception e) {
|
||||
Log.e("AgoraManager", "Preload song failed: songCode=" + mSongCode, e);
|
||||
}
|
||||
}, 3000); // 3秒延迟
|
||||
} else {
|
||||
try {
|
||||
musicPlayer.open(mSongCode, 0);
|
||||
} catch (Exception e) {
|
||||
Log.e("AgoraManager", "Open music failed: songCode=" + mSongCode, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 提取音乐播放器状态处理逻辑到单独的方法
|
||||
private static void handleMusicPlayerState2(long songCode) {
|
||||
if (musicPlayer == null) {
|
||||
Log.e("AgoraManager", "Music player is null, cannot handle state");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
io.agora.mediaplayer.Constants.MediaPlayerState state = musicPlayer.getState();
|
||||
switch (state) {
|
||||
case PLAYER_STATE_PAUSED:
|
||||
LogUtils.e("lxj", "暂停");
|
||||
musicPlayer.resume();
|
||||
break;
|
||||
case PLAYER_STATE_IDLE:
|
||||
LogUtils.e("lxj", "空闲");
|
||||
musicPlayer.open(songCode, 0);
|
||||
new Handler(Looper.getMainLooper()).postDelayed(() -> {
|
||||
isPreload2(songCode, 1);
|
||||
}, 3000); // 3秒延迟
|
||||
break;
|
||||
case PLAYER_STATE_OPEN_COMPLETED:
|
||||
LogUtils.e("lxj", "播放");
|
||||
musicPlayer.play();
|
||||
// musicPlayer.selectAudioTrack(0);
|
||||
break;
|
||||
case PLAYER_STATE_PAUSING_INTERNAL:
|
||||
LogUtils.e("lxj", "关闭");
|
||||
musicPlayer.pause();
|
||||
break;
|
||||
case PLAYER_STATE_PLAYBACK_ALL_LOOPS_COMPLETED:
|
||||
LogUtils.e("lxj", "daki");
|
||||
musicPlayer.open(songCode, 0);
|
||||
isPreload2(songCode, 1);
|
||||
break;
|
||||
case PLAYER_STATE_OPENING:
|
||||
LogUtils.e("lxj", state.toString());
|
||||
// isPreload2(songCode, 1);
|
||||
break;
|
||||
case PLAYER_STATE_PLAYING:
|
||||
LogUtils.e("lxj", state.toString());
|
||||
isPreload2(songCode, 1);
|
||||
break;
|
||||
case PLAYER_STATE_STOPPED:
|
||||
LogUtils.e("lxj", state.toString());
|
||||
isPreload2(songCode, 1);
|
||||
break;
|
||||
default:
|
||||
LogUtils.e("lxj", "未知状态: " + state);
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e("AgoraManager", "Exception handling music player state", e);
|
||||
}
|
||||
}
|
||||
// 删除重复的isPreload2方法,统一使用isPreload方法
|
||||
|
||||
|
||||
public void play() {
|
||||
@@ -1425,14 +1259,23 @@ public class AgoraManager {
|
||||
}
|
||||
}
|
||||
|
||||
public static void nextSong() {
|
||||
public void nextSong() {
|
||||
if (musicList != null && musicList.size() > 0) {
|
||||
stopMuisc();
|
||||
new Handler(Looper.getMainLooper()).postDelayed(() -> {
|
||||
EventBus.getDefault().post(musicList.get(0));
|
||||
isPreload2(musicList.get(0).songCode, 1);
|
||||
musicList.remove(musicList.get(0));
|
||||
}, 3000); // 3秒延迟
|
||||
// 使用后台线程处理延迟任务,避免阻塞UI线程
|
||||
executeInBackground(() -> {
|
||||
try {
|
||||
Thread.sleep(3000); // 3秒延迟
|
||||
// 切换回主线程执行UI更新操作
|
||||
createTrackedHandler(Looper.getMainLooper()).post(() -> {
|
||||
EventBus.getDefault().post(musicList.get(0));
|
||||
isPreload(musicList.get(0).songCode, 1);
|
||||
musicList.remove(musicList.get(0));
|
||||
});
|
||||
} catch (Exception e) {
|
||||
Log.e("AgoraManager", "Error in nextSong", e);
|
||||
}
|
||||
});
|
||||
|
||||
} else {
|
||||
stopMuisc();
|
||||
@@ -1517,12 +1360,30 @@ public class AgoraManager {
|
||||
}
|
||||
|
||||
public static byte[] convertFileToByteArray(File file) {
|
||||
if (file == null || !file.exists()) {
|
||||
Log.e("AgoraManager", "File is null or does not exist");
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] byteArray = null;
|
||||
try (FileInputStream fis = new FileInputStream(file)) {
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
fis = new FileInputStream(file);
|
||||
byteArray = new byte[(int) file.length()];
|
||||
fis.read(byteArray);
|
||||
int bytesRead = fis.read(byteArray);
|
||||
if (bytesRead != byteArray.length) {
|
||||
Log.w("AgoraManager", "Could not read entire file: " + file.getPath());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Log.e("AgoraManager", "Error converting file to byte array", e);
|
||||
} finally {
|
||||
if (fis != null) {
|
||||
try {
|
||||
fis.close();
|
||||
} catch (Exception e) {
|
||||
Log.e("AgoraManager", "Error closing file input stream", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
return byteArray;
|
||||
}
|
||||
|
||||
@@ -36,62 +36,35 @@ import io.agora.rtc2.video.VideoCanvas;
|
||||
public class AgoraManagerEx {
|
||||
|
||||
private static volatile AgoraManagerEx instance;
|
||||
private static RtcEngineEx rtcEngineEx;
|
||||
// private RtcEngineEx rtcEngineEx;
|
||||
private static Context context;
|
||||
private boolean isLocalAudioEnabled = true; // 默认开启麦克风
|
||||
// private final List<IRtcEngineEventHandler> eventHandlers = new ArrayList<>();
|
||||
private static IAgoraMusicContentCenter musicContentCenter;
|
||||
private static IAgoraMusicPlayer musicPlayer;
|
||||
private static MusicContentCenterConfiguration contentCenterConfiguration;
|
||||
private static String mRoomId = "";
|
||||
private static long mSongCode;
|
||||
|
||||
private static List<Music> musicList;
|
||||
private static boolean isBjMusic = false;
|
||||
private static List<SoundLevelUpdateListener> soundLevelUpdateListeners = new CopyOnWriteArrayList<>();
|
||||
private static ChannelMediaOptions options;
|
||||
private static RtcConnection connection;
|
||||
private RtcEngineEx rtcEngineEx;
|
||||
private Context context;
|
||||
private List<SoundLevelUpdateListener> soundLevelUpdateListeners = new CopyOnWriteArrayList<>();
|
||||
private ChannelMediaOptions options;
|
||||
private RtcConnection connection;
|
||||
private String pkRoomId = "";
|
||||
private static String lastRoomId;
|
||||
|
||||
public int pkUserId;
|
||||
|
||||
public int getPkUserId() {
|
||||
return pkUserId;
|
||||
}
|
||||
|
||||
public void setPkUserId(int pkUserId) {
|
||||
this.pkUserId = pkUserId;
|
||||
}
|
||||
private static boolean isOnJoin = false;
|
||||
public void setLastRoomId(String value) {
|
||||
lastRoomId = value;
|
||||
}
|
||||
|
||||
public String getLastRoomId() {
|
||||
LogUtils.e("上次的房间id:" + lastRoomId);
|
||||
return lastRoomId;
|
||||
}
|
||||
|
||||
private AgoraManagerEx() {
|
||||
|
||||
}
|
||||
|
||||
public static AgoraManagerEx getInstance(Context con) {
|
||||
public static AgoraManagerEx getInstance() {
|
||||
if (instance == null) {
|
||||
synchronized (AgoraManagerEx.class) {
|
||||
if (instance == null) {
|
||||
instance = new AgoraManagerEx();
|
||||
context = con.getApplicationContext(); // 使用ApplicationContext避免内存泄漏
|
||||
instance.context = CommonAppContext.getInstance(); // 使用ApplicationContext避免内存泄漏
|
||||
}
|
||||
}
|
||||
}
|
||||
// 确保引擎已初始化
|
||||
if (rtcEngineEx == null) {
|
||||
if (instance.rtcEngineEx == null) {
|
||||
synchronized (AgoraManagerEx.class) {
|
||||
if (rtcEngineEx == null) {
|
||||
init(CommonAppContext.getInstance().getCurrentEnvironment().getSwSdkAppId());
|
||||
if (instance.rtcEngineEx == null) {
|
||||
instance.init(CommonAppContext.getInstance().getCurrentEnvironment().getSwSdkAppId());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -99,72 +72,20 @@ public class AgoraManagerEx {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static void init(String appId) {
|
||||
public void init(String appId) {
|
||||
if (rtcEngineEx != null) {
|
||||
return;
|
||||
};
|
||||
}
|
||||
|
||||
synchronized (AgoraManager.class) {
|
||||
synchronized (AgoraManagerEx.class) {
|
||||
if (rtcEngineEx != null) {
|
||||
return;
|
||||
}
|
||||
rtcEngineEx =AgoraManager.getInstance( context).rtcEngine;
|
||||
|
||||
// try {
|
||||
// RtcEngineConfig config = new RtcEngineConfig();
|
||||
// config.mContext = context;
|
||||
// config.mAppId = appId;
|
||||
// config.mEventHandler = getDefaultEventHandler();
|
||||
// config.mChannelProfile = Constants.CHANNEL_PROFILE_LIVE_BROADCASTING;
|
||||
// config.mAudioScenario = Constants.AUDIO_SCENARIO_CHORUS;
|
||||
// config.mAreaCode = 1;
|
||||
//
|
||||
// rtcEngineEx = (RtcEngineEx) RtcEngineEx.create(config);
|
||||
// } catch (Exception e) {
|
||||
// Log.e("AgoraManager", "Failed to create RtcEngine", e);
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// if (rtcEngineEx != null) {
|
||||
// try {
|
||||
// rtcEngineEx.setAudioProfile(Constants.AUDIO_PROFILE_MUSIC_HIGH_QUALITY_STEREO,
|
||||
// Constants.AUDIO_SCENARIO_GAME_STREAMING);
|
||||
// rtcEngineEx.enableAudioVolumeIndication(200, 3, true);
|
||||
// rtcEngineEx.setClientRole(Constants.CLIENT_ROLE_BROADCASTER);
|
||||
// rtcEngineEx.muteLocalAudioStream(true); // 默认静音
|
||||
// rtcEngineEx.muteAllRemoteAudioStreams(false); // 监听远端的音频
|
||||
//
|
||||
// // 设置参数时添加异常处理
|
||||
// try {
|
||||
// rtcEngineEx.setParameters("{\"che.audio.custom_payload_type\":73}");
|
||||
// rtcEngineEx.setParameters("{\"che.audio.custom_bitrate\":128000}");
|
||||
// rtcEngineEx.setParameters("{\"rtc.enable_nasa2\": true}");
|
||||
// rtcEngineEx.setParameters("{\"rtc.use_audio4\": true}");
|
||||
// rtcEngineEx.setParameters("{" +
|
||||
// "\"rtc.report_app_scenario\":" +
|
||||
// "{" +
|
||||
// "\"appScenario\":" + 100 + "," +
|
||||
// "\"serviceType\":" + 11 + "," +
|
||||
// "\"appVersion\":\"" + rtcEngineEx.getSdkVersion() + "\"" +
|
||||
// "}" +
|
||||
// "}");
|
||||
// rtcEngineEx.setParameters("{\"che.video.mobile_1080p\":true}");
|
||||
// } catch (Exception e) {
|
||||
// Log.w("AgoraManager", "Failed to set parameters", e);
|
||||
// }
|
||||
// /*Enable video module*/
|
||||
// rtcEngineEx.enableVideo();
|
||||
// /*Set up to play remote sound with receiver*/
|
||||
// rtcEngineEx.setDefaultAudioRoutetoSpeakerphone(true);
|
||||
//
|
||||
// } catch (Exception e) {
|
||||
// Log.e("AgoraManager", "Failed to configure rtcEngineEx", e);
|
||||
// }
|
||||
// }
|
||||
rtcEngineEx = AgoraManager.getInstance().rtcEngine;
|
||||
}
|
||||
}
|
||||
|
||||
private static IRtcEngineEventHandler getDefaultEventHandler() {
|
||||
private IRtcEngineEventHandler getDefaultEventHandler() {
|
||||
return new IRtcEngineEventHandler() {
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user