如何使用纹理视图而不是表面视图从相机直播视频?

Posted

技术标签:

【中文标题】如何使用纹理视图而不是表面视图从相机直播视频?【英文标题】:How to live stream video from camera using texture view and not surfaceview? 【发布时间】:2017-01-31 13:23:46 【问题描述】:

我已经看到了很多关于如何使用 Surfaceview 将视频从 android 摄像头实时流式传输到 rtmp 服务器的示例。一个在这里:https://github.com/begeekmyfriend/yasea

但是是否可以使用纹理视图将视频从相机流式传输到 rtmp?如果是,我们怎么办?

Textureview mTextureView;

// inside oncreate

mTextureView = (TextureView) findViewById(R.id.texture_view);
mTextureView.setSurfaceTextureListener(AircraftControlActivity.this);



// Outside OnCreate
@Override
public void onSurfaceTextureAvailable(final SurfaceTexture surface, final int width, final int height) 



@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) 



@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) 




@Override
public void onSurfaceTextureUpdated(final SurfaceTexture surface) 


接下来要做什么?

【问题讨论】:

【参考方案1】:

看看Texture View

public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener


    Camera camera;
    TextureView textureView;
    ImageButton button ;  //ignore this one

    @Override
    protected void onCreate(Bundle savedInstanceState)
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textureView = (TextureView) findViewById(R.id.textureView);
        button = (ImageButton)findViewById(R.id.imageButton);

        textureView.setSurfaceTextureListener(this);

    

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
    
        camera = Camera.open();
        try
        
            camera.setPreviewTexture(surface);
            camera.startPreview();
        
        catch (IOException ioe)
        
            // Something bad happened
        
    

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height)
    
        // Ignored, Camera does all the work for us
    

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface)
    
        camera.stopPreview();
        camera.release();
        return true;
    

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface)
    
        // Invoked every time there's a new Camera preview frame
    

【讨论】:

以上是关于如何使用纹理视图而不是表面视图从相机直播视频?的主要内容,如果未能解决你的问题,请参考以下文章

android - 使用没有surfaceview或textureview的相机

以视频的纵横比在纹理视图上播放视频

如何在表面视图上绘图?

是否可以使用相机 2 API 捕获没有纹理视图的图像?

如何从 iOS 设备(而不是自己的相机)流式传输 (RTMP) 视频

如何修改默认意图相机[关闭]