【问题标题】:TextureView setSurfaceTexture method requires API 16 levelTextureView setSurfaceTexture 方法需要 API 16 级别
【发布时间】:2016-03-01 14:02:08
【问题描述】:

TextureView.setSurfaceTexture 方法需要 API 级别 16,但我目前的最低 14,如何在 14、15 API 中使用此方法?

更新(添加代码)

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".view.MainActivity"
    tools:showIn="@layout/activity_main">

    <FrameLayout
        android:id="@+id/videoSurfaceContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextureView
            android:id="@+id/texture_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </FrameLayout>
</RelativeLayout>

在 Fragment 的 onCreateView 方法中:

View rootView = inflater.inflate(R.layout.content_main, container, false);
    mVideoContainer = rootView.findViewById(R.id.videoSurfaceContainer);
            mTextureView = (TextureView) rootView.findViewById(R.id.texture_view);
            if (mSurfaceTexture != null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    mTextureView.setSurfaceTexture(mSurfaceTexture);
                }
            }
            mVideoControllerView.setAnchorView((FrameLayout) mVideoContainer);
            mTextureView.setSurfaceTextureListener(this);

表面纹理监听器:

@Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
        mSurfaceTexture = surfaceTexture;
        Surface surface = new Surface(mSurfaceTexture);
        mMediaPlayer.setSurface(surface);
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) {
        mSurfaceTexture = surfaceTexture;
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
        mSurfaceTexture = surfaceTexture;
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
        mSurfaceTexture = surfaceTexture;
    }

【问题讨论】:

  • 你能发布一些你的代码吗?你不能通过使用 api 级别 14 兼容的方法来克服这个限制,例如在你的活动上设置SurfaceTextureListener
  • 酷,请问您为什么需要致电mTextureView.setSurfaceTexture(mSurfaceTexture);?因为据我所知,所有工作都是在onSurfaceTextureAvailable 内部完成的,您可以在其中使用可用的SurfaceTexture 创建一个新的Surface 并将其设置在MediaPlayer 上?
  • 如果没有 setSurfaceTexture 方法,由于方向变化,我有白屏,但声音继续播放。一般来说,我希望视频在方向改变时不会停止播放。我的片段使用 setRetainInstance(true);

标签: android textureview android-textureview android-min-sdk


【解决方案1】:

TextureView.setSurfaceTexture 方法需要 API 级别 16

TextureView.setSurfaceTexture 需要 API 级别 16 或更高

没有#is现在没有办法。您必须调用 API 级别 16

打开您的build.gradle 部分

 defaultConfig {
    minSdkVersion 16    // must use 16 for this 
    targetSdkVersion 19 // set yours as per requirement 

}

【讨论】:

    猜你喜欢
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多