录像实现原理特别简单,先在配置文件中声明一下权限,这个就不说了,然后直接使用Intent跳转就行。
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
startActivityForResult(intent, 10);
下面是录音的代码,直接上全部代码。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_voice"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/voice"
tools:context="bhne.com.wlprojectapplication.ui.VoiceActivity">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imageview_voice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:src="@drawable/timg_voice" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp">
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:background="@drawable/shape"
android:text="开始录音" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="48dp"
android:layout_marginRight="200dp"
android:background="@drawable/shape"
android:text="停止录音" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
上面是布局文件,下面是代码。
package bhne.com.wlprojectapplication.ui;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import bhne.com.wlprojectapplication.Common.DBUtil;
import bhne.com.wlprojectapplication.Common.GreenDaoContext;
import bhne.com.wlprojectapplication.Common.RecordPlayer;
import bhne.com.wlprojectapplication.R;
import bhne.com.wlprojectapplication.db.equDao;
import bhne.com.wlprojectapplication.db.pictureDao;
import bhne.com.wlprojectapplication.db.projectDao;
import bhne.com.wlprojectapplication.entity.equ;
import bhne.com.wlprojectapplication.entity.picture;
import bhne.com.wlprojectapplication.entity.project;
public class VoiceActivity extends Activity implements View.OnClickListener {
private Button start,stop,play,pause_play,stop_play;
private MediaRecorder mediaRecorder;
private File recordFile;
private RecordPlayer player;
private projectDao ProjectDao;
private equDao EquDao;
private String Equ_id;
private String projectpath;
private String pathImage;
private pictureDao PictureDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voice);
Bundle bundle = this.getIntent().getExtras();
/*获取Bundle中的数据,注意类型和key*/
Equ_id = bundle.getString("equid");
EquDao= DBUtil.getDaoSession(VoiceActivity.this).getEquDao();
ProjectDao= DBUtil.getDaoSession(VoiceActivity.this).getProjectDao();
PictureDao= DBUtil.getDaoSession(VoiceActivity.this).getPictureDao();
equ t1=EquDao.queryBuilder().where(equDao.Properties.Id.eq(Equ_id)).list().get(0);
project t2=ProjectDao.queryBuilder().where(projectDao.Properties.Id.eq(t1.gcid)).list().get(0);
projectpath=t2.getName();
GreenDaoContext ct=new GreenDaoContext();
String t=java.util.UUID.randomUUID().toString();
recordFile = new File(ct.getSDPath()+"/bhne/export/"+projectpath+"/", t+".wav");
pathImage=ct.getSDPath()+"/bhne/export/"+projectpath+"/"+t+".wav";
init();
Listener();
}
private void init(){
start=(Button) findViewById(R.id.start);
stop=(Button) findViewById(R.id.stop);
// play=(Button) findViewById(R.id.paly);
// pause_play=(Button) findViewById(R.id.pause_paly);
// stop_play=(Button) findViewById(R.id.stop_paly);
}
private void Listener(){
start.setOnClickListener(this);
stop.setOnClickListener(this);
// play.setOnClickListener(this);
// pause_play.setOnClickListener(this);
// stop_play.setOnClickListener(this);
}
@Override
public void onClick(View v) {
player=new RecordPlayer(VoiceActivity.this);
int Id=v.getId();
switch (Id) {
case R.id.start:
startRecording();
break;
case R.id.stop:
stopRecording();
break;
// case R.id.paly:
// playRecording();
// break;
// case R.id.pause_paly:
// pauseplayer();
// break;
// case R.id.stop_paly:
// stopplayer();
// break;
}
}
private void startRecording() {
mediaRecorder = new MediaRecorder();
// 判断,若当前文件已存在,则删除
if (recordFile.exists()) {
recordFile.delete();
}
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mediaRecorder.setOutputFile(recordFile.getAbsolutePath());
try {
Toast.makeText(this,"正在录音",Toast.LENGTH_SHORT).show();
// 准备好开始录音
mediaRecorder.prepare();
mediaRecorder.start();
start.setEnabled(false);
stop.setEnabled(true);
} catch (Exception e) {
e.printStackTrace();
}
}
private void stopRecording() {
stop.setEnabled(false);
start.setEnabled(true);
picture info=new picture();
// info.setId(java.util.UUID.randomUUID().toString());
info.setEquid(Long.valueOf(Equ_id));
info.setPicturePath(pathImage);
// info.setID(java.util.UUID.randomUUID().toString());
//加载默认的项目
final long len = PictureDao.insert(info);
if(len>0){
Toast.makeText(getApplicationContext(), "新增语音成功", Toast.LENGTH_SHORT).show();
Toast.makeText(this,recordFile.toString(),Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "新增语音失败", Toast.LENGTH_SHORT).show();
}
if (recordFile != null) {
mediaRecorder.stop();
mediaRecorder.release();
}
Intent intent=new Intent(VoiceActivity.this,PictureActivity.class);
setResult(70,intent);
finish();
}
}
就這樣,就这样。