网站建设资讯

NEWS

网站建设资讯

IOS蓝牙语音通信-创新互联

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

创新互联建站成立于2013年,是专业互联网技术服务公司,拥有项目网站设计制作、成都做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元马边彝族做网站,已为上家服务,为马边彝族各地企业和个人服务,联系电话:13518219792

具体的每个函数干什么的请自行查询。都是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免费索取。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻标题:IOS蓝牙语音通信-创新互联
URL分享:http://cdweb.net/article/cdcsgi.html