You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin Android中CrossMediaManager全屏切换功能实现求助

实现CrossMediaManager Android端全屏播放功能

1. 确认基础依赖配置

  • 先确保使用最新版的CrossMediaManager及其依赖的ExoPlayer,避免旧版本功能缺失。在Module级别的build.gradle中检查并更新依赖:
    implementation 'com.google.android.exoplayer:exoplayer:2.X.X' // 版本需与CrossMediaManager匹配
    implementation 'com.martinbonnin.crossmediamanager:crossmediamanager:X.X.X'
    

2. 自定义播放布局,添加全屏按钮

CrossMediaManager默认Android控件无全屏按钮,需自定义布局:

  • 在res/layout下创建custom_player_layout.xml,整合ExoPlayer控件和全屏按钮:
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <com.google.android.exoplayer2.ui.PlayerView
            android:id="@+id/player_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <ImageButton
            android:id="@+id/fullscreen_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_margin="16dp"
            android:src="@drawable/ic_fullscreen" />
    </RelativeLayout>
    
  • 在Activity/Fragment中初始化控件并绑定CrossMediaManager:
    val playerView = findViewById<PlayerView>(R.id.player_view)
    val fullscreenBtn = findViewById<ImageButton>(R.id.fullscreen_btn)
    CrossMediaManager.instance.setPlayerView(playerView)
    
    // 绑定全屏按钮点击事件
    fullscreenBtn.setOnClickListener {
        toggleFullscreen()
    }
    

3. 实现全屏切换逻辑

  • 编写toggleFullscreen()方法,处理屏幕方向、状态栏和ActionBar的切换:
    private fun toggleFullscreen() {
        val isLandscape = resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
        if (isLandscape) {
            // 退出全屏:切回竖屏,显示状态栏和ActionBar
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
            supportActionBar?.show()
            window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
            showSystemUI()
        } else {
            // 进入全屏:切横屏,隐藏状态栏和ActionBar
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
            supportActionBar?.hide()
            window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
            hideSystemUI()
        }
    }
    
  • 在AndroidManifest.xml中给目标Activity添加旋转权限配置:
    <activity
        android:name=".YourVideoActivity"
        android:configChanges="orientation|screenSize|smallestScreenSize"
        android:screenOrientation="portrait">
    </activity>
    

4. 处理生命周期与状态恢复

  • 屏幕旋转时保存播放进度,避免重新加载:
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        val currentPos = CrossMediaManager.instance.player?.currentPosition ?: 0
        outState.putLong("PLAY_PROGRESS", currentPos)
    }
    
    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        val savedPos = savedInstanceState.getLong("PLAY_PROGRESS", 0)
        CrossMediaManager.instance.player?.seekTo(savedPos)
    }
    
  • 在页面销毁时释放播放资源:
    override fun onDestroy() {
        super.onDestroy()
        CrossMediaManager.instance.release()
    }
    

5. 优化全屏体验:隐藏导航栏

  • 添加系统UI控制方法,全屏时隐藏导航栏:
    private fun hideSystemUI() {
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN)
    }
    
    private fun showSystemUI() {
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
    }
    

内容的提问来源于stack exchange,提问作者Echo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 07:31:06