Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检查Android中的视频有多长?_Java_Android_Android Studio - Fatal编程技术网

Java 检查Android中的视频有多长?

Java 检查Android中的视频有多长?,java,android,android-studio,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.setOnClic

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

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

祝酒人从不露面,怎么了?

看来你的病情不对

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

resultCode
怎么可能同时等于
RESULT\u LOAD\u IMAGE
RESULT\u LOAD\u IMAGE

你的第二种方法是问题所在,它对图像而不是视频使用函数。

你应该在@Override method 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();
        }
    }
}
@覆盖
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
if(requestCode==RESULT\u LOAD\u IMAGE&&resultCode==RESULT\u OK&&data!=null){
//获取图像/数据的地址!!!!!还可以保存服务器的地址!!!!!!。
Uri SevenSecVideo=data.getData();
MediaMetadataRetriever retriever=新的MediaMetadataRetriever();
setDataSource(MainActivity.this,SevenSecVideo);
字符串时间=retriever.extractMetadata(MediaMetadataRetriever.METADATA\u KEY\u DURATION);
long-timeInMilliSec=long.parseLong(时间);
长持续时间=毫秒/1000的时间;
长时间=持续时间/3600;
长分钟=(持续时间-小时*3600)/60;
长秒=持续时间-(小时*3600+分钟*60);

如果(秒)我修复了if语句和TOAST仍然没有出现,我将更新问题并添加错误日志
@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();
        }
    }
}