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;
public class 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 = new MediaMetadataRetriever();
btnOpen.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT,
Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("video/mp4");
startActivityForResult(intent, 0);
}});
timeFrameBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
updateFrame();
}});
}
private 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 = new MediaMetadataRetriever();
try{
tRetriever.setDataSource(getBaseContext(), uri);
mediaMetadataRetriever = tRetriever;
//extract duration in millisecond, as String
String DURATION = mediaMetadataRetriever.extractMetadata(
MediaMetadataRetriever.METADATA_KEY_DURATION);
textMaxDur.setText(DURATION + " ms");
//convert to us, as 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="http://android-er.blogspot.com/"
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 in AndroidManifest.xml. And android:minSdkVersion have to be 10 or higher.
Download the files.
Next:
- Create animated GIF from MP4
simple and useful thanks.
ReplyDelete