最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Android 保存视频以及读取视频

IT培训 admin 2浏览 0评论

Android 保存视频以及读取视频

 

j最近在Adnroid开发用到了视频的读写操作,记录备忘一下:

都是借用android的med

1、保存TextView到mp4视频

 

2、读取MP4视频

  /*** 获取本地视频缩略图* @param* @return*/public void getVideoThumbnail() {//获得当前时间int sample = 5;//每秒5帧Bitmap bitmap = null;MediaMetadataRetriever mmr = new MediaMetadataRetriever();//实例化MediaMetadataRetriever对象File file = new File(FileHelper.instance().getVideoFilePath(this));//实例化File对象,文件路径为/storage/sdcard/Movies/video.mp4FileOutputStream fos = null;File fileGaode = null;if(file.exists()){mmr.setDataSource(file.getAbsolutePath());//设置数据源为该文件对象指定的绝对路径// 取得视频的长度(单位为毫秒)String time = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);// 转换单位为秒)int seconds = Integer.valueOf(time) / 1000;int dowsample = seconds * sample;// Bitmap bitmap = mmr.getFrameAtTime();//获得视频第一帧的Bitmap对象// 得到每一秒时刻的bitmap比如第一秒,第二秒for (int i = 1; i <= dowsample; i++) {bitmap = mmr.getFrameAtTime(i * 1000 * 1000 / sample, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);String path = FileHelper.instance().getPicFilePath(this) + "/" + i + ".jpg";//保存if(bitmap!=null) {try{fos = new FileOutputStream(path);bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);fos.flush();fos.close();}catch (Exception e){}}}}else{Toast.makeText(MainActivity.this, "文件不存在", Toast.LENGTH_SHORT).show();//文件不存在时,弹出消息提示框}return bitmap;}
    public void saveBitmap2file(Bitmap bmp, String filename) {if (bmp == null){return;}String savePath;String fileName = filename + ".jpeg";savePath = FileHelper.instance().getSDcardPath();File filePic = new File(savePath + fileName);try {if (!filePic.exists()) {filePic.getParentFile().mkdirs();filePic.createNewFile();}FileOutputStream fos = new FileOutputStream(filePic);bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);fos.flush();fos.close();//  Toast.makeText(MainActivity., "保存成功,位置:" + filePic.getAbsolutePath(), Toast.LENGTH_SHORT).show();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}

 

以下为测试在SD卡读写文件

{// 在SD卡目录下创建文件File file = new File(FileHelper.instance().getSDcardPath(), "mysdcard.txt");Log.d(TAG, "file.exists():" + file.exists() + " file.getAbsolutePath():"+ file.getAbsolutePath());if (file.exists()) {file.delete();try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}}// 在SD卡目录下的文件,写入内容FileWriter fw = null;try {fw = new FileWriter(file);} catch (IOException e) {e.printStackTrace();}try {fw.write("我sdcard内容.....");} catch (IOException e) {e.printStackTrace();}try {fw.close();} catch (IOException e) {e.printStackTrace();}Toast.makeText(MainActivity.this, "SD卡写入内容完成...",Toast.LENGTH_LONG).show();Log.d(TAG, "SD卡写入内容完成...");// 读取SD卡文件里面的内容FileReader fr = null;try {fr = new FileReader("/mnt/sdcard/mysdcard.txt");} catch (FileNotFoundException e) {e.printStackTrace();}BufferedReader r = new BufferedReader(fr);String result = null;try {result = r.readLine();} catch (IOException e) {e.printStackTrace();}Log.d(TAG, "SD卡文件里面的内容:" + result);Toast.makeText(MainActivity.this, "SD卡文件里面的内容...",Toast.LENGTH_LONG).show();mBtnRecord.setText(result);
}

3、FFmpegMediaMetadataRetriever 也挺好用

使用

(1)在app下建立libs ,下载aar放进去

 在app的build.gradle的andriod中加入

repositories {flatDir {dirs 'libs'   // aar目录}

 

我的:

android {compileSdkVersion 28buildToolsVersion '28.0.3'defaultConfig {applicationId ""minSdkVersion 21targetSdkVersion versionTargetversionCode 1versionName "1.0"multiDexEnabled truetestInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"externalNativeBuild {cmake {cppFlags "-std=c++11 -frtti -fexceptions "abiFilters "armeabi-v7a", "arm64-v8a"arguments "-DANDROID_STL=gnustl_static" //该处添加gnustl_static,使得可读取opencv JNI的库文件。}ndk {abiFilters "armeabi-v7a", "x86"}}}buildTypes {release {zipAlignEnabled false  //开启优化对齐minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}debug {zipAlignEnabled falseminifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}externalNativeBuild {cmake {path "CMakeLists.txt"}}sourceSets{main{jniLibs.srcDirs = ['src/main/jniLibs']}}repositories {flatDir {dirs 'libs'   // aar目录}}lintOptions {checkReleaseBuilds false// Or, if you prefer, you can continue to check for errors in release builds,// but continue the build even when errors are found:abortOnError false}
}

(2)在app的build.gradle中

Add the following maven dependency to your project's build.gradle file:

dependencies {compile 'com.github.wseemann:FFmpegMediaMetadataRetriever:1.0.14'
}

Optionally, to support individual ABIs:

dependencies {compile 'com.github.wseemann:FFmpegMediaMetadataRetriever-armeabi:1.0.14'compile 'com.github.wseemann:FFmpegMediaMetadataRetriever-armeabi-v7a:1.0.14'compile 'com.github.wseemann:FFmpegMediaMetadataRetriever-x86:1.0.14'compile 'com.github.wseemann:FFmpegMediaMetadataRetriever-mips:1.0.14'compile 'com.github.wseemann:FFmpegMediaMetadataRetriever-x86_64:1.0.14'compile 'com.github.wseemann:FFmpegMediaMetadataRetriever-arm64-v8a:1.0.14'
}

Android 保存视频以及读取视频

 

j最近在Adnroid开发用到了视频的读写操作,记录备忘一下:

都是借用android的med

1、保存TextView到mp4视频

 

2、读取MP4视频

  /*** 获取本地视频缩略图* @param* @return*/public void getVideoThumbnail() {//获得当前时间int sample = 5;//每秒5帧Bitmap bitmap = null;MediaMetadataRetriever mmr = new MediaMetadataRetriever();//实例化MediaMetadataRetriever对象File file = new File(FileHelper.instance().getVideoFilePath(this));//实例化File对象,文件路径为/storage/sdcard/Movies/video.mp4FileOutputStream fos = null;File fileGaode = null;if(file.exists()){mmr.setDataSource(file.getAbsolutePath());//设置数据源为该文件对象指定的绝对路径// 取得视频的长度(单位为毫秒)String time = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);// 转换单位为秒)int seconds = Integer.valueOf(time) / 1000;int dowsample = seconds * sample;// Bitmap bitmap = mmr.getFrameAtTime();//获得视频第一帧的Bitmap对象// 得到每一秒时刻的bitmap比如第一秒,第二秒for (int i = 1; i <= dowsample; i++) {bitmap = mmr.getFrameAtTime(i * 1000 * 1000 / sample, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);String path = FileHelper.instance().getPicFilePath(this) + "/" + i + ".jpg";//保存if(bitmap!=null) {try{fos = new FileOutputStream(path);bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);fos.flush();fos.close();}catch (Exception e){}}}}else{Toast.makeText(MainActivity.this, "文件不存在", Toast.LENGTH_SHORT).show();//文件不存在时,弹出消息提示框}return bitmap;}
    public void saveBitmap2file(Bitmap bmp, String filename) {if (bmp == null){return;}String savePath;String fileName = filename + ".jpeg";savePath = FileHelper.instance().getSDcardPath();File filePic = new File(savePath + fileName);try {if (!filePic.exists()) {filePic.getParentFile().mkdirs();filePic.createNewFile();}FileOutputStream fos = new FileOutputStream(filePic);bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);fos.flush();fos.close();//  Toast.makeText(MainActivity., "保存成功,位置:" + filePic.getAbsolutePath(), Toast.LENGTH_SHORT).show();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}

 

以下为测试在SD卡读写文件

{// 在SD卡目录下创建文件File file = new File(FileHelper.instance().getSDcardPath(), "mysdcard.txt");Log.d(TAG, "file.exists():" + file.exists() + " file.getAbsolutePath():"+ file.getAbsolutePath());if (file.exists()) {file.delete();try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}}// 在SD卡目录下的文件,写入内容FileWriter fw = null;try {fw = new FileWriter(file);} catch (IOException e) {e.printStackTrace();}try {fw.write("我sdcard内容.....");} catch (IOException e) {e.printStackTrace();}try {fw.close();} catch (IOException e) {e.printStackTrace();}Toast.makeText(MainActivity.this, "SD卡写入内容完成...",Toast.LENGTH_LONG).show();Log.d(TAG, "SD卡写入内容完成...");// 读取SD卡文件里面的内容FileReader fr = null;try {fr = new FileReader("/mnt/sdcard/mysdcard.txt");} catch (FileNotFoundException e) {e.printStackTrace();}BufferedReader r = new BufferedReader(fr);String result = null;try {result = r.readLine();} catch (IOException e) {e.printStackTrace();}Log.d(TAG, "SD卡文件里面的内容:" + result);Toast.makeText(MainActivity.this, "SD卡文件里面的内容...",Toast.LENGTH_LONG).show();mBtnRecord.setText(result);
}

3、FFmpegMediaMetadataRetriever 也挺好用

使用

(1)在app下建立libs ,下载aar放进去

 在app的build.gradle的andriod中加入

repositories {flatDir {dirs 'libs'   // aar目录}

 

我的:

android {compileSdkVersion 28buildToolsVersion '28.0.3'defaultConfig {applicationId ""minSdkVersion 21targetSdkVersion versionTargetversionCode 1versionName "1.0"multiDexEnabled truetestInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"externalNativeBuild {cmake {cppFlags "-std=c++11 -frtti -fexceptions "abiFilters "armeabi-v7a", "arm64-v8a"arguments "-DANDROID_STL=gnustl_static" //该处添加gnustl_static,使得可读取opencv JNI的库文件。}ndk {abiFilters "armeabi-v7a", "x86"}}}buildTypes {release {zipAlignEnabled false  //开启优化对齐minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}debug {zipAlignEnabled falseminifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}externalNativeBuild {cmake {path "CMakeLists.txt"}}sourceSets{main{jniLibs.srcDirs = ['src/main/jniLibs']}}repositories {flatDir {dirs 'libs'   // aar目录}}lintOptions {checkReleaseBuilds false// Or, if you prefer, you can continue to check for errors in release builds,// but continue the build even when errors are found:abortOnError false}
}

(2)在app的build.gradle中

Add the following maven dependency to your project's build.gradle file:

dependencies {compile 'com.github.wseemann:FFmpegMediaMetadataRetriever:1.0.14'
}

Optionally, to support individual ABIs:

dependencies {compile 'com.github.wseemann:FFmpegMediaMetadataRetriever-armeabi:1.0.14'compile 'com.github.wseemann:FFmpegMediaMetadataRetriever-armeabi-v7a:1.0.14'compile 'com.github.wseemann:FFmpegMediaMetadataRetriever-x86:1.0.14'compile 'com.github.wseemann:FFmpegMediaMetadataRetriever-mips:1.0.14'compile 'com.github.wseemann:FFmpegMediaMetadataRetriever-x86_64:1.0.14'compile 'com.github.wseemann:FFmpegMediaMetadataRetriever-arm64-v8a:1.0.14'
}
11136

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论