225 lines
6.2 KiB
Objective-C
225 lines
6.2 KiB
Objective-C
//
|
||
// ZegoRtcImpl.m
|
||
// xplan-ios
|
||
//
|
||
// Created by zu on 2021/10/20.
|
||
//
|
||
|
||
#import "ZegoRtcImpl.h"
|
||
#import <ZegoAudioRoom/ZegoAudioRoom.h>
|
||
|
||
@interface ZegoRtcImpl()<ZegoAudioRoomDelegate, ZegoAudioLivePublisherDelegate, ZegoSoundLevelDelegate, ZegoMediaPlayerEventDelegate>
|
||
|
||
@property (nonatomic, strong) ZegoAudioRoomApi *engine;
|
||
@property (nonatomic, strong) ZegoMediaPlayer *mediaPlayer;
|
||
///当前播放的背景音乐路径
|
||
@property (nonatomic,copy) NSString *filePath;
|
||
@end
|
||
|
||
@implementation ZegoRtcImpl
|
||
|
||
- (instancetype)initWithDelegate:(id<RtcImplDelegate>)delegate {
|
||
self = [super initWithDelegate:delegate];
|
||
if (self) {
|
||
#ifdef DEBUG
|
||
// 调试信息开关
|
||
[ZegoAudioRoomApi setVerbose:YES];
|
||
[ZegoAudioRoomApi setUseTestEnv:YES];
|
||
#else
|
||
[ZegoAudioRoomApi setUseTestEnv:NO];
|
||
#endif
|
||
[ZegoAudioRoomApi setAudioDeviceMode:ZEGOAPI_AUDIO_DEVICE_MODE_GENERAL];
|
||
//采样率48k
|
||
[ZegoAudioRoomApi setConfig:@"audio_encoder_samplerate=48000"];
|
||
_engine = [[ZegoAudioRoomApi alloc] initWithAppID:[self appID] appSignature:[self zegoAppSign]];
|
||
[_engine enableAEC:true];
|
||
[_engine enableAGC:true];
|
||
//手动发布
|
||
[_engine setManualPublish:true];
|
||
[_engine setManualPlay:true];
|
||
//delegate
|
||
[_engine setAudioRoomDelegate:self];
|
||
[_engine setAudioPublisherDelegate:self];
|
||
}
|
||
return self;
|
||
}
|
||
|
||
#pragma mark - RtcInterface impl
|
||
- (BOOL)joinChannel:(nonnull NSString *)channelId completion:(void (^ _Nullable)(void))completion {
|
||
//推流播放模式,会将音频混流推流中,调用端和拉流端都可以听到播放的声音。
|
||
self.mediaPlayer = [[ZegoMediaPlayer alloc] initWithPlayerType:MediaPlayerTypeAux];
|
||
[self.mediaPlayer setDelegate:self];
|
||
//开始监听
|
||
[[ZegoSoundLevel sharedInstance] setSoundLevelDelegate:self];
|
||
[[ZegoSoundLevel sharedInstance] setSoundLevelMonitorCycle:1200]; //监控周期 [100, 3000]。默认 200 ms。
|
||
[[ZegoSoundLevel sharedInstance] startSoundLevelMonitor];
|
||
NSString *uid = [AccountInfoStorage instance].getUid;
|
||
//设置用户id跟name
|
||
[ZegoAudioRoomApi setUserID:uid userName:uid];
|
||
|
||
[self.engine setLatencyMode:ZEGOAPI_LATENCY_MODE_LOW3];
|
||
//码率
|
||
[self.engine setAudioBitrate:128000];
|
||
// 关闭双声道采集(0: 使用单声道进行采集; ≠0: 始终使用双声道进行采集)
|
||
[ZegoAudioDevice enableAudioCaptureStereo:0];
|
||
// 设置推流音频单声道
|
||
[self.engine setAudioChannelCount:1];
|
||
|
||
//加入zego
|
||
int state = [self.engine loginRoom:channelId completionBlock:^(int errorCode) {
|
||
if (completion) {
|
||
completion();
|
||
}
|
||
}];
|
||
return state == 1;
|
||
}
|
||
|
||
- (BOOL)muteRemote:(BOOL)mute {
|
||
return [self.engine enableSpeaker:!mute];
|
||
}
|
||
|
||
- (void)broadcast:(BOOL)on {
|
||
if (on) {
|
||
[self.engine startPublish];
|
||
} else {
|
||
[self.engine stopPublish];
|
||
}
|
||
}
|
||
|
||
- (BOOL)muteLocal:(BOOL)mute {
|
||
return [self.engine enableMic:!mute];
|
||
}
|
||
|
||
- (void)exitChannel:(void (^ _Nullable)(void))completion {
|
||
[[ZegoSoundLevel sharedInstance] stopSoundLevelMonitor];
|
||
[self.engine logoutRoom];
|
||
if (completion) {
|
||
completion();
|
||
}
|
||
}
|
||
|
||
- (void)destory {
|
||
|
||
}
|
||
|
||
/// 播放背景音乐
|
||
/// @param filePath 音频文件的地址
|
||
/// @param musicId TRTC自己要的
|
||
- (BOOL)playBackMusic:(NSString *)filePath musicId:(int)musicId {
|
||
[self.mediaPlayer stop];//切换歌曲need stop
|
||
if (filePath) {
|
||
self.filePath = filePath;
|
||
[self.engine muteAux:true];
|
||
[self.mediaPlayer start:filePath repeat:NO];
|
||
}
|
||
return YES;
|
||
}
|
||
|
||
/// 改变播放器的状态
|
||
/// @param state 播放状态
|
||
- (BOOL)changePlayState:(BackMusicPlayState)state {
|
||
BOOL isPlaying = NO;
|
||
switch (state) {
|
||
case BackMusicPlayState_Stop:
|
||
{
|
||
[self.mediaPlayer stop];
|
||
[self.engine muteAux:false];
|
||
isPlaying = NO;
|
||
}
|
||
break;
|
||
case BackMusicPlayState_Pause:
|
||
{
|
||
[self.mediaPlayer pause];
|
||
isPlaying = NO;
|
||
}
|
||
break;
|
||
case BackMusicPlayState_Resume:
|
||
{
|
||
[self.mediaPlayer resume];
|
||
isPlaying = NO;
|
||
}
|
||
default:
|
||
break;
|
||
}
|
||
return isPlaying;
|
||
}
|
||
|
||
- (void)updateUserSound:(int)soundVol {
|
||
if (soundVol) {
|
||
[self.engine setCaptureVolume:soundVol];
|
||
}
|
||
}
|
||
|
||
- (void)updateMusicSound:(int)soundVol {
|
||
if (soundVol) {
|
||
[self.mediaPlayer setVolume:soundVol];
|
||
}
|
||
}
|
||
|
||
#pragma mark - ZegoAudioRoomDelegate
|
||
- (void)onStreamUpdated:(ZegoAudioStreamType)type stream:(ZegoAudioStream *)stream {
|
||
switch (type) {
|
||
case ZEGO_AUDIO_STREAM_ADD:
|
||
{
|
||
[self.engine startPlayStream:stream.streamID];
|
||
}
|
||
break;
|
||
case ZEGO_AUDIO_STREAM_DELETE:
|
||
{
|
||
[self.engine stopPlayStream:stream.streamID];
|
||
}
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
- (void)onPublishStateUpdate:(int)stateCode streamID:(NSString *)streamID streamInfo:(NSDictionary *)info {
|
||
if (stateCode != 0) {
|
||
[self.engine restartPublishStream];
|
||
}
|
||
}
|
||
|
||
- (void)onSoundLevelUpdate:(NSArray<ZegoSoundLevelInfo *> *)soundLevels {
|
||
[self.delegate usersSpeaking:[self formatSpeakingUids:soundLevels]];
|
||
}
|
||
|
||
- (void)onCaptureSoundLevelUpdate:(ZegoSoundLevelInfo *)captureSoundLevel {
|
||
[self.delegate usersSpeaking:[self formatSpeakingUids:@[captureSoundLevel]]];
|
||
}
|
||
|
||
#pragma mark - ZegoMediaPlayerEventDelegate
|
||
///播放结束
|
||
- (void)onPlayEnd {
|
||
[self.delegate currentBackMusicPlayFinish:self.filePath];
|
||
}
|
||
|
||
#pragma mark - private method
|
||
- (NSMutableArray *)formatSpeakingUids:(NSArray<ZegoSoundLevelInfo *> *)soundLevels {
|
||
NSMutableArray *uids = [NSMutableArray array];
|
||
for (ZegoSoundLevelInfo *userInfo in soundLevels) {
|
||
NSString *uid = nil;
|
||
NSArray *strArray = [userInfo.streamID componentsSeparatedByString:@"-"];
|
||
if (strArray.count >= 2) {
|
||
uid = [strArray objectAtIndex:1];
|
||
if (userInfo.soundLevel > 0) {
|
||
[uids addObject:uid];
|
||
}
|
||
}
|
||
}
|
||
return uids;
|
||
}
|
||
|
||
- (uint32_t)appID {
|
||
return 1067458582;
|
||
}
|
||
|
||
- (NSData *)zegoAppSign {
|
||
Byte signkey[] = {0x2b,0x86,0x24,0xef,0xd9,0x96,0xf1,0x1c,0x6b,0xa2,0x28,0xc3,0xef,0x24,0xdd,0x64,0x2e,0xd7,0x33,0x3f,0x33,0x90,0x07,0x53,0xeb,0xd2,0xd2,0x4e,0xc5,0xed,0xfd,0x43};
|
||
return [NSData dataWithBytes:signkey length:32];
|
||
}
|
||
|
||
|
||
|
||
@end
|