1対1音声通話
1対1音声通話を実装するサンプルコードです。
前提条件
開始する前に、次の作業が必要です。
- PlanetKitを初期化してください。
- 適切なアクセストークンを取得してください。
- 1対1通話フローにて、APIを使用するための全般的なプロセスを確認してください。
1対1音声通話実装時の考慮事項
受信者側で通話の通知を受けるには、通知用のシステムを実装するか、FCM(Firebase Cloud Messaging)といった外部のプッシュ通知システムを連携する必要があります。
また、受信者にどのような情報を伝えるべきかを知っておく必要があります。アプリサーバーの役割でアプリケーションが転送すべきデータであるcc_param
について確認できます。
makeCall()
またはverifyCall()
を呼び出した後は、返却されたPlanetKitCallResult
のreason
プロパティを確認する必要があります。
reason
がPlanetKitStartFailReason.NONE
の場合、成功を意味します。- それ例外は失敗を意味し、
reason
に応じて適切な処理を行う必要があります。
動作する全体のソースコードは、デモアプリのソースコードで確認できます。
権限申請
通話をする前に、次のランタイム権限を取得してください。
Manifest.permission.READ_PHONE_STATE
Manifest.permission.RECORD_AUDIO
Manifest.permission.BLUETOOTH_CONNECT
(targetSdkVersion
31以上)
発信側 - 通話の作成
適切な引数を使用してPlanetKit.makeCall()
関数を呼び出します。引数を作るには、PlanetKitMakeCallParam.Builder()
を使用する必要があります。
fun makeCallExample(myUserId: String, myServiceId: String, targetId: String, targetServiceId: String, accessToken: String)
{
val makeCallParam = PlanetKitMakeCallParam.Builder()
.myId(myUserId)
.myServiceId(myServiceId)
.peerId(targetId)
.peerServiceId(targetServiceId)
.accessToken(accessToken)
.build()
var result = PlanetKit.makeCall(makeCallParam, makeCallListener = object : MakeCallListener
{
override fun onConnected(call: PlanetKitCall, param: PlanetKitCallConnectedParam)
{
// This is called after the call is connected.
// Write your own code here.
}
override fun onWaitConnected(call: PlanetKitCall)
{
// This is called after making a call.
// Write your own code here.
}
override fun onDisconnected(call: PlanetKitCall, param: PlanetKitDisconnectedParam)
{
// This is called after the call is disconnected.
// Write your own code here.
}
})
if (result.reason == PlanetKitStartFailReason.NONE && result.call != null)
{
// The "result.call" instance is the main instance to call APIs from now on.
// You must keep it to control this call.
// See call.instanceId and PlanetKit.getCall(instanceId).
}
else
{
// result.reason: PlanetKitStartFailReason describes why makeCall failed.
}
}
受信側 - 通話の受信
Androidアプリは通常、プッシュ通知用にFirebase Cloud Messaging(FCM)を使用します。AndroidはFCMからメッセージを受信する用途で、FirebaseMessagingService
という基本クラスを提供します。
プッシュメッセージを受信したら、メッセージからcc_param
をパーシングし、cCParam
を生成します。このデータは、通話に応答する際に渡すべき必須引数です。
通話に応答するには、PlanetKitVerifyCallParam
を作成してからPlanetKit.verifyCall()
を呼び出してください。
class YourFirebaseMessagingService : FirebaseMessagingService()
{
override fun onMessageReceived(message: RemoteMessage)
{
val cCParamStr = message.data["cc_param"]
if (cCParamStr.isNullOrBlank())
{
Log.e("Push", "Empty message from application server")
return
}
val myUserId = "your user id"
val myServiceId = "your service id"
val cCParam = PlanetKitCCParam.create(cCParamStr)
if (cCParam == null) {
Log.e(TAG, "Can not create PlanetKitCCParam. Check cCParamStr=$cCParamStr")
return
}
Log.d("Push","ccParam{peerId=${cCParam.peerId}, peerServiceId=${cCParam.peerServiceId}, mediaType=${cCParam.mediaType}")
val verifyCallParam = PlanetKitVerifyCallParam.Builder()
.myId(myUserId)
.myServiceId(myServiceId)
.cCParam(cCParam)
.build()
val result = PlanetKit.verifyCall(verifyCallParam, verifyListener = object : VerifyListener
{
override fun onVerified(call: PlanetKitCall, peerStartMessage: PlanetKitCallStartMessage?, peerUseResponderPreparation: Boolean)
{
// This is called after verifying a call.
// Write your own code here.
// For example, you can show a notification and switch to your ringing screen.
//
// The "call" is the verified call instance.
// If you send an instance of call through 'Intent of Android',
// then you can use the "call.instanceId".
// You can get the instance of call with the API "PlanetKit.getCall(instanceId)".
}
override fun onDisconnected(call: PlanetKitCall, param: PlanetKitDisconnectedParam)
{
// This is called after the call is disconnected.
// Write your own code here.
}
})
// Store result.call or result.call.instanceId for acceptCall().
if (result.reason == PlanetKitStartFailReason.NONE && result.call != null)
{
// The "result.call" instance is the main instance to call APIs from now on.
// You must keep it to control this call.
// See call.instanceId and PlanetKit.getCall(instanceId).
}
else
{
// result.reason: PlanetKitStartFailReason describes why verifyCall failed.
}
}
}
受信側 - 通話の応答
一般的に通話を受信した後、通話を受けるかどうかを決める時間が必要です。通話に応答するには、acceptCall()
を呼び出します。
call
変数は検証されたのインスタンスです。
func acceptCallExample(instanceId: Int)
{
var call = PlanetKit.getCall(instanceId)
call.acceptCall(
listener = object : AcceptCallListener {
override fun onConnected(call: PlanetKitCall, param: PlanetKitCallConnectedParam) {
// This is called after the call is connected.
// Write your own code here.
...
}
override fun onDisconnected(call: PlanetKitCall, param: PlanetKitDisconnectedParam) {
// This is called after the call is disconnected.
// Write your own code here.
...
}
}
)
}