如何知道onSurfaceTextureAvailable为哪个TextureView调用

时间:2016-10-12 11:35:01

标签: android video android-mediaplayer textureview

我正在编写一个应用程序,我需要在我的活动中显示两个TextureView(s)。我使用TextureView内的以下代码使用MediaPlayer对象将视频附加到每个onSurfaceTextureAvailable()实例:

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {

    Surface surface = new Surface(surfaceTexture);

    mMediaPlayer1 = new MediaPlayer();

    try {
        mMediaPlayer1.setDataSource(<filePath>);
    } catch (IOException e) {
        Log.e(TAG, "Unable to load the file from the file system", e);
        return;
    }

    mMediaPlayer1.setSurface(surface);
    mMediaPlayer1.setLooping(true);
    mMediaPlayer1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mediaPlayer) {
            isPlaybackReady = true;
        }
    });

    mMediaPlayer1.prepareAsync();
}

现在,如果我有TextureView的两个实例,我怎么知道onSurfaceTextureAvailable(...)的回调是TextureView

另一种方法是在MediaPlayer之外设置onSurfaceTextureAvailable(...)个对象,方法是通过调用SurfaceTexture来暂停关联的TextureView.getSurfaceTexture()。我也试过了,但它总是返回null。我还需要做些什么呢?

1 个答案:

答案 0 :(得分:1)

您可以使用不同的侦听器设置两个TextureView实例。

textureView1.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
        doSomething(textureView1, surfaceTexture, i, i1);
    }
    // implement other methods 


    });

textureView2.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
        doSomething(textureView2, surfaceTexture, i, i1);
    }

    // implement other methods 
    });

}