Use Mediametadataretriever To Scream Back Frame From Mp4 Interactively

Posted on

MediaMetadataRetriever class, added from API Level 10, provides a unified interface for retrieving frame as well as meta information from an input media file. This instance demo how to recollect frame from mp4 file interactively.


MainActivity.java

package com.example.androidmediametadataretriever;  import android.support.v7.app.ActionBarActivity; import android.content.Intent; import android.graphics.Bitmap; import android.media.MediaMetadataRetriever; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore.Images; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.SeekBar; import android.widget.Toast; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView;  populace course of teaching MainActivity extends ActionBarActivity {   ImageView capturedImageView;   Button btnOpen;  TextView textInfo, textMaxDur, textCurDur;  SeekBar timeFrameBar;    long maxDur;    MediaMetadataRetriever mediaMetadataRetriever = null;   @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_main);      btnOpen = (Button)findViewById(R.id.open);   textInfo = (TextView)findViewById(R.id.info);   textMaxDur = (TextView)findViewById(R.id.maxdur);   textCurDur = (TextView)findViewById(R.id.curdur);   timeFrameBar = (SeekBar)findViewById(R.id.timeframe);    capturedImageView = (ImageView) findViewById(R.id.capturedimage);    mediaMetadataRetriever = novel MediaMetadataRetriever();    btnOpen.setOnClickListener(new OnClickListener(){     @Override    populace void onClick(View v) {      Intent intent = novel Intent(Intent.ACTION_GET_CONTENT,        Images.Media.EXTERNAL_CONTENT_URI);     intent.setType("video/mp4");     startActivityForResult(intent, 0);    }});    timeFrameBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){     @Override    populace void onProgressChanged(SeekBar seekBar, int progress,      boolean fromUser) {}     @Override    populace void onStartTrackingTouch(SeekBar seekBar) {}     @Override    populace void onStopTrackingTouch(SeekBar seekBar) {     updateFrame();    }});  }    mortal void updateFrame(){   int frameProgress = timeFrameBar.getProgress();      long frameTime = maxDur * frameProgress/100;      textCurDur.setText(String.valueOf(frameTime) + " us");   Bitmap bmFrame = mediaMetadataRetriever.getFrameAtTime(frameTime);   capturedImageView.setImageBitmap(bmFrame);  }     @Override  protected void onActivityResult(int requestCode, int resultCode, Intent data) {   super.onActivityResult(requestCode, resultCode, data);      if(resultCode == RESULT_OK){    Uri uri = data.getData();    textInfo.setText(uri.toString());        MediaMetadataRetriever tRetriever = novel MediaMetadataRetriever();     try{     tRetriever.setDataSource(getBaseContext(), uri);          mediaMetadataRetriever = tRetriever;     //extract duration inwards millisecond, equally String     String DURATION = mediaMetadataRetriever.extractMetadata(         MediaMetadataRetriever.METADATA_KEY_DURATION);     textMaxDur.setText(DURATION + " ms");     //convert to us, equally int     maxDur = (long)(1000*Double.parseDouble(DURATION));      timeFrameBar.setProgress(0);     updateFrame();    }catch(RuntimeException e){     e.printStackTrace();     Toast.makeText(MainActivity.this,        "Something Wrong!",        Toast.LENGTH_LONG).show();    }       }  }  } 

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     tools:context="com.example.androidmediametadataretriever.MainActivity" >      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />          <Button         android:id="@+id/open"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Open"/>     <TextView         android:id="@+id/info"         android:layout_width="match_parent"         android:layout_height="wrap_content"/>     <TextView         android:id="@+id/maxdur"         android:layout_width="match_parent"         android:layout_height="wrap_content"/>     <TextView         android:id="@+id/curdur"         android:layout_width="match_parent"         android:layout_height="wrap_content"/>     <SeekBar         android:id="@+id/timeframe"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:max="100"         android:progress="0"/>              <ImageView         android:id="@+id/capturedimage"         android:layout_width="match_parent"         android:layout_height="match_parent" />  </LinearLayout> 

Permission of “android.permission.READ_EXTERNAL_STORAGE” is needed inwards AndroidManifest.xml. And android:minSdkVersion convey to last 10 or higher.

 provides a unified interface for retrieving frame as well as meta information from an input media file Use MediaMetadataRetriever to recollect frame from mp4 interactivelyDownload the files.

Next:
– Create animated GIF from MP4