123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682 |
- //
- // XYChatRoomRTCManager.m
- // Starbuds
- //
- // Created by 翟玉磊 on 2020/7/9.
- // Copyright © 2020 翟玉磊. All rights reserved.
- //
- #import "XYChatRoomRTCManager.h"
- #import <ZegoExpressEngine/ZegoExpressEngine.h>
- typedef void(^ChatRoomRTCBlock)(NSInteger errCode, NSString *errMsg);
- @interface XYChatRoomRTCManager ()<ZegoEventHandler, ZegoMediaPlayerEventHandler>
- @property (nonatomic, copy) ChatRoomRTCBlock chatRoomRTCBlock;
- @property (nonatomic, copy) NSString *userId;
- @property (nonatomic, copy) NSString *streamId;
- @property (nonatomic, copy) NSString *currentPlayMusicId;
- @property (nonatomic, strong) ZegoMediaPlayer *mediaPlayer;
- /// 是否启用麦克风(音频采集)YES打开输出设备 NO关闭输出设备
- @property (nonatomic, assign) BOOL enableMicrophone;
- /// 跨房pk的流id数组
- @property (nonatomic, strong) NSMutableArray *kfPkStreamIds;
- @end
- @implementation XYChatRoomRTCManager
- - (void)clear {
- // 关闭播放器
- if (_mediaPlayer) {
- [_mediaPlayer stop];
- _mediaPlayer = nil;
- }
- // 销毁引擎
- [ZegoExpressEngine destroyEngine:nil];
- }
- - (instancetype)init {
- if (self = [super init]) {
-
- [self initValue];
- [self initRtcEngineKit];
- }
- return self;
- }
- - (void)initValue {
-
- self.kfPkMute = NO;
- self.kfPkStreamIds = [NSMutableArray array];
- self.enableMicrophone = NO;
- }
- - (void)initRtcEngineKit {
- NSLog(@"即构SDK版本:%@", [ZegoExpressEngine getVersion]);
-
- unsigned int appId = [[XYChannelInfoManager sharedInstance].JGAppId unsignedIntValue];
- NSString *appSign = [XYChannelInfoManager sharedInstance].JGAppSign;
- BOOL isTestEnv = NO;
- if ([ZYLService currentService].environment == ZYLServiceEnvironmentTest || [ZYLService currentService].environment == ZYLServiceEnvironmentDevelop || [ZYLService currentService].environment == ZYLServiceEnvironmentTestGy) {
- isTestEnv = YES;
- }
- // 设置房间模式为多房间模式
- // [ZegoExpressEngine setRoomMode:ZegoRoomModeMultiRoom];
-
- // 启动引擎
- // [ZegoExpressEngine createEngineWithAppID:appId appSign:appSign isTestEnv:isTestEnv scenario:ZegoScenarioGeneral eventHandler:self];
-
- ZegoEngineProfile *profile = [[ZegoEngineProfile alloc] init];
- profile.appID = appId;
- profile.appSign = appSign;
- //通用场景接入,请根据实际情况选择合适的场景
- profile.scenario = ZegoScenarioDefault;
- // 创建引擎,并注册 self 为 eventHandler 回调。不需要注册回调的话,eventHandler 参数可以传 nil,后续可调用 "-setEventHandler:" 方法设置回调
- [ZegoExpressEngine createEngineWithProfile:profile eventHandler:self];
-
- // 切换音频设备默认
- NSString *params = @"{\"method\":\"express.video.set_audio_device_mode\",\"params\":{\"mode\":6}}";
- [[ZegoExpressEngine sharedEngine] callExperimentalAPI:params];
-
- // 音频配置
- ZegoAudioConfig *audioConfig = [ZegoAudioConfig defaultConfig];
- audioConfig.codecID = ZegoAudioCodecIDLow3;
- [[ZegoExpressEngine sharedEngine] setAudioConfig:audioConfig];
-
- // 预览
- [[ZegoExpressEngine sharedEngine] startPreview];
- [[ZegoExpressEngine sharedEngine] stopPreview];
-
- // 先退出所有房间 防止异常关闭
- [[ZegoExpressEngine sharedEngine] logoutRoom];
- }
- - (void)setKfPKRoomId:(NSString *)kfPKRoomId {
- _kfPKRoomId = kfPKRoomId;
- if (StringIsEmpty(kfPKRoomId)) {
- // 停止拉流
- for (NSString *streamId in self.kfPkStreamIds) {
- [[ZegoExpressEngine sharedEngine] stopPlayingStream:streamId];
- }
- }
- [self.kfPkStreamIds removeAllObjects];
- }
- - (void)setKfPkMute:(BOOL)kfPkMute {
- _kfPkMute = kfPkMute;
- // 更新流静音状态
- for (NSString *streamId in self.kfPkStreamIds) {
- [[ZegoExpressEngine sharedEngine] mutePlayStreamAudio:kfPkMute streamID:streamId];
- }
- }
- /// 加入房间
- /// @param roomId 房间id
- /// @param userId 用户id
- - (void)joinRoomWithRoomId:(NSString *)roomId userId:(NSString *)userId token:(NSString *)token resultBlock:(void(^)(NSInteger errCode, NSString *errMsg))block {
-
- if (StringIsEmpty(roomId)) {
- return;
- }
-
- self.userId = [userId copy];
-
- if (block) {
- self.chatRoomRTCBlock = block;
- }
-
- // 创建用户对象
- ZegoUser *user = [ZegoUser userWithUserID:self.userId];
-
- // 房间配置
- ZegoRoomConfig *config = [ZegoRoomConfig defaultConfig];
- config.isUserStatusNotify = YES;
- if (StringIsNotEmpty(token)) {
- config.token = token;
- }
-
- // 开始登录房间
- [[ZegoExpressEngine sharedEngine] loginRoom:roomId user:user];
-
- // 启动声浪监控
- [[ZegoExpressEngine sharedEngine] startSoundLevelMonitor];
- }
- /// 退出房间
- /// @param roomId 房间id
- /// @param block 回调
- - (void)exitRoomWithRoomId:(NSString *)roomId resultBlock:(void(^)(NSInteger errCode, NSString *errMsg))block {
-
- if (block) {
- self.chatRoomRTCBlock = block;
- }
-
- // 停止声浪监控
- [[ZegoExpressEngine sharedEngine] stopSoundLevelMonitor];
-
- // 开始登出房间
- [[ZegoExpressEngine sharedEngine] logoutRoom:roomId];
- }
- /// 开始推流
- - (void)startPublishingStream:(NSString *)streamId roomId:(NSString *)roomId resultBlock:(void(^ _Nullable)(NSInteger errCode, NSString *errMsg))block {
-
- if (streamId.length == 0) {
- return;
- }
-
- self.streamId = streamId;
- self.chatRoomRTCBlock = block;
- // 开始推流
- ZegoPublisherConfig *config = [[ZegoPublisherConfig alloc] init];
- config.roomID = [roomId copy];
-
- [[ZegoExpressEngine sharedEngine] startPublishingStream:streamId config:config channel:ZegoPublishChannelMain];
- }
- /// 停止推流
- - (void)stopPublishingStreamResultBlock:(void(^ _Nullable)(NSInteger errCode, NSString *errMsg))block {
-
- self.chatRoomRTCBlock = block;
- // 停止推流
- [[ZegoExpressEngine sharedEngine] stopPublishingStream];
- }
- /// 检查音频输入是否静音
- - (BOOL)isSpeakerMuted {
- return [[ZegoExpressEngine sharedEngine] isSpeakerMuted];
- }
- /// 是否静音(关闭音频输入) YES:表示静音音频输入关闭 NO:表示音频输入没有关闭
- - (void)muteSpeaker:(BOOL)mute {
- [[ZegoExpressEngine sharedEngine] muteSpeaker:mute];
- }
- /// 检查麦克风是否关闭 YES关闭 NO开启
- - (BOOL)isMicrophoneMuted {
- return [[ZegoExpressEngine sharedEngine] isMicrophoneMuted];
- }
- /// 是否关闭麦克风 YES关闭 NO开启
- - (void)muteMicrophone:(BOOL)mute {
- self.enableMicrophone = !mute;
- [[ZegoExpressEngine sharedEngine] muteMicrophone:mute];
- if (_mediaPlayer) {
- // 禁止背景音乐输出
- [_mediaPlayer enableAux:!mute];
- }
- }
- /// 开/关音频采集设备 YES 表示打开音频采集设备;NO 表示关闭音频采集设备
- - (void)enableAudioCaptureDevice:(BOOL)enable {
- [[ZegoExpressEngine sharedEngine] enableAudioCaptureDevice:enable];
- self.enableMicrophone = enable;
- }
- /// 设置音频路由到扬声器
- /// @param defaultToSpeaker 是否使用内置扬声器播放声音,YES 表示使用内置扬声器播放声音,NO 表示使用当前系统调度的优先级最高的音频输出设备播放声音
- - (void)setAudioRouteToSpeaker:(BOOL)defaultToSpeaker {
- [[ZegoExpressEngine sharedEngine] setAudioRouteToSpeaker:defaultToSpeaker];
- }
- #pragma mark - ZegoEventHandler
- // 调试错误信息回调
- - (void)onDebugError:(int)errorCode funcName:(NSString *)funcName info:(NSString *)info {
- NSLog(@"即构SDK报错信息:errorCode:%d, info:%@", errorCode, info);
- NSString *errMsg = info;
- switch (errorCode) {
- case 1000002:
- errMsg = @"未登录房间";
- break;
- case 1002064:
- errMsg = @"房间ID已被登录房间接口使用,未退出房间之前,当前用户无法再登录该房间";
- break;
- default:
- break;
- }
- !self.chatRoomRTCBlock?:self.chatRoomRTCBlock(errorCode, errMsg);
- }
- // 音视频引擎状态更新的回调通知
- - (void)onEngineStateUpdate:(ZegoEngineState)state {
- if (state == ZegoEngineStateStart) {
- NSLog(@"即构SDK引擎正常");
- }else {
- NSLog(@"即构SDK引擎停止");
- }
- }
- // 房间状态变化通知
- - (void)onRoomStateChanged:(ZegoRoomStateChangedReason)reason errorCode:(int)errorCode extendedData:(NSDictionary *)extendedData roomID:(NSString *)roomID{
- NSLog(@"既构onRoomStateChanged=%d %d %@ %@,",reason,errorCode,extendedData,roomID);
- [SVProgressHUD showInfoWithStatus:[NSString stringWithFormat:@"既构onRoomStateChanged=%d %d %@ %@,",reason,errorCode,extendedData,roomID]];
- if (reason == ZegoRoomStateChangedReasonLogined && errorCode == 0) {
- NSLog(@"登录房间成功");
- !self.chatRoomRTCBlock?:self.chatRoomRTCBlock(0, @"");
- }
- if (reason == ZegoRoomStateChangedReasonKickOut && errorCode == 0) {
- NSLog(@"退出房间成功");
- !self.chatRoomRTCBlock?:self.chatRoomRTCBlock(0, @"");
- }
- if (errorCode != 0) {
- NSLog(@"房间状态变更失败");
- NSString *errMsg = @"请求失败";
- if (errorCode == 1002036) {
- // 没有开通多房间模式
- errMsg = @"没有开通多房间模式";
- }
- !self.chatRoomRTCBlock?:self.chatRoomRTCBlock(errorCode, [NSString stringWithFormat:@"%@ %d", errMsg, errorCode]);
- }
- }
- //- (void)onRoomStateUpdate:(ZegoRoomState)state errorCode:(int)errorCode extendedData:(NSDictionary *)extendedData roomID:(NSString *)roomID {
- // NSLog(@"既构onRoomStateUpdate=%d %d %@ %@,",state,errorCode,extendedData,roomID);
- // [SVProgressHUD showInfoWithStatus:[NSString stringWithFormat:@"既构onRoomStateUpdate=%d %d %@ %@,",state,errorCode,extendedData,roomID]];
- // if (state == ZegoRoomStateConnected && errorCode == 0) {
- // NSLog(@"登录房间成功");
- // !self.chatRoomRTCBlock?:self.chatRoomRTCBlock(0, @"");
- // }
- // if (state == ZegoRoomStateDisconnected && errorCode == 0) {
- // NSLog(@"退出房间成功");
- // !self.chatRoomRTCBlock?:self.chatRoomRTCBlock(0, @"");
- // }
- // if (errorCode != 0) {
- // NSLog(@"房间状态变更失败");
- // NSString *errMsg = @"请求失败";
- // if (errorCode == 1002036) {
- // // 没有开通多房间模式
- // errMsg = @"没有开通多房间模式";
- // }
- // !self.chatRoomRTCBlock?:self.chatRoomRTCBlock(errorCode, [NSString stringWithFormat:@"%@ %d", errMsg, errorCode]);
- // }
- //}
- // 房间内其他用户增加或减少的回调通知
- - (void)onRoomUserUpdate:(ZegoUpdateType)updateType userList:(NSArray<ZegoUser *> *)userList roomID:(NSString *)roomID {
- if (updateType == ZegoUpdateTypeAdd) {
- }
- if (updateType == ZegoUpdateTypeDelete) {
- }
- }
- // 房间内当前在线用户数量回调
- - (void)onRoomOnlineUserCountUpdate:(int)count roomID:(NSString *)roomID {
-
- }
- // 相同房间内其他用户推的流增加或减少的通知
- - (void)onRoomStreamUpdate:(ZegoUpdateType)updateType streamList:(NSArray<ZegoStream *> *)streamList extendedData:(NSDictionary *)extendedData roomID:(NSString *)roomID {
- for (ZegoStream *stream in streamList) {
- if (updateType == ZegoUpdateTypeAdd) {
- // 开始拉流
- ZegoPlayerConfig *config = [[ZegoPlayerConfig alloc] init];
- config.roomID = roomID;
- [[ZegoExpressEngine sharedEngine] startPlayingStream:stream.streamID config:config];
-
- // 跨发PK静音控制
- if ([roomID isEqualToString:self.kfPKRoomId]) {
- [self.kfPkStreamIds addObject:stream.streamID];
- if (self.kfPkMute) {
- [[ZegoExpressEngine sharedEngine] mutePlayStreamAudio:self.kfPkMute streamID:stream.streamID];
- }
- }
- }
- if (updateType == ZegoUpdateTypeDelete) {
- // 停止拉流
- [[ZegoExpressEngine sharedEngine] stopPlayingStream:stream.streamID];
- }
- }
- }
- // 房间内流附加信息更新通知
- - (void)onRoomStreamExtraInfoUpdate:(NSArray<ZegoStream *> *)streamList roomID:(NSString *)roomID {
-
- }
- // 房间附加信息更新通知
- - (void)onRoomExtraInfoUpdate:(NSArray<ZegoRoomExtraInfo *> *)roomExtraInfoList roomID:(NSString *)roomID {
-
- }
- // 房间 Token 鉴权将要过期的回调通知
- - (void)onRoomTokenWillExpire:(int)remainTimeInSecond roomID:(NSString *)roomID {
-
- [[XYVoiceRoomAPIManager new] getRefreshTokenWithRoomId:roomID successHandler:^(ZYLResponseModel *responseModel) {
- NSDictionary *authInfo = responseModel.data[@"authInfo"];
- if (authInfo) {
- NSString *token = authInfo[@"token"];
- if (StringIsNotEmpty(token)) {
- // 刷新token
- [[ZegoExpressEngine sharedEngine] renewToken:token roomID:roomID];
- }
- }
- } failureHandler:^(ZYLNetworkError *error) {
- }];
- }
- // 推流状态回调
- - (void)onPublisherStateUpdate:(ZegoPublisherState)state errorCode:(int)errorCode extendedData:(NSDictionary *)extendedData streamID:(NSString *)streamID {
- if (state == ZegoPublisherStatePublishing && errorCode == 0) {
- NSLog(@"房间正在推流");
- !self.chatRoomRTCBlock?:self.chatRoomRTCBlock(0, @"");
- }
- if (state == ZegoPublisherStateNoPublish && errorCode == 0) {
- NSLog(@"房间停止推流");
- !self.chatRoomRTCBlock?:self.chatRoomRTCBlock(0, @"");
- }
-
- if (errorCode != 0) {
- NSLog(@"推流失败errorCode:%d %@", errorCode,extendedData);
- !self.chatRoomRTCBlock?:self.chatRoomRTCBlock(errorCode, [NSString stringWithFormat:@"推流失败 %d", errorCode]);
- }
- }
- // 推流质量回调
- - (void)onPublisherQualityUpdate:(ZegoPublishStreamQuality *)quality streamID:(NSString *)streamID {
-
- }
- // 推流端音频采集首帧回调
- - (void)onPublisherCapturedAudioFirstFrame {
-
- }
- // 添加/删除转推 CDN 地址状态回调
- - (void)onPublisherRelayCDNStateUpdate:(NSArray< ZegoStreamRelayCDNInfo * > *)infoList streamID:(NSString *)streamID {
-
- }
- #pragma mark Player Callback
- // 拉流状态变更回调
- - (void)onPlayerStateUpdate:(ZegoPlayerState)state errorCode:(int)errorCode extendedData:(nullable NSDictionary *)extendedData streamID:(NSString *)streamID {
-
- }
- // 拉流质量回调
- - (void)onPlayerQualityUpdate:(ZegoPlayStreamQuality *)quality streamID:(NSString *)streamID {
-
- }
- // 拉流媒体事件回调
- - (void)onPlayerMediaEvent:(ZegoPlayerMediaEvent)event streamID:(NSString *)streamID {
-
- }
- // 拉流端音频接收首帧回调
- - (void)onPlayerRecvAudioFirstFrame:(NSString *)streamID {
-
- }
- // 收到远端流的 SEI 内容
- - (void)onPlayerRecvSEI:(NSData *)data streamID:(NSString *)streamID {
-
- }
- #pragma mark Mixer Callback
- // 混流转推 CDN 状态更新通知
- - (void)onMixerRelayCDNStateUpdate:(NSArray<ZegoStreamRelayCDNInfo *> *)infoList taskID:(NSString *)taskID {
-
- }
- // 混流中的每条单流的的声浪更新通知
- - (void)onMixerSoundLevelUpdate:(NSDictionary<NSNumber *, NSNumber *> *)soundLevels {
-
- }
- // 自动混流中的每条单流的的声浪更新通知
- - (void)onAutoMixerSoundLevelUpdate:(NSDictionary<NSString *, NSNumber *> *)soundLevels {
-
- }
- #pragma mark Device Callback
- // 音频设备状态改变
- - (void)onAudioDeviceStateChanged:(ZegoDeviceInfo *)deviceInfo updateType:(ZegoUpdateType)updateType deviceType:(ZegoAudioDeviceType)deviceType {
-
- }
- // 音频设备音量改变
- - (void)onAudioDeviceVolumeChanged:(int)volume deviceType:(ZegoAudioDeviceType)deviceType deviceID:(NSString *)deviceID {
-
- }
- // 本地采集音频声浪回调
- - (void)onCapturedSoundLevelUpdate:(NSNumber *)soundLevel {
- if (self.streamId.length == 0) {
- return;
- }
- if (!self.enableMicrophone) {
- // enableAudioCaptureDevice方法在禁止采集音频时声浪会保持最后一次的值,这里做下重置0
- soundLevel = [NSNumber numberWithInt:0];
- }
- // NSLog(@"当前用户的声浪:%@", soundLevel);
- dispatch_async(dispatch_get_main_queue(), ^{
- if (self.delegate && [self.delegate respondsToSelector:@selector(reportAudioVolumeIndicationOfSpeakers:)]) {
- [self.delegate reportAudioVolumeIndicationOfSpeakers:@{self.streamId:soundLevel}];
- }
- });
- }
- // 本地采集音频声浪回调 支持版本:2.10.0 及以上。
- - (void)onCapturedSoundLevelInfoUpdate:(ZegoSoundLevelInfo *)soundLevelInfo {
-
- }
- // 远端拉流音频声浪回调 key:streamId value:volume 0~100
- - (void)onRemoteSoundLevelUpdate:(NSDictionary<NSString *, NSNumber *> *)soundLevels {
- // NSLog(@"其他用户的声浪:%@", soundLevels);
- dispatch_async(dispatch_get_main_queue(), ^{
- if (self.delegate && [self.delegate respondsToSelector:@selector(reportAudioVolumeIndicationOfSpeakers:)]) {
- [self.delegate reportAudioVolumeIndicationOfSpeakers:soundLevels];
- }
- });
- }
- // 远端拉流音频声浪回调 支持版本:2.10.0 及以上。
- - (void)onRemoteSoundLevelInfoUpdate:(NSDictionary<NSString *, ZegoSoundLevelInfo *> *)soundLevelInfos {
-
- }
- // 本地采集音频频谱回调
- - (void)onCapturedAudioSpectrumUpdate:(NSArray<NSNumber *> *)audioSpectrum {
-
- }
- // 远端拉流音频频谱回调
- - (void)onRemoteAudioSpectrumUpdate:(NSDictionary<NSString *, NSArray<NSNumber *> *> *)audioSpectrums {
-
- }
- // 设备异常通知
- - (void)onDeviceError:(int)errorCode deviceName:(NSString *)deviceName {
-
- }
- // 远端麦克风设备状态通知
- - (void)onRemoteMicStateUpdate:(ZegoRemoteDeviceState)state streamID:(NSString *)streamID {
-
- }
- // 音频设备路由变更通知
- - (void)onAudioRouteChange:(ZegoAudioRoute)audioRoute {
-
- }
- // 房间内正在推拉流的用户的网络质量回调
- - (void)onNetworkQuality:(NSString *)userID upstreamQuality:(ZegoStreamQualityLevel)upstreamQuality downstreamQuality:(ZegoStreamQualityLevel)downstreamQuality {
-
- }
- #pragma mark — 音乐播放管理
- - (void)initAudioPlayer {
-
- ZegoMediaPlayer *mediaPlayer = [[ZegoExpressEngine sharedEngine] createMediaPlayer];
-
- if (mediaPlayer) {
- [mediaPlayer setEventHandler:self];
- // 禁止背景音乐输出
- [self.mediaPlayer enableAux:self.enableMicrophone];
-
- self.mediaPlayer = mediaPlayer;
- } else {
- NSLog(@"创建播放器失败");
- }
- }
- /// 开始播放音乐文件
- /// @param filePath 指定需要混音的音频文件名和文件路径名,例如: /var/mobile/Containers/Data/audio.mp4。建议填写文件后缀名。若无法确定文件后缀名,可不填。支持以下音频格式: mp3,aac,m4a,3gp,wav
- - (void)startAudioMixing:(NSString * _Nonnull)filePath musicId:(nonnull NSString *)musicId {
-
- if (_mediaPlayer == nil) {
- [self initAudioPlayer];
- }
- if (self.mediaPlayer.currentState == ZegoMediaPlayerStatePlaying) {
- [self.mediaPlayer stop];
- }
- // 设置背景音乐混流状态
- [self.mediaPlayer enableAux:self.enableMicrophone];
-
- [self.mediaPlayer loadResource:filePath callback:^(int errorCode) {
- if (errorCode == 0) {
- self.currentPlayMusicId = [musicId copy];
- [self.mediaPlayer start];
- }else {
- if (self.delegate && [self.delegate respondsToSelector:@selector(playLocalAudioMixingStatusChange:musicId:)]) {
- [self.delegate playLocalAudioMixingStatusChange:MusicPlayStatusFailed musicId:musicId];
- }
- }
- }];
- }
- /// 停止播放音乐文件
- - (void)stopAudioMixing {
- [self.mediaPlayer stop];
- }
- /// 暂停播放音乐文件
- - (void)pauseAudioMixing {
- [self.mediaPlayer pause];
- }
- /// 恢复播放音乐文件
- - (void)resumeAudioMixing {
- [self.mediaPlayer resume];
- }
- /// 调节音乐文件的播放音量 该方法调节混音的音乐文件在本地和远端播放的音量大小。
- /// @param volume 音乐文件播放音量范围为 0~200。默认 100 为原始文件音量
- - (void)adjustAudioMixingVolume:(int)volume {
- // 设置播放器音量
- [self.mediaPlayer setVolume:volume];
- }
- /// 获取音乐文件的本地播放音量
- - (int)getAudioMixingPlayoutVolume {
- // 获取播放器当前播放音量
- return self.mediaPlayer.playVolume;
- }
- /// 获取音乐文件时长 单位为毫秒
- - (int)getAudioMixingDuration {
- return (int)self.mediaPlayer.totalDuration;
- }
- /// 获取音乐文件播放进度,单位为毫秒。
- - (int)getAudioMixingCurrentPosition {
- return (int)self.mediaPlayer.currentProgress;
- }
- /// 设置本地混响效果
- /// @param soundName 混响效果的名字 不传则说明是关闭
- - (void)setLocalVoiceReverbPresetName:(NSString *)soundName {
- if (StringIsEmpty(soundName)) {
- [[ZegoExpressEngine sharedEngine] setReverbPreset:ZegoReverbPresetNone];
- }else {
- if ([soundName isEqualToString:@"小房间"]) {
- [[ZegoExpressEngine sharedEngine] setReverbPreset:ZegoReverbPresetSoftRoom];
- }else if ([soundName isEqualToString:@"大房间"]) {
- [[ZegoExpressEngine sharedEngine] setReverbPreset:ZegoReverbPresetLargeRoom];
- }else if ([soundName isEqualToString:@"音乐厅"]) {
- [[ZegoExpressEngine sharedEngine] setReverbPreset:ZegoReverbPresetConcertHall];
- }else if ([soundName isEqualToString:@"山谷"]) {
- [[ZegoExpressEngine sharedEngine] setReverbPreset:ZegoReverbPresetValley];
- }else if ([soundName isEqualToString:@"录音室"]) {
- [[ZegoExpressEngine sharedEngine] setReverbPreset:ZegoReverbPresetRecordingStudio];
- }else if ([soundName isEqualToString:@"地下室"]) {
- [[ZegoExpressEngine sharedEngine] setReverbPreset:ZegoReverbPresetBasement];
- }else if ([soundName isEqualToString:@"KTV"]) {
- [[ZegoExpressEngine sharedEngine] setReverbPreset:ZegoReverbPresetKTV];
- }else if ([soundName isEqualToString:@"演唱会"]) {
- [[ZegoExpressEngine sharedEngine] setReverbPreset:ZegoReverbPresetVocalConcert];
- }else if ([soundName isEqualToString:@"流行"]) {
- [[ZegoExpressEngine sharedEngine] setReverbPreset:ZegoReverbPresetPopular];
- }else if ([soundName isEqualToString:@"摇滚"]) {
- [[ZegoExpressEngine sharedEngine] setReverbPreset:ZegoReverbPresetRock];
- }
- }
- }
- #pragma mark - ZegoMediaPlayerEventHandler
- /// 播放器播放状态回调
- ///
- /// @param mediaPlayer 回调的播放器实例
- /// @param state 播放器状态
- /// @param errorCode 错误码,详情请参考常用错误码文档
- - (void)mediaPlayer:(ZegoMediaPlayer *)mediaPlayer stateUpdate:(ZegoMediaPlayerState)state errorCode:(int)errorCode {
- MusicPlayStatus playStatus = 0;
- switch (state) {
- case ZegoMediaPlayerStateNoPlay:
- // 播放停止状态
- playStatus = MusicPlayStatusStopped;
- break;
- case ZegoMediaPlayerStatePlaying:
- // 正在播放状态
- playStatus = MusicPlayStatusPlaying;
- break;
- case ZegoMediaPlayerStatePausing:
- // 暂停状态
- playStatus = MusicPlayStatusPaused;
- break;
- case ZegoMediaPlayerStatePlayEnded:
- // 当前曲目播放完成,可执行播放下一首等操作
- playStatus = MusicPlayStatusStopped;
- break;
- }
- if (errorCode != 0) {
- playStatus = MusicPlayStatusFailed;
- }
- if (self.delegate && [self.delegate respondsToSelector:@selector(playLocalAudioMixingStatusChange:musicId:)]) {
- [self.delegate playLocalAudioMixingStatusChange:playStatus musicId:self.currentPlayMusicId];
- }
- NSLog(@"播放状态变更:%lu \n播放报错:%ld", (unsigned long)state, (long)errorCode);
- }
- /// 播放器网络状态事件回调
- ///
- /// @param mediaPlayer 回调的播放器实例
- /// @param networkEvent 网络状态事件
- - (void)mediaPlayer:(ZegoMediaPlayer *)mediaPlayer networkEvent:(ZegoMediaPlayerNetworkEvent)networkEvent {
- if (networkEvent == ZegoMediaPlayerNetworkEventBufferBegin) {
- // 展示 Loading UI
- } else if (networkEvent == ZegoMediaPlayerNetworkEventBufferEnded) {
- // 关闭 Loading UI
- }
- }
- /// 播放器播放进度回调
- ///
- /// @param mediaPlayer 回调的播放器实例
- /// @param millisecond 进度,单位为毫秒
- - (void)mediaPlayer:(ZegoMediaPlayer *)mediaPlayer playingProgress:(unsigned long long)millisecond {
- // 更新进度条
- }
- @end
|