android videoview

mark一下,免得忘记

项目需要验证android videoview对视频容器的支持,需要一个小demo , android程序员各种墨迹,算了,自己动手,丰衣足食.

就顺手写了个小demo,遇到一个坑,sd卡权限,不说了,6.0 搞不定,直接切换sdk到4.0,哈哈,兼容就是好,直接权限就有了,可能需要卸载,不过不是问题

源码位置: https://github.com/godvmxi/VideoTest

Android VideoView中getDuration()方法使用问题

调用setVideoPath之后,VideoView里的MediaPlayer还未处于prepared状态,因此得不到duration.

所以只要videoView.setOnPreparedListener()方法在该监听器中去获取值就行了.

功能嘛及其简单,遍历一个目录,选择播放其中的文件.

package com.example.dan.videotest;

import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.textservice.TextInfo;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import org.w3c.dom.Text;

import java.io.File;

public class MainActivity extends AppCompatActivity {
Button buttonList;
Button buttonNext;
Button buttonPlay;
String TAG;

int fileIndex;
TextView textViewCurFile;
TextView textViewFileList;
File[]  fileList;
VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    TAG =  new String("VideoTest");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    buttonList = (Button)findViewById(R.id.buttonList);
    buttonPlay = (Button)findViewById(R.id.buttonPlay);
    buttonNext = (Button)findViewById(R.id.buttonNext);
    textViewCurFile = (TextView)findViewById(R.id.textViewCurFile);
    textViewFileList = (TextView)findViewById(R.id.textViewFileList);
    videoView = (VideoView)findViewById(R.id.videoView);
    videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            Log.d(TAG, "onPrepared: " + mp.getDuration());
            Toast.makeText(getApplicationContext(), "video length " +mp.getDuration(),1500).show();
        }
    });

    fileIndex = 0;
    fileList  = new File[]{};

    textViewCurFile.setText("Current file -> NULL");
    textViewFileList.setText("File list -> ");

    buttonList.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.d(TAG, "onClick: ");
            //File fileDir = new File("/mnt/sdcard/Movies");
            File fileDir = new File("/mnt/sdcard/Movies");
            Log.d(TAG, "onClick: read ->"+ fileDir.canRead());
            Log.d(TAG, "onClick: write ->"+ fileDir.canWrite());
            fileList = fileDir.listFiles();

            if(fileList.length > 0 ) {
                String fileListString = new String("File list -> ");
                for (int i = 0; i < fileList.length; i++) {
                    Log.d(TAG, "file list -> : " + fileList[i].toString());
                    fileListString = fileListString + "\n" + fileList[i].toString();
                }
                fileIndex = 0;
                textViewFileList.setText(fileListString);
                textViewCurFile.setText(fileList.length + ":" + fileIndex + "->" + fileList[fileIndex].toString());
            }
            else {
                fileIndex = 0;
                fileList =  new File[]{};
                Toast.makeText(getApplicationContext(), "no file in target dir",500).show();
                String fileListString = new String("File list -> NULL");
                textViewFileList.setText(fileListString);
                textViewCurFile.setText(fileList.length+":0-> NULL");
            }
        }
    });

    buttonPlay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //videoView.stopPlayback();
            if(fileList.length > 0) {
                String curFile = fileList[fileIndex].toString();
                videoView.setVideoURI(Uri.parse(curFile));
                videoView.start();
            }
            else {
                Toast.makeText(getApplicationContext(), "no file in target dir",1500).show();
            }
        }
    });

    buttonNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(fileList.length <=> NULL&quot;);
            }
            else {
                fileIndex++;
                if(fileIndex &gt;= fileList.length ){
                    Toast.makeText(getApplicationContext(), &quot;back to first file&quot;,500).show();
                    fileIndex = 0;
                }
                textViewCurFile.setText(fileList.length+&quot;:&quot;+ (fileIndex+1) +&quot;-&gt;&quot; + fileList[fileIndex].toString());
            }
        }
    });
}

}

布局如下:

1

权限:

1