录制Android应用程序

时间:2016-01-13 12:38:03

标签: java android audio-recording microphone

基本上,我正在努力使应用程序能够使用麦克风录制内容并保存文件中记录的内容。我已经按照我发现的教程进行了操作,到目前为止,我有这段代码:

package com.nicu.bogdan.vrec;

import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

private static MediaRecorder mediaRecorder;
private static MediaPlayer mediaPlayer;
Button record;
Button stop;
Button play;
static String audioFilePath;
boolean isRecording;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    record = (Button) findViewById(R.id.record);
    stop = (Button) findViewById(R.id.stop);
    play = (Button) findViewById(R.id.play);

    if (!hasMicrophone())
    {
        stop.setEnabled(false);
        play.setEnabled(false);
        record.setEnabled(false);
    } else {
        play.setEnabled(false);
        stop.setEnabled(false);
    }



}

public boolean hasMicrophone() {
    PackageManager pmanager = this.getPackageManager();
    return pmanager.hasSystemFeature(
            PackageManager.FEATURE_MICROPHONE);
}

public void record (View view) throws IOException
{

    audioFilePath =  Environment.getExternalStorageDirectory().getAbsolutePath() + "/"
            + DateFormat.getDateTimeInstance().format(new Date()) + ".3gp";
    isRecording = true;
    stop.setEnabled(true);
    play.setEnabled(false);
    record.setEnabled(false);


        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); //the error comes from this line
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setOutputFile(audioFilePath);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mediaRecorder.prepare();


    mediaRecorder.start();
}

public void stop (View view)
{

    stop.setEnabled(false);
    play.setEnabled(true);
    if (isRecording)
    {
        record.setEnabled(false);
        mediaRecorder.stop();
        mediaRecorder.release();
        mediaRecorder = null;
        isRecording = false;
    } else {
        mediaPlayer.release();
        mediaPlayer = null;
        record.setEnabled(true);
    }
}

public void play (View view) throws IOException
{
    play.setEnabled(false);
    record.setEnabled(false);
    stop.setEnabled(true);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setDataSource(audioFilePath);
    mediaPlayer.prepare();
    mediaPlayer.start();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

这是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nicu.bogdan.vrec" >

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

布局非常简单,3个按钮:录制,停止和播放。我不会发布该代码,因为它并不重要。

点击&#34;记录&#34;按钮,我收到此错误:Caused by: java.lang.RuntimeException: setAudioSource failed.来自我上面评论的行。现在的问题是,只有当我从Nexus 5运行应用程序时才能获得此功能,当我从Genymotion模拟器运行应用程序时,我不会收到此错误。

1 个答案:

答案 0 :(得分:0)

您需要添加

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

到您的清单,因为访问音频源正在修改音频设置。