How To Capture Screenshot Of Videoview When Streaming Video In Android
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
Solution 1:
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.
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 :)
Here is the solution:
publicclassTextureViewActivityextendsActivityimplementsTextureView.SurfaceTextureListener,
OnBufferingUpdateListener,
OnCompletionListener,
OnPreparedListener,
OnVideoSizeChangedListener
{
private MediaPlayer mp;
private TextureView tv;
publicstaticStringMY_VIDEO="https://www.blahblahblah.com/myVideo.mp4";
publicstaticStringTAG="TextureViewActivity";
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_texture_view);
tv = (TextureView) findViewById(R.id.textureView1);
tv.setSurfaceTextureListener(this);
}
publicvoidgetBitmap(TextureView vv)
{
StringmPath= Environment.getExternalStorageDirectory().toString()
+ "/Pictures/" + Utilities.getDayTimeString() + ".png";
Toast.makeText(getApplicationContext(), "Capturing Screenshot: " + mPath, Toast.LENGTH_SHORT).show();
Bitmapbm= vv.getBitmap();
if(bm == null)
Log.e(TAG,"bitmap is null");
OutputStreamfout=null;
FileimageFile=newFile(mPath);
try {
fout = newFileOutputStream(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();
}
}
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.media_player_video, menu);
returntrue;
}
@OverridepublicbooleanonOptionsItemSelected(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.intid= item.getItemId();
if (id == R.id.action_settings) {
returntrue;
}
returnsuper.onOptionsItemSelected(item);
}
@OverridepublicvoidonSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
{
Surfaces=newSurface(surface);
try
{
mp = newMediaPlayer();
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();
Buttonb= (Button) findViewById(R.id.textureViewButton);
b.setOnClickListener(newOnClickListener(){
@OverridepublicvoidonClick(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();
}
}
Post a Comment for "How To Capture Screenshot Of Videoview When Streaming Video In Android"