kotlin 写法
package com.guwu.varysandroid.utils
import it.sauronsoftware.jave.Encoder
import it.sauronsoftware.jave.EncoderException
import java.io.File
import java.io.IOException
import java.net.UnknownHostException
/**
* Created by admin 2019/10/21/14:52
* Describe
*作者 洪亮 admin
*/
object VideoInfoTest {
/**
* 获取网络文件,暂存为临时文件
* @param url
* @return
* @throws UnknownHostException
* @throws IOException
*/
@Throws(UnknownHostException::class, IOException::class)
fun getFileByUrl(url: String): File {
val tmpFile = File.createTempFile("temp", ".tmp")//创建临时文件
Image2Binary.toBDFile(url, tmpFile.canonicalPath)
return tmpFile
}
/**
* 获取时长
* @param
* @return
* @throws IOException
* @throws
* @throws EncoderException
*/
@Throws(EncoderException::class)
fun getDuration(file: File): Long {
val m = Encoder().getInfo(file)
return m.duration
}
}
java 写法
package com.guwu.varysandroid.utils;
import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.MultimediaInfo;
/**
* Created by admin 2019/10/21/12:00
* Describe
* 作者 洪亮 admin
*/
public class VideoInfoTest {
/**
* 获取网络文件,暂存为临时文件
* @param url
* @return
* @throws UnknownHostException
* @throws IOException
*/
public static File getFileByUrl(String url) throws UnknownHostException, IOException {
File tmpFile = File.createTempFile("temp", ".tmp");//创建临时文件
Image2Binary.toBDFile(url, tmpFile.getCanonicalPath());
return tmpFile;
}
/**
* 获取时长
* @param
* @return
* @throws IOException
* @throws
* @throws EncoderException
*/
public static long getDuration(File file) throws EncoderException {
MultimediaInfo m = new Encoder().getInfo(file);
return m.getDuration();
}
}
Image2Binary 类
package com.guwu.varysandroid.utils;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
/**
* Created by admin 2019/10/21/12:00
* Describe
* 作者 洪亮 admin
*/
public class Image2Binary
{
public static byte[] toByteArray(InputStream in) throws IOException {
ByteArrayOutputStream out=new ByteArrayOutputStream();
byte[] buffer=new byte[1024*4];
int n=0;
while ( (n=in.read(buffer)) !=-1) {
out.write(buffer,0,n);
}
return out.toByteArray();
}
/**
* 网络文件转换为byte二进制
* @Title: toByteArray
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param url
* @param @return
* @param @throws IOException 设定文件
* @return byte[] 返回类型
* @throws
*/
public static byte[] toByteArray(String urlStr) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
DataInputStream in = new DataInputStream(conn.getInputStream());
ByteArrayOutputStream out=new ByteArrayOutputStream();
byte[] buffer=new byte[1024*4];
int n=0;
while ( (n=in.read(buffer)) !=-1) {
out.write(buffer,0,n);
}
return out.toByteArray();
}
/**
* @throws IOException
* @throws MalformedURLException
* 网络文件转换为本地文件
* @Title: toByteArray
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param url
* @param @return
* @param @throws IOException 设定文件
* @return byte[] 返回类型
* @throws
*/
public static void toBDFile(String urlStr, String bdUrl) throws IOException, UnknownHostException {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
DataInputStream in = new DataInputStream(conn.getInputStream());
byte[] data=toByteArray(in);
in.close();
FileOutputStream out=new FileOutputStream(bdUrl);
out.write(data);
out.close();
}
/**
* 获取网络文件的输入流
* @param urlStr
* @return
*/
public static InputStream getInputStreamByUrl(String urlStr){
DataInputStream in = null;
try {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
in = new DataInputStream(conn.getInputStream());
} catch (IOException e) {
Log.d("url转换输入流失败,错误信息{}",""+e.getMessage());
}
return in;
}
}
需要下载一个java jar包
http://www.sauronsoftware.it/projects/jave/download.php 下载路径