diff --git a/example/libs/sellycloudsdk-1.0.0.aar b/example/libs/sellycloudsdk-1.0.0.aar index 08cbf6f..0a10475 100644 Binary files a/example/libs/sellycloudsdk-1.0.0.aar and b/example/libs/sellycloudsdk-1.0.0.aar differ diff --git a/example/src/main/java/com/demo/SellyCloudSDK/FeatureHubActivity.kt b/example/src/main/java/com/demo/SellyCloudSDK/FeatureHubActivity.kt index 7f75b67..9b33584 100644 --- a/example/src/main/java/com/demo/SellyCloudSDK/FeatureHubActivity.kt +++ b/example/src/main/java/com/demo/SellyCloudSDK/FeatureHubActivity.kt @@ -201,6 +201,7 @@ class FeatureHubActivity : AppCompatActivity() { } private fun handleAliveItemClick(item: AliveStreamItem) { + LivePlayActivity.closePipIfAny() val url = item.url?.trim().orEmpty() val intent = if (url.isNotEmpty()) { LivePlayActivity.createIntent( diff --git a/example/src/main/java/com/demo/SellyCloudSDK/live/LivePlayActivity.kt b/example/src/main/java/com/demo/SellyCloudSDK/live/LivePlayActivity.kt index 4bb0b83..4add4ae 100644 --- a/example/src/main/java/com/demo/SellyCloudSDK/live/LivePlayActivity.kt +++ b/example/src/main/java/com/demo/SellyCloudSDK/live/LivePlayActivity.kt @@ -55,6 +55,7 @@ import kotlinx.coroutines.launch import java.text.SimpleDateFormat import java.util.Date import java.util.Locale +import java.lang.ref.WeakReference class LivePlayActivity : AppCompatActivity() { @@ -80,6 +81,7 @@ class LivePlayActivity : AppCompatActivity() { private var firstAudioFrameCostMs: Long? = null private var isLatencyChasingActive: Boolean = false private var lastLatencyChasingSpeed: Float? = null + private var hasReleasedPlayer: Boolean = false private val logLines: ArrayDeque = ArrayDeque() private val logTimeFormat = SimpleDateFormat("HH:mm:ss.SSS", Locale.getDefault()) @@ -209,9 +211,12 @@ class LivePlayActivity : AppCompatActivity() { logEvent("释放播放器") logDialog?.dismiss() if (this::playerClient.isInitialized) { - playerClient.release() + releasePlayerIfNeeded() } uiScope.cancel() + if (pipActivityRef?.get() === this) { + pipActivityRef = null + } } override fun onStop() { @@ -228,6 +233,11 @@ class LivePlayActivity : AppCompatActivity() { override fun onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean, newConfig: Configuration) { super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig) updatePipUi(isInPictureInPictureMode) + if (isInPictureInPictureMode) { + pipActivityRef = WeakReference(this) + } else if (pipActivityRef?.get() === this) { + pipActivityRef = null + } } private fun togglePlay() { @@ -258,6 +268,20 @@ class LivePlayActivity : AppCompatActivity() { pipController.enterPictureInPictureMode(renderView) } + private fun releasePlayerIfNeeded() { + if (hasReleasedPlayer) return + hasReleasedPlayer = true + playerClient.release() + } + + private fun finishForNewPlayback() { + logEvent("外部请求: 结束画中画播放") + if (this::playerClient.isInitialized) { + releasePlayerIfNeeded() + } + finish() + } + private fun startPlayback() { val env = envStore.read() args.playParams?.let { params -> @@ -706,6 +730,8 @@ class LivePlayActivity : AppCompatActivity() { companion object { private const val TAG = "LivePlayActivity" private const val MAX_LOG_LINES = 200 + @Volatile + private var pipActivityRef: WeakReference? = null const val EXTRA_PLAY_PROTOCOL = "play_protocol" const val EXTRA_STREAM_ID_OR_URL = "stream_id_or_url" const val EXTRA_PLAY_VHOST = "play_vhost" @@ -741,6 +767,24 @@ class LivePlayActivity : AppCompatActivity() { .putExtra(EXTRA_PLAY_STREAM_NAME, streamName) .putExtra(EXTRA_AUTO_START, autoStart) } + + fun closePipIfAny(): Boolean { + val activity = pipActivityRef?.get() ?: return false + if (activity.isDestroyed || activity.isFinishing) { + pipActivityRef = null + return false + } + if (!activity.isInPictureInPictureMode) { + pipActivityRef = null + return false + } + if (Looper.myLooper() == Looper.getMainLooper()) { + activity.finishForNewPlayback() + } else { + activity.runOnUiThread { activity.finishForNewPlayback() } + } + return true + } } private fun resolveChannelId(input: String): String {