无法在Android中播放使用AudioRecord录制的pcm文件

时间:2016-09-22 15:30:05

标签: android audio-recording android-audiorecord

我正在开发一款Android应用。在我的应用程序中,我需要使用AudionRecord录制音频并需要播放它。但是在播放录制的pcm文件时抛出错误。主要问题是我无法记录错误,因为我必须在真实设备上进行测试而无法在模拟器上运行。因为当我在模拟器上运行并记录时,我的应用程序崩溃了。但请看下面的代码有什么问题。

这是我的活动

public class MainActivity extends AppCompatActivity {

    Boolean recording;
    private Thread recordingThread = null;
    DataOutputStream dataOutputStream;
    short[] audioData;


    private Button btnPlay,btnStop,btnRecord;
    private String outputFile = null;
    //outputFile = Environment.getExternalStorageDirectory().getAbsolutePath()+"/recording.3gp";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();
        initViews();
        setUpViews();
    }

    private void initialize()
    {

    }

    private void initViews()
    {
        btnPlay = (Button)findViewById(R.id.btn_play);
        btnRecord = (Button)findViewById(R.id.btn_record);
        btnStop = (Button)findViewById(R.id.btn_stop);
    }

    private void setUpViews()
    {
        btnRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startRecord();
            }
        });

        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                recording = false;
                Toast.makeText(getBaseContext(),"Stopped",Toast.LENGTH_SHORT).show();
            }
        });

        btnPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                playRecord();
            }
        });
    }

    private void startRecord(){

        File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");

        try {
            file.createNewFile();

            OutputStream outputStream = new FileOutputStream(file);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
            dataOutputStream = new DataOutputStream(bufferedOutputStream);

            final int minBufferSize = AudioRecord.getMinBufferSize(11025,
                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT);

            audioData = new short[minBufferSize];

            final AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                    11025,
                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT,
                    minBufferSize);

            audioRecord.startRecording();

            Toast.makeText(getBaseContext(),"Recording",Toast.LENGTH_SHORT).show();


            recordingThread = new Thread(new Runnable() {
                public void run() {
                    while(recording){
                        int numberOfShort = audioRecord.read(audioData, 0, minBufferSize);
                        for(int i = 0; i < numberOfShort; i++){
                            try{
                                dataOutputStream.writeShort(audioData[i]);
                            }
                            catch (IOException e)
                            {
                                Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                }
            }, "AudioRecorder Thread");

            audioRecord.stop();
            dataOutputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void playRecord(){

        File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");

        int shortSizeInBytes = Short.SIZE/Byte.SIZE;

        int bufferSizeInBytes = (int)(file.length()/shortSizeInBytes);
        short[] audioData = new short[bufferSizeInBytes];

        try {
            InputStream inputStream = new FileInputStream(file);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
            DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);

            int i = 0;
            while(dataInputStream.available() > 0){
                audioData[i] = dataInputStream.readShort();
                i++;
            }

            dataInputStream.close();

            AudioTrack audioTrack = new AudioTrack(
                    AudioManager.STREAM_MUSIC,
                    11025,
                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT,
                    bufferSizeInBytes,
                    AudioTrack.MODE_STREAM);

            audioTrack.play();
            audioTrack.write(audioData, 0, bufferSizeInBytes);


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

问题在于播放录制的pcm。当我单击播放按钮时,应用程序抛出致命错误并停止。我也无法记录错误。 pcm文件也确实存在于定义的路径中。我的代码出了什么问题?为什么不能玩?

如下所示,录制后成功创建了pcm文件 enter image description here

1 个答案:

答案 0 :(得分:0)

如果没有看到Logcat,很难在代码中找出问题。看看这个有关如何使用AudioTrack播放pcm文件的工作代码&#39;:

$('.app-icon').click(function() {
  var index = $(this).index();
  $('.app-div').eq(index).toggle().siblings('.app-div').hide();
});

//animate.css effect
$('.app-div').addClass('animated fadeInUp');
来自http://jongladwin.blogspot.co.uk/2010/03/android-play-pcmwav-audio-buffer-using.html

代码