2014-12-12 3 views
0

휴대 전화에 로컬로 저장된 비디오를 재생하는 VideoView에서 비디오 프레임을 캡처하는 방법을 알 수있었습니다. 비디오가 VideoView에서 IP를 통해 스트리밍되는 경우 스크린 샷/이미지/비디오 프레임을 캡처하는 것이 매우 어려워 보입니다. 나는이 문제에 대한 해결책을 고맙게 생각한다.Android에서 비디오를 스트리밍 할 때 VideoView의 캡쳐 화면을 캡처하는 방법

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; 
    } 
: 여기 How to capture a frame from video in android?

(비디오는 휴대 전화에 저장되어있는 경우 ONLY) 비디오 프레임을 캡쳐하는 용액이다 : 여기

는 응답을받지 못한 유사한 질문은

답변

0

이 문제에 대한 해결책을 찾았습니다. SurfaceView를 사용하는 동안 VideoView는 저수준 하드웨어 GPU 이유 때문에 이것을 허용하지 않습니다.

해결책은 TextureView를 사용하고 MediaPlayer를 사용하여 그 안에 비디오를 재생하는 것입니다. 액티비티는 TextureView.SurfaceTextureListener를 구현해야합니다. 이 솔루션으로 스크린 샷을 찍을 때 비디오가 잠시 멈 춥니 다. 또한 TextureView는 재생 진행 막대 (재생, 일시 정지, FF/RW, 재생 시간 등)에 대한 기본 UI를 표시하지 않습니다. 그것은 하나의 단점입니다.

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(); 
     } 
    } 
: 다른 솔루션이있는 경우 나 : 여기

을 알려하여 주시기 바랍니다 솔루션입니다

관련 문제