博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
安卓学习-Camera,音频录制与Vitamio框架
阅读量:6625 次
发布时间:2019-06-25

本文共 7929 字,大约阅读时间需要 26 分钟。

hot3.png

 

案例:

activity_main.xml

MainActivity.java

package com.example.administrator.cameravideo; import android.content.Intent; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import java.io.File; public class MainActivity extends AppCompatActivity {
private static final int MEDIA_TYPE_IMAGE = 1; private static final int MEDIA_TYPE_VIDEO = 2; @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //拍照 public void imageCapture(View view) {
Intent intent = new Intent(); intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFileUri(MEDIA_TYPE_IMAGE)); startActivityForResult(intent, MEDIA_TYPE_IMAGE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); switch (resultCode) {
case RESULT_OK: if (MEDIA_TYPE_IMAGE == requestCode) {
//图片 // Uri uri= data.getData(); System.out.println(file); } else if (MEDIA_TYPE_VIDEO == requestCode) {
System.out.println(file); System.out.println(data.getData()); } break; } } //录制视频 public void videoCapture(View view) {
Intent intent=new Intent(); intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT,getOutputMediaFileUri(MEDIA_TYPE_VIDEO)); //第二个参数为视频质量 intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1); startActivityForResult(intent,MEDIA_TYPE_VIDEO); } File file = null; private Uri getOutputMediaFileUri(int type) {
file = getOutputMediaFile(type); return Uri.fromFile(file); } private File getOutputMediaFile(int type) {
File file = null; String rootPath = null; switch (type) {
case MEDIA_TYPE_IMAGE: rootPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath(); file = new File(rootPath + File.separator + System.currentTimeMillis() + ".jpg"); break; case MEDIA_TYPE_VIDEO: rootPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getPath(); file = new File(rootPath + File.separator + System.currentTimeMillis() + ".mp4"); break; } return file; } }

 

音频录制

mediarecorder_state_diagram.gif

 

activity_main2.xml

MainActivity.java

package com.example.administrator.cameravideo; import android.app.Activity; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.Button; import java.io.File; import java.io.IOException; public class MainActivity2 extends Activity implements MediaRecorder.OnErrorListener {
private MediaRecorder mr; private boolean prepared = false; private Button button_record; private Button button_stop; @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); button_record = (Button) findViewById(R.id.button_record); button_stop = (Button) findViewById(R.id.button_stop); mr = new MediaRecorder(); init(); } private void init() {
mr.reset(); mr.setAudioSource(MediaRecorder.AudioSource.MIC); mr.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + File.separator + System.currentTimeMillis() + ".mp3"; mr.setOutputFile(path); try {
mr.prepare(); prepared = true; } catch (IOException e) {
e.printStackTrace(); } button_stop.setEnabled(false); } public void recordClick(View view) {
if (prepared) {
mr.start(); prepared = false; } button_stop.setEnabled(true); button_record.setEnabled(false); } public void stopClick(View view) {
mr.stop(); init(); button_stop.setEnabled(false); button_record.setEnabled(true); } @Override protected void onDestroy() {
super.onDestroy(); if (mr != null) {
mr.release(); } } @Override public void onError(MediaRecorder mr, int what, int extra) {
mr.reset(); } }

 

Vitamio框架

准备工作,下载Vitamio。在AS中添加依赖。必须加上以下权限和内容

 
 
在界面Avtivity设置布局前加上
Vitamio.isInitialized(getApplicationContext());

案例一:播放音频

activity_main.xml

MainActivity.java

package com.example.administrator.vitamio; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.view.View; import java.io.IOException; import io.vov.vitamio.MediaPlayer; import io.vov.vitamio.Vitamio; public class MainActivity extends Activity implements MediaPlayer.OnPreparedListener {
private MediaPlayer mediaplayer; @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //加载so类库(通常使用C。C++编写类库) /*if (!LibsChecker.checkVitamioLibs(this)) {
return ; } */ Vitamio.isInitialized(getApplicationContext()); setContentView(R.layout.activity_main); initMethodPlayer(); } private void initMethodPlayer() {
mediaplayer=new MediaPlayer(this); } public void play(View view) {
String path= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/a.mp3"; try {
mediaplayer.reset(); mediaplayer.setDataSource(path); mediaplayer.setOnPreparedListener(this); mediaplayer.prepareAsync(); } catch (IOException e) {
e.printStackTrace(); } } public void pause(View view) {
mediaplayer.pause(); } public void stop(View view) {
mediaplayer.stop(); mediaplayer.release(); } @Override public void onPrepared(MediaPlayer mp) {
mp.start(); } }

案例二:VideoView播放视频

activity_main2.xml

MainActivity2.java

package com.example.administrator.vitamio; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import io.vov.vitamio.Vitamio; import io.vov.vitamio.widget.MediaController; import io.vov.vitamio.widget.VideoView; public class MainActivity2 extends AppCompatActivity {
private VideoView vv; @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); Vitamio.isInitialized(getApplicationContext()); setContentView(R.layout.activity_main2); vv= (VideoView) findViewById(R.id.surface_view); MediaController mc=new MediaController(this); vv.setMediaController(mc); String path= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)+"/Nothing.ifox"; vv.setVideoPath(path); vv.requestFocus(); vv.start(); } }

转载于:https://my.oschina.net/xiaofeiandroid/blog/657743

你可能感兴趣的文章
db file sequential read等待事件
查看>>
CISCO路由器上使用 TCP intercept 防止DOS攻击
查看>>
ActiveMQ接收消息+发送消息的简单实例
查看>>
linux 日志清理
查看>>
CentOS系统启动流程
查看>>
Emacs之ditaa与PlantUML与dot绘图环境配置
查看>>
Android UI学习 - Tab的学习和使用
查看>>
Windows Server入门系列之十七 ARP协议原理
查看>>
利用Python脚本管理Windows服务
查看>>
初学flask_sqlalchemy
查看>>
SQLite 3.7.13的加密解密(一)—— 前言
查看>>
修正Strut2 自带上传拦截器功能
查看>>
LYNC安装故障排除分享
查看>>
jQuery做个TextBox自动完成条
查看>>
IP地址规划
查看>>
【ZooKeeper Notes 4】可视化zookeeper的事务日志
查看>>
Globalize for Ruby on Rails
查看>>
Linux计划任务
查看>>
4-2 ADO.NET-查询和检索数据9
查看>>
用简单的命令检查电脑是否被安装木马
查看>>