ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

如何在Android中捕获VideoView的屏幕截图或视频帧

2019-10-02 23:27:36  阅读:359  来源: 互联网

标签:android-video-player android video android-videoview


我从这个博客(http://android-er.blogspot.kr/2013/05/get-current-frame-in-videoview-using.html)尝试了以下教程,该教程展示了如何使用MediaMetadataRetriever从视频源捕获视频帧.但是,它仅在视频位于手机本地时才有效.

有没有办法在VideoView通过IP流式传输视频时捕获视频帧?

解决方法:

我找到了解决这个问题的方法.由于使用SurfaceView时出现低级硬件GPU原因,VideoView似乎不允许这样做.

解决方案是使用TextureView并使用MediaPlayer在其中播放视频. Activity需要实现TextureView.SurfaceTextureListener.使用此解决方案截取屏幕截图时,视频会冻结一段时间.此外,TextureView不会显示播放进度条的默认UI(播放,暂停,FF / RW,播放时间等).这是一个缺点.如果您有其他解决方案,请告诉我:)

这是解决方案:

public class TextureViewActivity extends Activity 
    implements TextureView.SurfaceTextureListener, 
                OnBufferingUpdateListener, 
                OnCompletionListener, 
                OnPreparedListener, 
                OnVideoSizeChangedListener 
{
    private MediaPlayer mp;
    private TextureView tv;
    public static String MY_VIDEO = "https://www.blahblahblah.com/myVideo.mp4";
    public static String TAG = "TextureViewActivity";

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

        tv = (TextureView) findViewById(R.id.textureView1);
        tv.setSurfaceTextureListener(this);
    }

    public void getBitmap(TextureView vv)
    {
        String mPath = Environment.getExternalStorageDirectory().toString() 
                + "/Pictures/" + Utilities.getDayTimeString() + ".png";   
        Toast.makeText(getApplicationContext(), "Capturing Screenshot: " + mPath, Toast.LENGTH_SHORT).show();

        Bitmap bm = vv.getBitmap();
        if(bm == null)
            Log.e(TAG,"bitmap is null");

        OutputStream fout = null;
        File imageFile = new File(mPath);

        try {
            fout = new FileOutputStream(imageFile);
            bm.compress(Bitmap.CompressFormat.PNG, 90, fout);
            fout.flush();
            fout.close();
        } catch (FileNotFoundException e) {
            Log.e(TAG, "FileNotFoundException");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e(TAG, "IOException");
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.media_player_video, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) 
    {
        Surface s = new Surface(surface);

        try
        {
            mp = new MediaPlayer();
            mp.setDataSource(MY_VIDEO);
            mp.setSurface(s);
            mp.prepare();

            mp.setOnBufferingUpdateListener(this);
            mp.setOnCompletionListener(this);
            mp.setOnPreparedListener(this);
            mp.setOnVideoSizeChangedListener(this);

            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mp.start();

            Button b = (Button) findViewById(R.id.textureViewButton);
            b.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) 
                {
                    TextureViewActivity.this.getBitmap(tv);
                }
            });
        }
        catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
    }

标签:android-video-player,android,video,android-videoview
来源: https://codeday.me/bug/20191002/1845165.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有