在Android中流式传输视频时如何捕获VideoView的屏幕截图 [英] How to capture screenshot of VideoView when streaming video in Android

查看:181
本文介绍了在Android中流式传输视频时如何捕获VideoView的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够弄清楚如何从正在播放本地存储在手机中的视频的VideoView中捕获视频帧. 但是,当视频通过IP在VideoView中流式传输时,捕获屏幕截图/图像/视频帧似乎非常困难.对于此问题的解决方案,我将不胜感激.

I was able to figure out how to capture a video frame from a VideoView that is playing a video stored locally on the phone. However, when the video is being streamed over IP in the VideoView, it appears very difficult to capture a screenshot/image/video frame. I would appreciate a solution to this problem.

这是一个类似的问题,尚未得到答案: 如何从android视频中捕获帧?

Here is a similar question which has not received an answer: How to capture a frame from video in android?

这是一种捕获视频帧的解决方案(如果视频存储在手机中,则):

Here is a solution to capturing the video frame (ONLY if the video is stored on the phone):

public static Bitmap captureVideoFrame(Activity activity, VideoView vv, String viewSource, int currPosInMs)
    {   
        MediaMetadataRetriever mmRetriever = new MediaMetadataRetriever();
        mmRetriever.setDataSource(viewSource);

        Bitmap bmFrame = mmRetriever.getFrameAtTime(currPosInMs * 1000); // unit in microsecond

        if(bmFrame == null){
            Toast.makeText(activity.getApplicationContext(), "Bitmap is null! Curr Position: " + currPosInMs, Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(activity.getApplicationContext(), "Bitmap is not null! Curr Position: " + currPosInMs, Toast.LENGTH_SHORT).show();

            // Save file
            String mPath = Environment.getExternalStorageDirectory().toString() + "/" + SCREENSHOT_DIRECTORY + "myScreenshot.png";
            OutputStream fout = null;
            File imageFile = new File(mPath);

            try {
                fout = new FileOutputStream(imageFile);
                bmFrame.compress(Bitmap.CompressFormat.JPEG, 100, fout);
                fout.flush();
                fout.close();

                Toast.makeText(activity.getApplicationContext(), "Saved file successfully!", Toast.LENGTH_SHORT).show();
            }
            catch(Exception e){

            }
        }

        return bmFrame;
    }

推荐答案

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

I have found a solution to this problem. It appears that the VideoView does not allow this because of low-level hardware GPU reasons while using a SurfaceView.

解决方案是使用TextureView并使用MediaPlayer在其中播放视频.该活动将需要实现TextureView.SurfaceTextureListener.使用此解决方案拍摄屏幕截图时,视频会停顿一小段时间.另外,TextureView不会为播放进度栏(播放,暂停,FF/RW,播放时间等)显示默认的UI.那是一个缺点.如果您还有其他解决方案,请告诉我:)

The solution is to use a TextureView and use a MediaPlayer to play a video inside of it. The Activity will need to implement TextureView.SurfaceTextureListener. When taking a screenshot with this solution, the video freezes for a short while. Also, the TextureView does not display a default UI for the playback progress bar (play, pause, FF/RW, play time, etc). That is one drawback. If you have another solution please let me know :)

这是解决方案:

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中流式传输视频时如何捕获VideoView的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆