检查Android中的视频有多长?

时间:2016-03-10 04:50:51

标签: java android android-studio

所以我希望应用程序在我单击按钮时打开我的视频,然后将视频或目录保存在变量中,然后使用该变量检查视频的长度。到目前为止,这是我的代码:

    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();
        }
    }
}

错误日志:

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

TOASTs永远不会出现,什么是错的?

3 个答案:

答案 0 :(得分:1)

您应该在@Override方法onActivityResult中编写第二个功能代码,如下所示

@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();
        }
    }
}

答案 1 :(得分:0)

您的情况似乎错了

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

resultCode如何可能同时等于RESULT_LOAD_IMAGE RESULT_LOAD_IMAGE

答案 2 :(得分:0)

你的第二种方法是问题,它使用的图像功能不是视频。