温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

IOS 蓝牙语音通信

发布时间:2020-08-04 09:52:17 来源:网络 阅读:1172 作者:zfajqp 栏目:移动开发

 最近看了不少关于ios蓝牙语音通信的文章,网上错误不少。最近自己写了个小Demo。

具体的每个函数干什么的请自行查询。都是Gamekit框架的api。

代码实现如下

- (void)viewDidLoad

{

    [super viewDidLoad];

    CGSize size=[[UIScreen mainScreen] bounds].size;

    

    UIButton *muteBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    [muteBtn setTitle:@"聊天中" forState:UIControlStateHighlighted];

    [muteBtn setTitle:@"聊天" forState:UIControlStateNormal];

    [muteBtn setFrame:CGRectMake(size.width/2.0-140.0, 20, 280, 280)];

    [muteBtn addTarget:self action:@selector(muteAction:) forControlEvents:UIControlEventTouchDown];

    [muteBtn addTarget:self action:@selector(UnmuteAction:) forControlEvents:UIControlEventTouchUpInside];

    

    UIButton *connectBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    [connectBtn setTitle:@"连接" forState:UIControlStateNormal];

    [connectBtn setFrame:CGRectMake(size.width/2.0-50.0, 320, 100, 40)];

    [connectBtn addTarget:self action:@selector(connectAction:) forControlEvents:UIControlEventTouchUpInside];

    

    

    UIButton *disConnectBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    [disConnectBtn setTitle:@"断开连接" forState:UIControlStateNormal];

    [disConnectBtn setFrame:CGRectMake(size.width/2.0-50.0, 380, 100, 40)];

    [disConnectBtn addTarget:self action:@selector(disConnectAction:) forControlEvents:UIControlEventTouchUpInside];

     [self.view addSubview:muteBtn];

    [self.view addSubview:connectBtn];

    [self.view addSubview:disConnectBtn];

}

 

-(void)connectAction:(id)sender{

    if (!picker) {

        picker=[[GKPeerPickerController alloc] init];

        picker.connectionTypesMask=GKPeerPickerConnectionTypeNearby;

        picker.delegate=self;

    }

    if (!isConnect) {

        [picker show];

    }

}

 

-(GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type{

    if (!_session) {

        _session=[[GKSession alloc] initWithSessionID:(_sessionID ? _sessionID : @"Sample Session") displayName:nil sessionMode:GKSessionModePeer];

        _session.delegate=self;

    }

return _session;

}

 

-(void)peerPickerController:(GKPeerPickerController *)picker1 didConnectPeer:(NSString *)peerID toSession:(GKSession *)session{

    

    [picker dismiss];

    isConnect=YES;

    [_session setDataReceiveHandler:self withContext:nil];

    NSError *error;

    

    AVAudioSession *audioSession=[AVAudioSession sharedInstance];

    

    if (![audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error]) {

        NSLog(@"设置播放记录错误:%@",[error localizedDescription]);

        return;

    }

    if (![audioSession setActive:YES error:&error]) {

        NSLog(@"激活失败:%@",[error localizedDescription]);

        return;

    }

    

    [GKVoiceChatService defaultVoiceChatService].client=self;

    if (![[GKVoiceChatService defaultVoiceChatService] startVoiceChatWithParticipantID:peerID error:&error]) {

        NSLog(@"开启语音失败 :%@",[error userInfo]);

    }

}

-(void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker1{

    picker.delegate=nil;

     picker=nil;

}

-(void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state{

    

    if (state==GKPeerStateConnected) {

        isConnect=YES;

    

    }

    

    if (state==GKPeerStateDisconnected) {

        

        [[GKVoiceChatService defaultVoiceChatService] stopVoiceChatWithParticipantID:peerID];

        //[self disConnectAction:nil];

    }

 

}

-(void)disConnectAction:(id)sender{

    [_session disconnectFromAllPeers];

    _session.available=NO;

    _session.delegate=nil;

    [_session setDataReceiveHandler:nil withContext:nil];

    _session=nil;

    isConnect=NO;

}

-(void)muteAction:(id)sender{

    [GKVoiceChatService defaultVoiceChatService].microphoneMuted=YES;

}

-(void)UnmuteAction:(id)sender{

    [GKVoiceChatService defaultVoiceChatService].microphoneMuted=NO;

}

-(NSString *)participantID{

    

    return _session.peerID;

}

 

-(void)voiceChatService:(GKVoiceChatService *)voiceChatService sendData:(NSData *)data toParticipantID:(NSString *)participantID{

   

    [_session sendData:data toPeers:[NSArray arrayWithObject:participantID] withDataMode:GKSendDataReliable error:nil];

}

- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context

{

[[GKVoiceChatService defaultVoiceChatService] receivedData:data fromParticipantID:peer];

}

 

加红的方法大家注意下。第一个加红的方法如果不写可能也能实现,但会有一些内存上的错误。关闭蓝牙连接不是简单的   [_session disconnectFromAllPeers];就行的  要像第二个红色方法里那写全才能真正正确关闭。(水平有限欢迎拍砖)。源码至http://down.51cto.com/data/703329下载。或联系478043385@qq.com免费索取。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI