如何为 Exoplayer 360 视频启用 GyroSensors

How to enable GyroSensors for Exoplayer 360 videos

我正在开发一个基于 Exoplayer 的自定义播放器来播放 VR 视频。我能够使用 SimpleExoPlayer 创建一个简化版本,但是,即使我移动了我的设备,VR 播放器也没有按应有的方式旋转。

然后我去试用了 Exoplayer 演示,它完全有效。但是,我无法修改代码以满足我的需要,所以我不得不从头开始。正如我提到的,我能够创建一个非常简化的版本。但是,陀螺仪传感器不会旋转视频。我想知道,我错过了什么。

这是我的VRPlayerActivity

class VRPlayerActivity : AppCompatActivity() {

    private val mp4VideoUri: Uri? = Uri.parse("https://theyouthbuzz.com/ytplayer/Seoul8KVR3DTrailer.mp4")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_vrplayer)
        val player = SimpleExoPlayer.Builder(this).build()
        val playerView = findViewById<SimpleExoPlayerView>(R.id.player_view)

        // Produces DataSource instances through which media data is loaded.
        // Produces DataSource instances through which media data is loaded.
        val dataSourceFactory: DataSource.Factory = DefaultDataSourceFactory(
            this,
            Util.getUserAgent(this, "YouthBuzz")
        )
        // This is the MediaSource representing the media to be played.
        // This is the MediaSource representing the media to be played.
        val videoSource: MediaSource = ProgressiveMediaSource.Factory(dataSourceFactory)
            .createMediaSource(mp4VideoUri)
        // Prepare the player with the source.
        // Prepare the player with the source.
        player.prepare(videoSource)
        (playerView.videoSurfaceView as SphericalGLSurfaceView?)!!.setDefaultStereoMode(
            C.STEREO_MODE_TOP_BOTTOM
        )

        playerView.player = player

    }
}

这是我的activity_vrplayer.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#0099cc"
    tools:context="._views.VRPlayer.VRPlayerActivity">

    <!-- The primary full-screen view. This can be replaced with whatever view
         is needed to present your content, e.g. VideoView, SurfaceView,
         TextureView, etc. -->
    <com.google.android.exoplayer2.ui.SimpleExoPlayerView
        android:id="@+id/player_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:surface_type="spherical_gl_surface_view"/>

</FrameLayout>

这个问题是因为您忘记使用正确的生命周期挂钩方法调用 playerView.onResume(),根据其 javadoc:

public void onResume()

Should be called when the player is visible to the user and if surface_type is spherical_gl_surface_view. It is the counterpart to onPause(). This method should typically be called in Activity.onStart(), or Activity.onResume() for API versions <= 23.

事实上,如果我们深入研究 PlayerView 源代码,我们可以看到这调用了 SphericalGLSurfaceViewonResume() 方法,它方便地调用 updateOrientationListenerRegistration() 来注册 android.hardware.SensorManager

同样,尽管您将实施简化版本,但您应该调用对应的方法和 player.release()