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()方法在该监听器中去获取值就行了.
功能嘛及其简单,遍历一个目录,选择播放其中的文件.
</pre><pre code_snippet_id="1947575" snippet_file_name="blog_20161025_2_9975143" name="code" class="java">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 <= 0 ){
Toast.makeText(getApplicationContext(), "Click the list first",500).show();
textViewCurFile.setText(fileList.length+":"+ (fileIndex+1)+"-> NULL");
}
else {
fileIndex++;
if(fileIndex >= fileList.length ){
Toast.makeText(getApplicationContext(), "back to first file",500).show();
fileIndex = 0;
}
textViewCurFile.setText(fileList.length+":"+ (fileIndex+1) +"->" + fileList[fileIndex].toString());
}
}
});
}
}
布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
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.dan.videotest.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_marginBottom="1dp"
android:layout_marginEnd="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
tools:layout_marginRight="1dp"
android:layout_marginStart="1dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current File ->!"
android:id="@+id/textViewCurFile" />
<VideoView
android:layout_width="match_parent"
android:id="@+id/videoView"
android:layout_gravity="center_horizontal"
android:layout_height="400dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List"
android:id="@+id/buttonList" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:id="@+id/buttonPlay" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="@+id/buttonNext" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textViewFileList" />
</LinearLayout>
</RelativeLayout>
权限:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dan.videotest">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>