Android实现调用系统相机录像及实现录音

简介: Android实现调用系统相机录像及实现录音

录像实现原理特别简单,先在配置文件中声明一下权限,这个就不说了,然后直接使用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();
    }
}

就这样。


目录
相关文章
|
3天前
|
Shell Android开发
Android系统 adb shell push/pull 禁止特定文件
Android系统 adb shell push/pull 禁止特定文件
16 1
|
1月前
|
搜索推荐 Android开发 iOS开发
安卓与iOS系统的用户界面设计对比分析
本文通过对安卓和iOS两大操作系统的用户界面设计进行对比分析,探讨它们在设计理念、交互方式、视觉风格等方面的差异及各自特点,旨在帮助读者更好地理解和评估不同系统的用户体验。
21 1
|
3天前
|
存储 Java Android开发
Android系统 设置第三方应用为默认Launcher实现和原理分析
Android系统 设置第三方应用为默认Launcher实现和原理分析
18 0
|
3天前
|
Android开发
Android构建系统:Android.mk(2)函数详解
Android构建系统:Android.mk(2)函数详解
12 1
|
3天前
|
存储 Java API
Android系统 文件访问权限笔记
Android系统 文件访问权限笔记
34 1
|
3天前
|
移动开发 Java Unix
Android系统 自动加载自定义JAR文件
Android系统 自动加载自定义JAR文件
21 1
|
3天前
|
Shell Android开发 开发者
Android系统 自定义动态修改init.custom.rc
Android系统 自定义动态修改init.custom.rc
22 0
|
3天前
|
测试技术 Android开发 开发者
RK3568 Android系统客制化动态替换ro任意属性
RK3568 Android系统客制化动态替换ro任意属性
23 1
|
3天前
|
存储 Linux Android开发
RK3568 Android/Linux 系统动态更换 U-Boot/Kernel Logo
RK3568 Android/Linux 系统动态更换 U-Boot/Kernel Logo
18 0
|
3天前
|
存储 缓存 安全
Android系统 应用存储路径与权限
Android系统 应用存储路径与权限
6 0
Android系统 应用存储路径与权限