如何为Exoplayer 360视频启用陀螺仪传感器

问题描述 投票:0回答:1

我正在开发基于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>
android kotlin video exoplayer
1个回答
0
投票

此问题是因为您忘记了根据其javadoc使用适当的生命周期挂钩方法来调用playerView.onResume()

public void onResume()

当播放器对用户可见时,如果 surface_type是球形_gl_surface_view。它是对应的 onPause()。通常应在以下位置调用此方法 对于API版本<= 23,为Activity.onStart()或Activity.onResume()。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.