Lazar Kukolj
Lazar Kukolj

Reputation: 706

check how long video is in Android?

So i want the app to open up to my videos when i click a button, then save the video or directory in a variable then use that variable to check the length of the video. this is my code so far:

    public void SevenSecVideo(){
    SevenSecVideo = (Button)findViewById(R.id.SevenSecondBtn);//Finds the button in design and put it into a button variable.
    SevenSecVideo.setOnClickListener(//Listens for a button click.
            new View.OnClickListener() {//Creates a new click listener.
                @Override
                public void onClick(View v) {

                    Intent mediaChooser = new Intent(Intent.ACTION_GET_CONTENT);
                    //comma-separated MIME types
                    mediaChooser.setType("video/*");
                    startActivityForResult(mediaChooser, RESULT_LOAD_IMAGE);
                }
            }
    );
}


@Override
public void onActivityReenter(int resultCode, Intent data) {
    super.onActivityReenter(resultCode, data);

    if(resultCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null){
        //gets the address of the image/data !!!!!!ALSO CAN HOLD THE ADDRESS FOR THE SERVER!!!!!!.
        Uri SevenSecVideo = data.getData();

        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setDataSource(String.valueOf(SevenSecVideo));
        String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        long timeInMilliSec = Long.parseLong( time );
        long duration = timeInMilliSec / 1000;
        long hours = duration / 3600;
        long minutes = (duration - hours * 3600) / 60;
        long seconds = duration - (hours * 3600 + minutes * 60);

        if (seconds <= 7){
            Toast.makeText(this, "is 7 sec or less!", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "is NOT 7 sec or less!", Toast.LENGTH_SHORT).show();
        }
    }
}

Error Logs:

03-10 00:05:22.086 16868-16904/com.wilsapp.wilsapp D/OpenGLRenderer: endAllStagingAnimators on 0x9f351280 (RippleDrawable) with handle 0xaefbf980
03-10 00:05:35.406 16868-16868/com.wilsapp.wilsapp D/skia: --- SkImageDecoder::Factory returned null
03-10 00:05:35.407 16868-16868/com.wilsapp.wilsapp I/System.out: resolveUri failed on bad bitmap uri: content://com.android.providers.media.documents/document/video%3A370166

The TOASTs never show up, whats wrong?

Upvotes: 1

Views: 254

Answers (3)

Chirag Chavda
Chirag Chavda

Reputation: 556

you should write your second function code in @Override method onActivityResult as below

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null){
        //gets the address of the image/data !!!!!!ALSO CAN HOLD THE ADDRESS FOR THE SERVER!!!!!!.
        Uri SevenSecVideo = data.getData();
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setDataSource(MainActivity.this,SevenSecVideo);
        String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        long timeInMilliSec = Long.parseLong( time );
        long duration = timeInMilliSec / 1000;
        long hours = duration / 3600;
        long minutes = (duration - hours * 3600) / 60;
        long seconds = duration - (hours * 3600 + minutes * 60);

        if (seconds <= 7){
            Toast.makeText(this, "is 7 sec or less!", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "is NOT 7 sec or less!", Toast.LENGTH_SHORT).show();
        }
    }
}

Upvotes: 2

Lazar
Lazar

Reputation: 89

your second method is the issue, its using functions for images not video.

Upvotes: 0

Vasily Kabunov
Vasily Kabunov

Reputation: 6761

It seems that your condition is wrong

 if(resultCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK ...

How is it possible that resultCode equals RESULT_LOAD_IMAGE and RESULT_LOAD_IMAGE at the same time.

Upvotes: 0

Related Questions