ICode9

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

如何在Android视频中捕获帧?

2019-11-22 16:28:18  阅读:192  来源: 互联网

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


嗨,我制作了一个自定义视频,在android中播放.带有一些简单的“播放”,“暂停”,“再次播放”和“捕捉”按钮.现在,我已经完成了除“捕捉”之外的所有这些功能.我已经找到了很多链接和谷歌搜索很多,但是我找不到如何从运行Android的视频中捕获帧?我的代码如下:请帮助我的是:

Main.java

package org.apache.android.media;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.URLUtil;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

    import android.widget.VideoView;
    public class VideoViewDemo extends Activity {
        private static final String TAG = "VideoViewDemo";
        private VideoView mVideoView;
        private EditText mPath;
        private ImageButton mPlay;
        private ImageButton mPause;
        private ImageButton mReset;
        private ImageButton mStop;
        private ImageButton mcaptur;
        private String current;
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.main);
            mVideoView = (VideoView) findViewById(R.id.surface_view);
            mPath = (EditText) findViewById(R.id.path);
            mPath.setText("http://daily3gp.com/vids/747.3gp");
            mPlay = (ImageButton) findViewById(R.id.play);
            mPause = (ImageButton) findViewById(R.id.pause);
            mReset = (ImageButton) findViewById(R.id.reset);
            mStop = (ImageButton) findViewById(R.id.stop);
            mcaptur = (ImageButton) findViewById(R.id.Captur);
            mPlay.setOnClickListener(new OnClickListener() {
                public void onClick(View view) {
                    playVideo();
                }
            });
            mPause.setOnClickListener(new OnClickListener() {
                public void onClick(View view) {
                    if (mVideoView != null) {
                        mVideoView.pause();
                    }
                }
            });
            mReset.setOnClickListener(new OnClickListener() {
                public void onClick(View view) {
                    if (mVideoView != null) {
                        mVideoView.seekTo(0);
                    }
                }
            });
            mStop.setOnClickListener(new OnClickListener() {
                public void onClick(View view) {
                    if (mVideoView != null) {
                        current = null;
                        mVideoView.stopPlayback();
                    }
                }
            });
            runOnUiThread(new Runnable() {
                public void run() {
                    playVideo();
                }
            });
            mcaptur.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
        }
        private void playVideo() {
            try {
                final String path = mPath.getText().toString();
                Log.v(TAG, "path: " + path);
                if (path == null || path.length() == 0) {
                    Toast.makeText(VideoViewDemo.this, "File URL/path is empty",
                            Toast.LENGTH_LONG).show();
                } else {
                    // If the path has not changed, just start the media player
                    if (path.equals(current) && mVideoView != null) {
                        mVideoView.start();
                        mVideoView.requestFocus();
                        return;
                    }
                    current = path;
                    mVideoView.setVideoPath(getDataSource(path));
                    mVideoView.start();
                    mVideoView.requestFocus();
                }
            } catch (Exception e) {
                Log.e(TAG, "error: " + e.getMessage(), e);
                if (mVideoView != null) {
                    mVideoView.stopPlayback();
                }
            }
        }
        private String getDataSource(String path) throws IOException {
            if (!URLUtil.isNetworkUrl(path)) {
                return path;
            } else {
                URL url = new URL(path);
                URLConnection cn = url.openConnection();
                cn.connect();
                InputStream stream = cn.getInputStream();
                if (stream == null)
                    throw new RuntimeException("stream is null");
                File temp = File.createTempFile("mediaplayertmp", "dat");
                temp.deleteOnExit();
                String tempPath = temp.getAbsolutePath();
                FileOutputStream out = new FileOutputStream(temp);
                byte buf[] = new byte[128];
                do {
                    int numread = stream.read(buf);
                    if (numread <= 0)
                        break;
                    out.write(buf, 0, numread);
                } while (true);
                try {
                    stream.close();
                } catch (IOException ex) {
                    Log.e(TAG, "error: " + ex.getMessage(), ex);
                }
                return tempPath;
            }
        }
    }

解决方法:

您可以使用MediaMetadataRetriever获得视频帧.基本用法如下.

MediaMetadataRetriever retriever = new MediaMetadataRetriever();

// Set data source to retriever.
// From your code, you might want to use your 'String path' here.
retriever.setDataSource(yourPath);

// Get a frame in Bitmap by specifying time.
// Be aware that the parameter must be in "microseconds", not milliseconds.
Bitmap bitmap = retriever.getFrameAtTime(timeInMicroSeconds);

// Do something with your bitmap.

您可能要使用FFmpegMediaMetadataRetriever以获得更好的性能.

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

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

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

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

ICode9版权所有