录制用户语音 android

recording user voice android

我正在尝试制作一个应用程序,它应该首先记录用户的声音,然后从记录的文件中计算出不同的东西(例如频谱)。我无法播放录制的文件(我找不到该文件 - 它甚至可以录制吗?)。这是我的代码:

package com.example.annik.puhesovellus;

import android.media.MediaPlayer; 
import android.media.MediaRecorder;
import android.net.Uri;
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 java.io.File;
import java.io.IOException;


public class DisplayAanitysIkkuna extends AppCompatActivity {
private String mFileName;
private MediaRecorder mRecorder;
private MediaPlayer mPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_aanitys_ikkuna);
    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();
        }
    });
    mFileName = Environment.getExternalStorageDirectory()+"/audiorecorder.3gpp";

}
public void ButtonTapped(View view) {
    switch(view.getId()) {
        case R.id.start:
            try {
                beginRecording();
            }catch(Exception e){
                e.printStackTrace();
            }
            break;
        case R.id.stop:
            try {
                stopRecording();
            }catch(Exception e) {
                e.printStackTrace();
            }
            break;
        case R.id.play:
            try {
                playRecording();
            }catch(Exception e) {
                e.printStackTrace();
            }
            break;
        case R.id.stopPlaying:
            try {
                stopPlaying();
            }catch(Exception e) {
                e.printStackTrace();
            }
            break;

    }

}

private void stopPlaying() {
    mPlayer.release();
    mPlayer = null;
}

private void playRecording() throws Exception{
    ditchMediaPlayer();
    MediaPlayer mPlayer = MediaPlayer.create(this, Uri.parse(mFileName));
    mPlayer.start();

}

private void ditchMediaPlayer() {
    if(mPlayer != null) {
        try {
            mPlayer.release();
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}

private void beginRecording() throws IOException {
    ditchMediaRecorder();
    File outFile = new File(mFileName);

    if(outFile.exists()) {
        outFile.delete();
    }

    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    mRecorder.setOutputFile(mFileName);
    mRecorder.prepare();
    mRecorder.start();
}

private void ditchMediaRecorder() {
    if(mRecorder != null) {
        mRecorder.release();

    }
}

private void stopRecording() {
    if(mRecorder != null) {
        mRecorder.stop();
    }

}


}

而且我知道缩进不正确,因为我粘贴的代码很糟糕...您知道为什么这段代码不起作用吗?它给出了两个错误:"Should have subtitle controller already set" 但我读到我不需要介意和 "QCMediaPlayer mediaplayer NOT present" 并且我读到这意味着我的平台不支持 QCMediaPlayer..? 这段代码是否录制了一个文件,但它无法播放,还是无法录制文件?如何录制用户语音并处理保存的文件?

我认为问题出在 playRecording()。当您键入 MediaPlayer mPlayer = ... 时,它会在该方法内创建一个具有作用域的新变量。您没有使用 class MediaPlayer 变量。尝试改变

MediaPlayer mPlayer = MediaPlayer.create(this, Uri.parse(mFileName));

mPlayer = MediaPlayer.create(this, Uri.parse(mFileName));