第一种形式:(网络的URL)
Intent it = new Intent();
it.setAction(Intent.ACTION_VIEW);
it.setData(Uri.parse("http://www.baidu.com"));
startActivity(it);
第二种形式:(本地URL)
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/foo.mp3");
intent.setDataAndType(uri, "audio/mp3");
startActivity(intent);
可是有时候我们加载本地URL的形式却是这样子的:/storage/emulated/0/video/test.MP4
如果还是用Uri.parse()可能在一些手机调用不到系统的播放器(播放不了),解决办法如下:
把Uri.parse()
改为Uri.fromFile(new File(URL))
;
有一种需求是获取本地视频的缩略图,网上有很多办法获取,在这里我只介绍一种,本人觉得是最有用,最有效的:(异步,缓存加载)
这里有个工具类:
/**
* Created by CWJ on 2016/4/15.
*/
public class VideoThumbnailLoader {
private static VideoThumbnailLoader mInstance;
private Map<String, SoftReference<Bitmap>> caches;
private List<LoadTask> mTaskQueue;
public static VideoThumbnailLoader getInstance(){
if (mInstance == null){
mInstance = new VideoThumbnailLoader();
}
return mInstance;
}
private VideoThumbnailLoader(){
caches = new HashMap<>();
mTaskQueue = new ArrayList<>();
isRunning = true;
runnable = new LoadRunnable();
new Thread(runnable).start();
}
public void displayThumbnail(ImageView imageView, String videoPath, int resId) {
imageView.setTag(videoPath);
Bitmap bitmap = loadImage(videoPath, getImageCallback(imageView, resId));
if (bitmap == null){
imageView.setImageResource(resId);
}else {
imageView.setImageBitmap(bitmap);
}
}
private Bitmap loadImage(String videoPath, ImageCallback callback) {
if (caches .containsKey(videoPath)){
SoftReference<Bitmap> softReference = caches.get(videoPath);
Bitmap bitmap = softReference.get();
if (bitmap == null){
caches.remove(videoPath);
}else {
return bitmap;
}
}else {
LoadTask task = new LoadTask();
task.path = videoPath;
task.callback = callback;
if (!mTaskQueue.contains(task)){
mTaskQueue.add(task);
synchronized (runnable) {
runnable.notify();
}
}
}
return null;
}
//回调接口
public interface ImageCallback{
void loadImage(String path, Bitmap bitmap);
}
class LoadTask{
String path;
Bitmap bitmap;
ImageCallback callback;
@Override
public boolean equals(Object o) {
LoadTask task = (LoadTask)o;
return task.path.equals(path);
}
}
private LoadRunnable runnable;
private boolean isRunning = false;
class LoadRunnable implements Runnable {
@Override
public void run() {
while (isRunning){
while (mTaskQueue.size() > 0){
LoadTask task = mTaskQueue.remove(0);
task.bitmap = CustomUtil.getVideoThumbnail1(task.path);
caches.put(task.path, new SoftReference<Bitmap>(task.bitmap));
if(handler != null){
Message msg = handler.obtainMessage();
msg.obj = task;
handler.sendMessage(msg);
}
}
synchronized (this) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
private ImageCallback getImageCallback(final ImageView imageView, final int resId){
return new ImageCallback() {
@Override
public void loadImage(String path, Bitmap bitmap) {
if(path.equals(imageView.getTag().toString())){
imageView.setImageBitmap(bitmap);
}else{
imageView.setImageResource(resId);
}
}
};
}
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
LoadTask task = (LoadTask)msg.obj;
task.callback.loadImage(task.path, task.bitmap);
}
};
}
然后在listview或者recycleview加载item:
VideoThumbnailLoader.getInstance().displayThumbnail(holder.iv_photo, sdState + array.get(position - 1), R.drawable.loading_img);
第一个参数的加载的view(ImageView),第二个是文件的路径(URL),第三个是默认加载的图片,也就是当没有获取到视频缩略图时默认显示的图片。