网站建设资讯

NEWS

网站建设资讯

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://cdweb.net/article/gdeccp.html