1
TextureView.setSurfaceTexture
方法要求API級別16,但是我當前的分鐘14,我該如何在14,15個API中使用這個方法?TextureView setSurfaceTexture方法需要API 16級別
UPDATE(添加代碼)
佈局:
<?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>
和片段的方法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」? – pleft
我附上代碼 –
很酷,請問爲什麼你需要調用'mTextureView.setSurfaceTexture(mSurfaceTexture);'?由於所有工作,只要我可以理解的是在'onSurfaceTextureAvailable'內完成,在那裏你用可用的'SurfaceTexture'創建一個新的'Surface'並將其設置在'MediaPlayer'上? – pleft