knightjsh 2022-05-01 10:55 采纳率: 100%
浏览 9
已结题

Android-4.4 实现录音功能报错

Layout布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
    <TextView
        android:text="录音"
        android:textSize="20sp"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/>
    <LinearLayout
        android:layout_width="fill_parent" android:layout_height="wrap_content">
        <Button
            android:id="@+id/startBtn1"
            android:text="开始" 
            android:layout_weight="1"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/stopBtn1"
            android:text="停止"
            android:enabled="false"
            android:layout_weight="1"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <SurfaceView
        android:id="@+id/view"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" />
</LinearLayout>
main文件:
package com.example.coremv;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.app.Activity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    MediaRecorder recorder1;//录音的
    Button startBtn1;
    Button stopBtn1;
    
    MediaRecorder recorder2; //录像的
    Button startBtn2;
    Button stopBtn2;
    
    SurfaceView view;//录像时的预览视图
    SurfaceHolder holder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);   
        initAudio();
    }   
    
    void initAudio(){
        recorder1 = new MediaRecorder();
        startBtn1 =(Button)findViewById(R.id.startBtn1);
        stopBtn1 =(Button)findViewById(R.id.stopBtn1);
        //音频源是麦克风
        recorder1.setAudioSource(MediaRecorder.AudioSource.MIC);//输出格式THREE_GPP
        recorder1.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//编码格式AMR_NB
        recorder1.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//输出文件路径
        recorder1.setOutputFile("/sdcard/"
        + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".3gp");
        
        startBtn1.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick (View v){
            try {
                recorder1.prepare();
                recorder1.start();
                startBtn1.setEnabled(false);
                stopBtn1.setEnabled(true);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    });
        
        stopBtn1.setOnClickListener (new OnClickListener() {
            @Override
            public void onClick (View v){
                try{
                    recorder1.stop();
                    recorder1.release();
                    startBtn1.setEnabled(true);
                    stopBtn1.setEnabled(false);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }
}
运行时闪退,模拟器报错 unfortunately ,coreMV has stopped。

使用的开发环境是adt-bundle-windows-x86-20131030,LogCat报错如下:

img

  • 写回答

1条回答 默认 最新

  • 不会写代码的猴子 Android领域优质创作者 2022-05-01 21:22
    关注

    你申请录音权限了吗?

    img

    报错原因看起来像是你没有申请麦克风权限

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月7日
  • 已采纳回答 8月30日
  • 创建了问题 5月1日

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装