直接上代碼:
布局文件就是兩個全屏的videoview和imageview重疊
<LinearLayout?xmlns:android="/apk/res/android"android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/pictureView"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
java部分:
/*視頻圖片混合展示demo
*/package?com.example.administrator.hunbo;
import?android.content.Context;
import?android.media.MediaPlayer;
import?android.net.Uri;
import?android.os.Handler;
import?android.os.storage.StorageManager;
import?android.support.v7.app.AppCompatActivity;
import?android.os.Bundle;
import?android.view.View;
import?android.widget.ImageView;
import?android.widget.Toast;
import?android.widget.VideoView;
import?java.io.File;
import?java.lang.reflect.InvocationTargetException;
import?java.lang.reflect.Method;
import?java.util.ArrayList;
public?class?MainActivity?extends?AppCompatActivity?{
private?VideoView?videoView;
private?ImageView?pictureView;
boolean?isPlaying?=?false;
private?ArrayList?arrayList?=?new?ArrayList<String>();
@Override
protected?void?onCreate(Bundle?savedInstanceState)?{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();//隱藏actionBar
//初始化控件
videoView?=?findViewById(R.id.videoView);
pictureView?=?findViewById(R.id.pictureView);
//最開始兩個view都是隱藏的
videoView.setVisibility(View.GONE);
pictureView.setVisibility(View.GONE);
//檢測SD卡是否存在
String[]?result?=?null;
StorageManager?storageManager?=?(StorageManager)?getSystemService(Context.STORAGE_SERVICE);
try?{
Method?method?=?StorageManager.class.getMethod("getVolumePaths");
method.setAccessible(true);
try?{
result?=?(String[])?method.invoke(storageManager);
}?catch?(InvocationTargetException?e)?{
e.printStackTrace();
}
if?(result.length?>?1)?{
//Toast.makeText(this,?"檢測到U盤",?Toast.LENGTH_SHORT).show();
AllFilesPath("/mnt/usb/");
playList();
}?else?{
Toast.makeText(this,?"未檢測到U盤,請在開機前插入U盤,或者重新啟動此應用",?Toast.LENGTH_SHORT).show();
Handler?handler?=?new?Handler();
handler.postDelayed(new?Runnable()?{
@Override
public?void?run()?{
finish();
}
},?2500);
}
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
//獲取所有圖片視頻的絕對路徑到arraylist
private?ArrayList?AllFilesPath(String?path)?{
File?file?=?new?File(path);
File[]?files?=?file.listFiles();
for?(File?f?:?files)?{
if?(f.getName().endsWith("jpg")?||?f.getName().endsWith("jpeg")?||?f.getName().endsWith("mp4")
||?f.getName().endsWith("avi")?||?f.getName().endsWith("mkv")?||?f.getName().endsWith("rmvb")
||?f.getName().endsWith("flv"))?{
System.out.println("------------獲取到了壹個可用路徑:"+f.getAbsolutePath());
arrayList.add(f.getAbsolutePath());//添加到arralist
}?else?if?(f.isDirectory())?{
AllFilesPath(f.getAbsolutePath());
}
}
return?arrayList;
}
//依次混合播放arralist裏的圖片或視頻
public?int?listNum;
private?void?playList()?{
if?(listNum?>=?arrayList.size()){
finish();
//listNum?=?0;
}else?{
System.out.println("---------------------------收入路徑---------------------------");
System.out.println("isplaying?=?"+isPlaying);
final?File?f?=?new?File(arrayList.get(listNum).toString());
if?(f.getName().endsWith("jpg")?||?f.getName().endsWith("jpeg")||f.getName().endsWith("png"))?{
System.out.println("---------------------------添加了壹張圖片:"+f.getAbsolutePath());
pictureView.setVisibility(View.VISIBLE);
pictureView.setImageURI(Uri.fromFile(f));
Handler?handler?=?new?Handler();
handler.postDelayed(new?Runnable()?{
@Override
public?void?run()?{
System.out.println("---------------------------播完了壹張位於"+f.getAbsolutePath()+"的圖片》》》》》》》》》》》》》》》》》");
pictureView.setVisibility(View.GONE);
listNum++;
playList();
}
},?2000);//2秒後結束當前圖片
}?else?if?(f.getName().endsWith("mp4")?||?f.getName().endsWith("avi")?||?f.getName().endsWith("mkv")
||?f.getName().endsWith("rmvb")?||?f.getName().endsWith("flv"))?{
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~添加了壹個視頻"+f.getAbsolutePath());
videoView.setVideoPath(f.getAbsolutePath());
videoView.setVisibility(View.VISIBLE);//播放之前顯示videoView
videoView.start();
videoView.setOnCompletionListener(new?MediaPlayer.OnCompletionListener()?{
@Override
public?void?onCompletion(MediaPlayer?mp)?{
System.out.println("---------------------------播完了壹個位於"+f.getAbsolutePath()+"的視頻《《《《《《《《《《《《《《《《《《");
videoView.setVisibility(View.GONE);//視頻播放完畢後隱藏videoView
listNum++;
playList();
}
});
}
}
}