2013-10-28 45 views
3

嗨,我做了一個自定義視頻播放android.with一些簡單的「玩」,「暫停」,「再次播放」和「捕捉」button.NOw我已經完成了除「捕獲」之外的所有這些功能。我已經seacrched很多許多鏈接和google搜索了很多,但我無法找到如何捕捉在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; 
      } 
     } 
    } 
+0

我認爲你不能用視頻視圖來做到這一點。使用'open cv'庫來做這種事情。 –

+0

當點擊捕獲按鈕時,你想顯示與該視頻相關的圖像? –

+0

@ AliImran-thanx ali ...但我可以得到一些很好的鏈接或一些東西? – jigar

回答

0

可以使用的ffmpeg

請參閱此鏈接查看更多細節

How to extract images from a Video using FFmpeg

您需要在您的活動中運行此ffmpeg命令才能實現此目的

還可以墊視頻,作物視頻使用的ffmpeg

+0

兄弟..我做了我的自定義視頻播放器,只有我必須添加捕捉功能..沒有得到我的問題 – jigar

3

你可以得到一個視頻幀與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以獲得更好的性能。