如何捕捉视频Android的框架? [英] How to capture a frame from video in android?

查看:188
本文介绍了如何捕捉视频Android的框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我已在一个自定义的视频打出android.with一些简单的播放,暂停,再玩一次和俘获button.NOw我所做的这一切,除了功能捕获。我已经seacrched很多很多的链接和GOOGLE了很多,但我找不到如何捕捉在运行Android视频帧?我的code是如下:请帮我要的是:

Main.java

 包org.apache.android.media;
进口的java.io.File;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口的java.net.URL;
进口java.net.URLConnection中;
进口android.app.Activity;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.webkit.URLUtil;
进口android.widget.EditText;
进口android.widget.ImageButton;
进口android.widget.Toast;    进口android.widget.VideoView;
    公共类VideoViewDemo延伸活动{
        私有静态最后弦乐TAG =VideoViewDemo;
        私人VideoView mVideoView;
        私人的EditText的mpath;
        私人的ImageButton mPlay;
        私人的ImageButton mPause;
        私人的ImageButton MRESET;
        私人的ImageButton MSTOP;
        私人的ImageButton mcaptur;
        私人字符串电流;
        @覆盖
        公共无效的onCreate(捆绑冰柱){
            super.onCreate(冰柱);
            的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.C​​aptur);
            mPlay.setOnClickListener(新OnClickListener(){
                公共无效的onClick(查看视图){
                    的playVideo();
                }
            });
            mPause.setOnClickListener(新OnClickListener(){
                公共无效的onClick(查看视图){
                    如果(mVideoView!= NULL){
                        mVideoView.pause();
                    }
                }
            });
            mReset.setOnClickListener(新OnClickListener(){
                公共无效的onClick(查看视图){
                    如果(mVideoView!= NULL){
                        mVideoView.seekTo(0);
                    }
                }
            });
            mStop.setOnClickListener(新OnClickListener(){
                公共无效的onClick(查看视图){
                    如果(mVideoView!= NULL){
                        电流=无效;
                        mVideoView.stopPlayback();
                    }
                }
            });
            runOnUiThread(新的Runnable(){
                公共无效的run(){
                    的playVideo();
                }
            });
            mcaptur.setOnClickListener(新OnClickListener(){
                @覆盖
                公共无效的onClick(视图v){
                }
            });
        }
        私人无效的playVideo(){
            尝试{
                。最终的字符串路径= mPath.getText()的toString();
                Log.v(TAG,路径为:+路径);
                如果(路径== NULL || path.length()== 0){
                    Toast.makeText(VideoViewDemo.this,文件URL /路径为空,
                            Toast.LENGTH_LONG).show();
                }其他{
                    //如果路径没有改变,只是开始在媒体播放器
                    如果(path.equals(电流)及&放大器;!mVideoView =空){
                        mVideoView.start();
                        mVideoView.requestFocus();
                        返回;
                    }
                    电流=路径;
                    mVideoView.setVideoPath(了getDataSource(路径));
                    mVideoView.start();
                    mVideoView.requestFocus();
                }
            }赶上(例外五){
                Log.e(TAG,错误:+ e.getMessage(),E);
                如果(mVideoView!= NULL){
                    mVideoView.stopPlayback();
                }
            }
        }
        私人了getDataSource字符串(字符串路径)抛出IOException
            如果(!URLUtil.isNetworkUrl(路径)){
                返回路径;
            }其他{
                网址URL =新的URL(路径);
                的URLConnection CN = url.openConnection();
                cn.connect();
                InputStream的流= cn.getInputStream();
                如果(流== NULL)
                    抛出新的RuntimeException(流为空);
                文件TEMP = File.createTempFile(mediaplayertmp,DAT);
                temp.deleteOnExit();
                串TEMPPATH = temp.getAbsolutePath();
                FileOutputStream中出=新的FileOutputStream(TEMP);
                中byte buf [] =新的字节[128];
                做{
                    INT numread = stream.read(BUF);
                    如果(numread&下; = 0)
                        打破;
                    out.write(BUF,0,numread);
                }而(真);
                尝试{
                    stream.close();
                }赶上(IOException异常前){
                    Log.e(TAG,错误:+ ex.getMessage(),除息);
                }
                返回TEMPPATH;
            }
        }
    }


解决方案

您可以用 MediaMetadataRetriever 视频帧。其基本用法如下:

  MediaMetadataRetriever猎犬=新MediaMetadataRetriever();//设置数据源检索。
//从code,你可能想使用你的'字符串路径'在这里。
retriever.setDataSource(yourPath);//指定时间获取位图一帧。
//请注意,参数必须是在微秒,而不是毫秒。
位图位图= retriever.getFrameAtTime(timeInMicroSeconds);//使用您的位图的东西。

您可能需要使用 FFmpegMediaMetadataRetriever 为更好的性能。

Hi i have made a custom video played in android.with some simple "play","pause","play again" and "capture" button.NOw i have done all this functionalities except "capture".I have seacrched lot many links and googled a lot,But i can't find how to capture a frame from running video in android? my code is as below: please help me for is:

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;
            }
        }
    }

解决方案

You can get a video frame with MediaMetadataRetriever. The basic usage is as follows.

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.

You might want to use FFmpegMediaMetadataRetriever for better performance.

这篇关于如何捕捉视频Android的框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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