网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

IOS蓝牙语音通信

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

10年积累的成都做网站、网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有南山免费网站建设让你可以放心的选择与我们合作。

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

代码实现如下

- (void)viewDidLoad

{

    [superviewDidLoad];

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

    

   UIButton *muteBtn=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [muteBtnsetTitle:@"聊天中"forState:UIControlStateHighlighted];

    [muteBtnsetTitle:@"聊天"forState:UIControlStateNormal];

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

    [muteBtnaddTarget:selfaction:@selector(muteAction:)forControlEvents:UIControlEventTouchDown];

    [muteBtnaddTarget:selfaction:@selector(UnmuteAction:)forControlEvents:UIControlEventTouchUpInside];

    

   UIButton *connectBtn=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    [connectBtnsetTitle:@"连接"forState:UIControlStateNormal];

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

    [connectBtnaddTarget:selfaction:@selector(connectAction:)forControlEvents:UIControlEventTouchUpInside];

    

    

   UIButton *disConnectBtn=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

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

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

    [disConnectBtnaddTarget:selfaction:@selector(disConnectAction:)forControlEvents:UIControlEventTouchUpInside];

     [self.view addSubview:muteBtn];

    [self.view addSubview:connectBtn];

    [self.view addSubview:disConnectBtn];

}

 

-(void)connectAction:(id)sender{

    if (!picker) {

       picker=[[GKPeerPickerControlleralloc]init];

       picker.connectionTypesMask=GKPeerPickerConnectionTypeNearby;

        picker.delegate=self;

    }

    if (!isConnect) {

        [picker show];

    }

}

 

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

    if (!_session) {

       _session=[[GKSessionalloc]initWithSessionID:(_sessionID ?_sessionID :@"Sample Session")displayName:nilsessionMode:GKSessionModePeer];

        _session.delegate=self;

    }

return_session;

}

 

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

    

    [picker dismiss];

   isConnect=YES;

    [_sessionsetDataReceiveHandler:selfwithContext:nil];

    NSError *error;

    

   AVAudioSession *audioSession=[AVAudioSessionsharedInstance];

    

   if (![audioSessionsetCategory:AVAudioSessionCategoryPlayAndRecorderror:&error]) {

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

        return;

    }

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

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

        return;

    }

    

    [GKVoiceChatServicedefaultVoiceChatService].client=self;

   if (![[GKVoiceChatServicedefaultVoiceChatService]startVoiceChatWithParticipantID:peerIDerror:&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) {

        

        [[GKVoiceChatServicedefaultVoiceChatService]stopVoiceChatWithParticipantID:peerID];

       //[self disConnectAction:nil];

    }

 

}

-(void)disConnectAction:(id)sender{

    [_sessiondisconnectFromAllPeers];

    _session.available=NO;

   _session.delegate=nil;

    [_sessionsetDataReceiveHandler:nilwithContext:nil];

   _session=nil;

    isConnect=NO;

}

-(void)muteAction:(id)sender{

    [GKVoiceChatServicedefaultVoiceChatService].microphoneMuted=YES;

}

-(void)UnmuteAction:(id)sender{

    [GKVoiceChatServicedefaultVoiceChatService].microphoneMuted=NO;

}

-(NSString *)participantID{

    

   return_session.peerID;

}

 

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

   

    [_sessionsendData:datatoPeers:[NSArrayarrayWithObject:participantID]withDataMode:GKSendDataReliableerror:nil];

}

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

{

[[GKVoiceChatServicedefaultVoiceChatService]receivedData:datafromParticipantID:peer];

}

 

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


名称栏目:IOS蓝牙语音通信
文章链接:http://bjjierui.cn/article/gdeccp.html

其他资讯