我刚开始写作。是否有一种处理jetpack撰写中的picture-in-picture模式的方法?我找不到任何与此相关的官方文件。
发布于 2022-09-19 10:31:00
class MainActivity : ComponentActivity() {
class MyReciever:BroadcastReceiver(){
override fun onReceive(context: Context?, intent: Intent?) {
println("clicked on PIP action")
}
}
private val isPipSupported by lazy {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
packageManager.hasSystemFeature(
PackageManager.FEATURE_PICTURE_IN_PICTURE
)
} else {
false
}
}
private var videoViewBounds = Rect()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
PictureInPictureTheme {
/***
* video view does't exit for compose.
* So we use android view
* and in factory we create video view and use apply to assign video.
*/
AndroidView(
factory ={
VideoView(it,null).apply {
setVideoURI(Uri.parse("android.resource://$packageName/${R.raw.lakshay}"))
start()
}
} ,
modifier = Modifier
.fillMaxWidth()
.onGloballyPositioned {
videoViewBounds = it
.boundsInWindow()
.toAndroidRect()
}
)
}
}
}
private fun updatedPipParams(): PictureInPictureParams?{
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
PictureInPictureParams.Builder()
.setSourceRectHint(videoViewBounds)
.setAspectRatio(Rational(16,9))
.setActions(
listOf(
RemoteAction(
android.graphics.drawable.Icon.createWithResource(applicationContext,
R.drawable.ic_baseline_baby_changing_station_24),
"Baby Changing Station",
"Baby Changing Station",
PendingIntent.getBroadcast(
applicationContext,
0,
Intent(applicationContext,MyReciever::class.java),
PendingIntent.FLAG_IMMUTABLE
)
)
)
)
.build()
} else {
TODO("VERSION.SDK_INT < O")
}
}
override fun onUserLeaveHint() {
super.onUserLeaveHint()
if(!isPipSupported){
return
}
updatedPipParams()?.let {params->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
enterPictureInPictureMode(params)
}
}
}}
https://stackoverflow.com/questions/73274447
复制相似问题