Files
peko-ios/YuMi/Modules/YMRTC/RtcImpl/AgoraRtcImpl.m
2024-01-06 10:22:25 +08:00

194 lines
6.0 KiB
Objective-C

//
// AgoraRtcImpl.m
// YUMI
//
// Created by zu on 2021/10/19.
//
#import "AgoraRtcImpl.h"
#import "YUMIConstant.h"
#import <AgoraRtcKit/AgoraRtcEngineKit.h>
#import "RtcInterface.h"
@interface AgoraRtcImpl()<AgoraRtcEngineDelegate>
@property(nonatomic, strong) AgoraRtcEngineKit *engine;
///音乐播放完成
@property (nonatomic,copy) void(^MusicCompletion)(NSString *filePath);
///播放的路径
@property (nonatomic,copy) NSString *filePath;
@end
@implementation AgoraRtcImpl
- (instancetype)initWithDelegate:(id<RtcImplDelegate>)delegate {
self = [super initWithDelegate:delegate];
if (self) {
_engine = [AgoraRtcEngineKit sharedEngineWithAppId:KeyWithType(KeyType_Agora) delegate:self];
[_engine setChannelProfile:AgoraChannelProfileLiveBroadcasting];
// [_engine enableLastmileTest];
[_engine startLastmileProbeTest:nil];
[_engine setParameters:@"{\"che.audio.keep.audiosession\":true}"];
[_engine enableAudio];
[_engine disableVideo];
[_engine enableAudioVolumeIndication:900 smooth:3 reportVad:YES];
[_engine setAudioProfile:AgoraAudioProfileMusicStandard];
[_engine setAudioScenario:AgoraAudioScenarioGameStreaming];
#ifdef DEBUG
[_engine setLogFilter:AgoraLogFilterInfo];
#else
[_engine setLogFilter:0];
#endif
}
return self;
}
#pragma mark - RtcInterface impl
- (BOOL)joinChannel:(nonnull NSString *)channelId completion:(void (^ _Nullable)(void))completion {
[self.engine setClientRole:AgoraClientRoleAudience];
int code = [self.engine joinChannelByToken:nil channelId:channelId info:nil uid:[[[AccountInfoStorage instance] getUid] integerValue] joinSuccess:^(NSString * _Nonnull channel, NSUInteger uid, NSInteger elapsed) {
if (completion) {
completion();
}
}];
return code == 0;
}
/// 静音某个人
/// @param userId 用户id
- (BOOL)muteRemote:(BOOL)mute userId:(NSString *)userId {
return [self.engine muteRemoteAudioStream:[userId integerValue] mute:mute];
}
- (BOOL)muteRemote:(BOOL)mute {
return [self.engine muteAllRemoteAudioStreams:mute] == 0;
}
- (void)broadcast:(BOOL)on {
[self.engine setClientRole:on ? AgoraClientRoleBroadcaster : AgoraClientRoleAudience];
}
- (BOOL)muteLocal:(BOOL)mute {
return [self.engine muteLocalAudioStream:mute] == 0;
}
- (void)exitChannel:(void (^ _Nullable)(void))completion {
[self.engine leaveChannel:^(AgoraChannelStats * _Nonnull stat) {
if (completion) {
completion();
}
}];
}
///发起跨房通话
- (void)connectOtherRoom:(NSString *)roomUid userId:(NSString *)uid {
AgoraChannelMediaRelayConfiguration *config = [[AgoraChannelMediaRelayConfiguration alloc]init];
config.sourceInfo = [[AgoraChannelMediaRelayInfo alloc]initWithToken:nil];
AgoraChannelMediaRelayInfo *destinationInfo = [[AgoraChannelMediaRelayInfo alloc]initWithToken:nil];
[config setDestinationInfo:destinationInfo forChannelName:roomUid];
[self.engine startOrUpdateChannelMediaRelay:config];
}
///退出跨房通话
- (void)disconnectOtherRoom {
[self.engine stopChannelMediaRelay];
}
- (void)destory {
[AgoraRtcEngineKit destroy];
}
/// 播放背景音乐
/// @param filePath 音频文件的地址
/// @param musicId TRTC自己要的
- (BOOL)playBackMusic:(NSString *)filePath musicId:(int)musicId completion:(nonnull void (^)(NSString * _Nonnull))completion {
if (filePath) {
self.MusicCompletion = completion;
[self changePlayState:BackMusicPlayState_Stop];//切换歌曲need stop
// [self.engine startAudioMixing:filePath loopback:NO replace:NO cycle:1];
[self.engine startAudioMixing:filePath loopback:NO cycle:1];
return YES;
} else {
return NO;
}
}
/// 改变播放器的状态
/// @param state 播放状态
- (BOOL)changePlayState:(BackMusicPlayState)state {
BOOL isPlaying = NO;
switch (state) {
case BackMusicPlayState_Stop:
{
[self.engine stopAudioMixing];
isPlaying = NO;
}
break;
case BackMusicPlayState_Pause:
{
[self.engine pauseAudioMixing];
isPlaying = NO;
}
break;
case BackMusicPlayState_Resume:
{
[self.engine resumeAudioMixing];
isPlaying = YES;
}
default:
break;
}
return isPlaying;
}
- (void)updateUserSound:(int)soundVol {
[self.engine adjustRecordingSignalVolume:soundVol];
}
- (void)updateMusicSound:(int)soundVol {
[self.engine adjustAudioMixingVolume:soundVol];
}
#pragma mark - AgoraRtcEngineDelegate
///监听跨房语音
- (void)rtcEngine:(AgoraRtcEngineKit * _Nonnull)engine
channelMediaRelayStateDidChange:(AgoraChannelMediaRelayState)state
error:(AgoraChannelMediaRelayError)error NS_SWIFT_NAME(rtcEngine(_:channelMediaRelayStateDidChange:error:)){
switch (state) {
case AgoraChannelMediaRelayStateRunning:
break;
case AgoraChannelMediaRelayStateFailure:
break;
case AgoraChannelMediaRelayStateIdle:
break;
default:
break;
}
}
- (void)rtcEngine:(AgoraRtcEngineKit *)engine reportAudioVolumeIndicationOfSpeakers:(NSArray *)speakers totalVolume:(NSInteger)totalVolume {
NSMutableArray *uids = [NSMutableArray array];
for (AgoraRtcAudioVolumeInfo *userInfo in speakers) {
NSString *uid = [NSString stringWithFormat:@"%ld", (long)userInfo.uid];
if (userInfo.volume > 15){
if (uid.integerValue == 0) {
[uids addObject:[[AccountInfoStorage instance] getUid]];
}else {
[uids addObject:uid];
}
}
}
[self.delegate usersSpeaking:uids];
}
- (void)rtcEngineLocalAudioMixingDidFinish:(AgoraRtcEngineKit *)engine {
if (self.MusicCompletion) {
self.MusicCompletion(self.filePath);
}
///这个用在最小化的时候 因为最小化没办法知道当前歌曲是否播放完毕了 目前没有想到其他的好的版本就用通知吧
[[NSNotificationCenter defaultCenter] postNotificationName:kRoomBackMusicPlayMusicFinishKey object:self.filePath];
}
@end