在Android Studio中使用AudioRecord和Visualizer进行录制时可视化

时间:2015-03-20 18:40:10

标签: java android android-studio audiorecord visualizer

我正在创建一个Android应用程序,它记录并保存一个wave文件并播放它。播放.wav文件时,我有一个可视化工具。但是,我希望应用程序在录制时可视化,以便用户获得反馈并知道录制正在运行。我尝试的所有技术要么导致程序崩溃,要么根本不起作用。任何帮助表示赞赏。

在下面的迭代中,当我使用recorder.getAudioSessionId()进行录制时尝试设置可视化工具时会出现问题。在“setupRecorderVisualizerFxAndUI()”方法中。当我按下录制按钮时,这会导致程序崩溃。使用mPlayer.getAudioSessionId()时,相同的技术在播放时完美运行;在setupPlaybackVisualizerFxAndUI()方法中。

以下是我的主要活动:

package beebros.androidaudiovisualizer;

import android.content.pm.PackageManager;
import android.graphics.Canvas;
import android.graphics.Color;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Environment;
import android.os.Parcel;
import android.support.annotation.NonNull;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import android.support.v7.app.ActionBarActivity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.audiofx.Visualizer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


import java.io.IOException;
import java.util.List;

public class MainActivity extends ActionBarActivity {

    VisualizerView mVisualizerView;

    private Visualizer mVisualizer;

    private static MediaRecorder mRecorder;
    private static MediaPlayer mPlayer;

    private static String audioFilePath;
    private static String audioFilePathTemp;
    private static Button stopButton;
    private static Button playButton;
    private static Button recordButton;

    private boolean isRecording = false;

    private static final int RECORDER_BPP = 16;
    private static final String AUDIO_RECORDER_FILE_EXT_WAV = ".wav";
    private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
    private static final String AUDIO_RECORDER_TEMP_FILE = "record_temp.raw";
    private static final int RECORDER_SAMPLERATE = 44100;
    private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_STEREO;
    private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;

    private AudioRecord recorder = null;
    private int bufferSize = 0;
    private Thread recordingThread = null;

    String filePlace;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mVisualizerView = (VisualizerView) findViewById(R.id.myvisualizerview); //reference to visualizer in xml

        //identifies location of SD card, where the recording will be stored
        //should probably check first if phone has an SDCard
        audioFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() +
                "myaudio.wav";//"/myaudio.3gp";

        //gets references to the buttons
        recordButton = (Button) findViewById(R.id.recordButton);
        playButton = (Button) findViewById(R.id.playButton);
        stopButton = (Button) findViewById(R.id.stopButton);

        //check if there is a microphone
        if (!hasMicrophone()) { //turns off buttons if no microphone
            stopButton.setEnabled(false);
            playButton.setEnabled(false);
            recordButton.setEnabled(false);
        } else {
            playButton.setEnabled(false);
            stopButton.setEnabled(false);
        }

        bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);
    }
    public void recordAudio(View view) throws IOException {

        isRecording = true;

        //enables/disables appropriate buttons
        stopButton.setEnabled(true);
        playButton.setEnabled(false);
        recordButton.setEnabled(false);

        recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
                RECORDER_SAMPLERATE, RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING, bufferSize);

        recorder.startRecording();

        isRecording = true;

        recordingThread = new Thread(new Runnable() {

            @Override
            public void run() {
                writeAudioDataToFile();
            }
        },"AudioRecorder Thread");

        recordingThread.start();

        setupRecorderVisualizerFxAndUI();//method that sets up the visualizer while recording and causes the crash
    try {
            mVisualizer.setEnabled(true); //enabling the visualizer is what actually makes it crash
                                            //calling setupRecorderVisualizerFxAndUI(); alone does not crash
        } catch (Exception e){
            Log.e(TAG,Log.getStackTraceString(e));
        }
    }

    public void stopAudio(View view) {

        playButton.setEnabled(true);

        if(null != recorder){
            isRecording = false;

            recorder.stop();
            recorder.release();

            recorder = null;
            recordingThread = null;
        }

        recordButton.setEnabled(true); //
        filePlace = getFilename();
        copyWaveFile(getTempFilename(),getFilename());//copies the latest recording to the permanent folder.
        deleteTempFile();

       mVisualizer.setEnabled(false);
    }

    //plays the audio and visuals
    public void playAudio(View view) throws IOException {

        //enables/diables appt. buttons
        playButton.setEnabled(false);
        recordButton.setEnabled(false);
        stopButton.setEnabled(true);

        String filepath = Environment.getExternalStorageDirectory().getPath();
        File file = new File(filepath,AUDIO_RECORDER_FOLDER);

        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        mPlayer = MediaPlayer.create(this, Uri.parse(file.getAbsolutePath() + "/" + "1426282058997" + AUDIO_RECORDER_FILE_EXT_WAV)); //plays back a specific file saved on my phone for testing

        setupPlaybackVisualizerFxAndUI();
        // Make sure the visualizer is enabled only when you actually want to
        // receive data, and when it makes sense to receive data.
        mVisualizer.setEnabled(true);
        // When the stream ends, we don't need to collect any more data. We
        // don't do this in setupVisualizerFxAndUI because we likely want to have more,
        // non-Visualizer related code in this callback.

        mPlayer
                .setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    public void onCompletion(MediaPlayer mediaPlayer) {
                        mVisualizer.setEnabled(false);
                    }
                });

        mPlayer.start();
    }

    //method to check for the presence of a microphone
    protected boolean hasMicrophone() {
        PackageManager pManager = this.getPackageManager();
        return pManager.hasSystemFeature(PackageManager.FEATURE_MICROPHONE);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (isFinishing() && mPlayer != null) { //d
            mVisualizer.release();
            mPlayer.release(); //d
            mPlayer = null; //d
        }
    }

    private void setupPlaybackVisualizerFxAndUI() {

        // Create the Visualizer object and attach it to our media player.
        mVisualizer = new Visualizer(mPlayer.getAudioSessionId());
        mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
        mVisualizer.setDataCaptureListener(
                new Visualizer.OnDataCaptureListener() {
                    public void onWaveFormDataCapture(Visualizer visualizer,
                                                      byte[] bytes, int samplingRate) {
                        mVisualizerView.updateVisualizer(bytes);
                    }

                    public void onFftDataCapture(Visualizer visualizer,
                                                 byte[] bytes, int samplingRate) {
                    }
                }, Visualizer.getMaxCaptureRate() / 2, true, false);
    }

    private void setupRecorderVisualizerFxAndUI() {

        // Create the Visualizer object and attach it to our media player.
        mVisualizer = new Visualizer(recorder.getAudioSessionId());//calling recorder.getAudioSessionId causes the application to crash.
        mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);

           mVisualizer.setDataCaptureListener(
                   new Visualizer.OnDataCaptureListener() {
                       public void onWaveFormDataCapture(Visualizer visualizer,
                                                         byte[] bytes, int samplingRate) {
                           mVisualizerView.updateVisualizer(bytes);
                       }

                       public void onFftDataCapture(Visualizer visualizer,
                                                    byte[] bytes, int samplingRate) {
                       }
                   }, Visualizer.getMaxCaptureRate() / 2, true, false);

    }

    private void writeAudioDataToFile(){
        byte data[] = new byte[bufferSize];
        String filename = getTempFilename();
        FileOutputStream os = null;

        try {
            os = new FileOutputStream(filename);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        int read = 0;

        if(null != os){
            while(isRecording){
                read = recorder.read(data, 0, bufferSize);

                if(AudioRecord.ERROR_INVALID_OPERATION != read){
                    try {
                        os.write(data);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private String getFilename(){
        String filepath = Environment.getExternalStorageDirectory().getPath();
        File file = new File(filepath,AUDIO_RECORDER_FOLDER);

        if(!file.exists()){
            file.mkdirs();
        }

        return (file.getAbsolutePath() + "/" + "myAudioFile" + AUDIO_RECORDER_FILE_EXT_WAV);
    }

    private String getTempFilename(){
        String filepath = Environment.getExternalStorageDirectory().getPath();
        File file = new File(filepath,AUDIO_RECORDER_FOLDER);

        if(!file.exists()){
            file.mkdirs();
        }

        File tempFile = new File(filepath,AUDIO_RECORDER_TEMP_FILE);

        if(tempFile.exists())
            tempFile.delete();

        return (file.getAbsolutePath() + "/" + AUDIO_RECORDER_TEMP_FILE);
    }

    private void deleteTempFile() {
        File file = new File(getTempFilename());

        file.delete();
    }

    private void copyWaveFile(String inFilename,String outFilename){
        FileInputStream in = null;
        FileOutputStream out = null;
        long totalAudioLen = 0;
        long totalDataLen = totalAudioLen + 36;
        long longSampleRate = RECORDER_SAMPLERATE;
        int channels = 2;
        long byteRate = RECORDER_BPP * RECORDER_SAMPLERATE * channels/8;

        byte[] data = new byte[bufferSize];

        try {
            in = new FileInputStream(inFilename);
            out = new FileOutputStream(outFilename);
            totalAudioLen = in.getChannel().size();
            totalDataLen = totalAudioLen + 36;

            //AppLog.logString("File size: " + totalDataLen);

            WriteWaveFileHeader(out, totalAudioLen, totalDataLen,
                    longSampleRate, channels, byteRate);

            while(in.read(data) != -1){
                out.write(data);
            }

            in.close();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void WriteWaveFileHeader(
            FileOutputStream out, long totalAudioLen,
            long totalDataLen, long longSampleRate, int channels,
            long byteRate) throws IOException {

        byte[] header = new byte[44];

        header[0] = 'R';  // RIFF/WAVE header
        header[1] = 'I';
        header[2] = 'F';
        header[3] = 'F';
        header[4] = (byte) (totalDataLen & 0xff);
        header[5] = (byte) ((totalDataLen >> 8) & 0xff);
        header[6] = (byte) ((totalDataLen >> 16) & 0xff);
        header[7] = (byte) ((totalDataLen >> 24) & 0xff);
        header[8] = 'W';
        header[9] = 'A';
        header[10] = 'V';
        header[11] = 'E';
        header[12] = 'f';  // 'fmt ' chunk
        header[13] = 'm';
        header[14] = 't';
        header[15] = ' ';
        header[16] = 16;  // 4 bytes: size of 'fmt ' chunk
        header[17] = 0;
        header[18] = 0;
        header[19] = 0;
        header[20] = 1;  // format = 1
        header[21] = 0;
        header[22] = (byte) channels;
        header[23] = 0;
        header[24] = (byte) (longSampleRate & 0xff);
        header[25] = (byte) ((longSampleRate >> 8) & 0xff);
        header[26] = (byte) ((longSampleRate >> 16) & 0xff);
        header[27] = (byte) ((longSampleRate >> 24) & 0xff);
        header[28] = (byte) (byteRate & 0xff);
        header[29] = (byte) ((byteRate >> 8) & 0xff);
        header[30] = (byte) ((byteRate >> 16) & 0xff);
        header[31] = (byte) ((byteRate >> 24) & 0xff);
        header[32] = (byte) (2 * 16 / 8);  // block align
        header[33] = 0;
        header[34] = RECORDER_BPP;  // bits per sample
        header[35] = 0;
        header[36] = 'd';
        header[37] = 'a';
        header[38] = 't';
        header[39] = 'a';
        header[40] = (byte) (totalAudioLen & 0xff);
        header[41] = (byte) ((totalAudioLen >> 8) & 0xff);
        header[42] = (byte) ((totalAudioLen >> 16) & 0xff);
        header[43] = (byte) ((totalAudioLen >> 24) & 0xff);

        out.write(header, 0, 44);
    }
}

我可以提供.xml和AndroidVisualizer.java opon请求的代码。

谢谢!

logcat的:

03-21 14:09:00.475    2351-2351/beebros.androidaudiovisualizer E/AudioEffect﹕ set(): AudioFlinger could not create effect, status: -22

03-21 14:09:00.475    2351-2351/beebros.androidaudiovisualizer E/visualizers-JNI﹕ Visualizer initCheck failed -4

03-21 14:09:00.475    2351-2351/beebros.androidaudiovisualizer E/Visualizer-JAVA﹕ Error code -4 when initializing Visualizer.

03-21 14:09:00.475    2351-2351/beebros.androidaudiovisualizer W/System.err﹕ java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -4

03-21 14:09:00.475    2351-2351/beebros.androidaudiovisualizer W/System.err﹕ at android.media.audiofx.Visualizer.<init>(Visualizer.java:217)

03-21 14:09:00.475    2351-2351/beebros.androidaudiovisualizer W/System.err﹕ at beebros.androidaudiovisualizer.MainActivity.setupRecorderVisualizerFxAndUI(MainActivity.java:233)

03-21 14:09:00.475    2351-2351/beebros.androidaudiovisualizer W/System.err﹕ at beebros.androidaudiovisualizer.MainActivity.recordAudio(MainActivity.java:132)

03-21 14:09:00.475    2351-2351/beebros.androidaudiovisualizer W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)

03-21 14:09:00.475    2351-2351/beebros.androidaudiovisualizer W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)

03-21 14:09:00.475    2351-2351/beebros.androidaudiovisualizer W/System.err﹕ at android.view.View$1.onClick(View.java:3978)

03-21 14:09:00.475    2351-2351/beebros.androidaudiovisualizer W/System.err﹕ at android.view.View.performClick(View.java:4654)

03-21 14:09:00.475    2351-2351/beebros.androidaudiovisualizer W/System.err﹕ at android.view.View$PerformClick.run(View.java:19438)

03-21 14:09:00.475    2351-2351/beebros.androidaudiovisualizer W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)

03-21 14:09:00.475    2351-2351/beebros.androidaudiovisualizer W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)

03-21 14:09:00.485    2351-2351/beebros.androidaudiovisualizer W/System.err﹕ at android.os.Looper.loop(Looper.java:146)

03-21 14:09:00.485    2351-2351/beebros.androidaudiovisualizer W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5487)

03-21 14:09:00.485    2351-2351/beebros.androidaudiovisualizer W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)

03-21 14:09:00.485    2351-2351/beebros.androidaudiovisualizer W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)

03-21 14:09:00.485    2351-2351/beebros.androidaudiovisualizer W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)

03-21 14:09:00.485    2351-2351/beebros.androidaudiovisualizer W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)

03-21 14:09:00.485    2351-2351/beebros.androidaudiovisualizer W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

03-21 14:09:00.485    2351-2351/beebros.androidaudiovisualizer E/Messed Up﹕ java.lang.NullPointerException
            at beebros.androidaudiovisualizer.MainActivity.recordAudio(MainActivity.java:134)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3978)
            at android.view.View.performClick(View.java:4654)
            at android.view.View$PerformClick.run(View.java:19438)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5487)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案