IOS藍(lán)牙語(yǔ)音通信

 最近看了不少關(guān)于ios藍(lán)牙語(yǔ)音通信的文章,網(wǎng)上錯(cuò)誤不少。最近自己寫了個(gè)小Demo。

10年積累的成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有南山免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

具體的每個(gè)函數(shù)干什么的請(qǐng)自行查詢。都是Gamekit框架的api。

代碼實(shí)現(xiàn)如下

- (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(@"設(shè)置播放記錄錯(cuò)誤:%@",[errorlocalizedDescription]);

        return;

    }

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

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

        return;

    }

    

    [GKVoiceChatServicedefaultVoiceChatService].client=self;

   if (![[GKVoiceChatServicedefaultVoiceChatService]startVoiceChatWithParticipantID:peerIDerror:&error]) {

        NSLog(@"開啟語(yǔ)音失敗 :%@",[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];

}

 

加紅的方法大家注意下。第一個(gè)加紅的方法如果不寫可能也能實(shí)現(xiàn),但會(huì)有一些內(nèi)存上的錯(cuò)誤。關(guān)閉藍(lán)牙連接不是簡(jiǎn)單的   [_session disconnectFromAllPeers];就行的  要像第二個(gè)紅色方法里那寫全才能真正正確關(guān)閉。(水平有限歡迎拍磚)。源碼至http://down.51cto.com/data/703329下載?;蚵?lián)系478043385@qq.com免費(fèi)索取。

網(wǎng)站欄目:IOS藍(lán)牙語(yǔ)音通信
URL鏈接:http://muchs.cn/article28/gdeccp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站用戶體驗(yàn)、商城網(wǎng)站軟件開發(fā)、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)