Skip to content
This repository has been archived by the owner on Jan 31, 2023. It is now read-only.

2.5.相手へ発信

hkato edited this page Mar 27, 2016 · 4 revisions

相手へビデオ通話をかける

サーバに接続しているピアの一覧を取得する

Java

// 接続相手を選択する
private void getPeerList(){
  if ((null == _peer) || (null == _id) || (0 == _id.length())){
    return;
  }

  _listPeerIds.clear();

  _peer.listAllPeers(new OnCallback() {
    @Override
    public void onCallback(Object object) {
      JSONArray peers = (JSONArray) object;
      for (int i = 0; peers.length() > i; i++) {
        String strValue = "";
        try {
          strValue = peers.getString(i);
        } catch (Exception e) {
          e.printStackTrace();
        }

        if (0 != _id.compareToIgnoreCase(strValue)) {
          _listPeerIds.add(strValue);
        }
      }

      if ((null != _listPeerIds) && (0 < _listPeerIds.size())) {
        showPeerListDialog();
      }
    }
  });
}

Objective-C

//接続相手を選択する
- (void)getPeerList
{
    if ((nil == _peer) || (nil == _id) || (0 == _id.length)){
        return;
    }
    
    [_peer listAllPeers:^(NSArray* peers)
     {
         _listPeerIds = [[NSMutableArray alloc] init];

             for (NSString* strValue in peers)
             {
                 if (NSOrderedSame == [_id caseInsensitiveCompare:strValue])
                 {
                     continue;
                 }
                 
                 [_listPeerIds addObject:strValue];
             }
         if((nil != _listPeerIds) && (0< [_listPeerIds count]))
         {
             [self showPeerListDialog];
         }
         
     }];
}

Swift

func getPeerList(){
        if (_peer == nil) || (_id == nil) || (_id?.characters.count == 0) {
            return
        }
        _peer?.listAllPeers({ (peers:[AnyObject]!) -> Void in
            self._listPeerIds = []
            let peersArray:[String] = peers as! [String]
            for strValue:String in peersArray{
                
                if strValue == self._id{
                    continue
                }   
                self._listPeerIds.append(strValue)
            }
            if self._listPeerIds.count > 0{
                self.showPeerDialog()
            }

        })
    }

通話したい相手を選んで、ビデオ通話発信する

Java

// ビデオ通話をかける
private void call(String strPeerId){
  CallOption option = new CallOption();
  _media = _peer.call(strPeerId, _msLocal, option);

  if (null != _media){
    setMediaCallback(_media);
    _bEstablished = true;
  }

  updateUI();
}

// ビデオ通話を終了する
private void close(){
  if (_bEstablished) {
    _bEstablished = false;
    if (null != _media) {
      _media.close();
    }
  }
}

Objective-C

- (void)call:(NSString *)strDestId
{
    SKWCallOption *option = [[SKWCallOption alloc]init];
    _mediaConnection = [_peer callWithId:strDestId stream:_msLocal options:option];
    
    if(_mediaConnection != nil){
        [self setMediaCallbacks:_mediaConnection];
        _bEstablished = YES;
    }
    
    [self updateUI];
    
}


//ビデオ通話を終了する
- (void)closeChat
{
    if (nil != _mediaConnection)
    {
        if (nil != _msRemote)
        {
            SKWVideo* video = (SKWVideo *)[self.view viewWithTag:TAG_REMOTE_VIDEO];
            if (nil != video)
            {
                [video removeSrc:_msRemote track:0];
            }
            
            [_msRemote close];
            
            _msRemote = nil;
        }
        
        [_mediaConnection close];
    }
}

Swift

//ビデオ通話を開始する
func call(strDestId: String) {
    let option = SKWCallOption()
    _mediaConnection = _peer!.callWithId(strDestId, stream: _msLocal, options: option)
    if _mediaConnection != nil {
        self.setMediaCallbacks(self._mediaConnection!)
        _bEstablished = true
    }
    self.updateUI()
}

//ビデオ通話を終了する
func closeChat(){
    if _mediaConnection != nil{
        if _msRemote != nil{
            let remoteVideoView:SKWVideo = self.view.viewWithTag(ViewTag.TAG_REMOTE_VIDEO.hashValue) as! SKWVideo

            remoteVideoView .removeSrc(_msRemote, track: 0)
            _msRemote?.close()
            _msRemote = nil
        }
        _mediaConnection?.close()
    }
}