简体   繁体   English

录制用户语音android

[英]recording user voice android

I am trying to make an application which should first record the user's voice and then calculate different things (eg spectrum) from the recorded file. 我正在尝试制作一个应用程序,该应用程序应首先记录用户的语音,然后从记录的文件中计算出不同的内容(例如频谱)。 I can't play the recorded file (I can't find that file - does it even record?). 我无法播放录制的文件(我找不到该文件-甚至可以录制吗?)。 Here is my code: 这是我的代码:

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();
    }

}


}

And I know the indents are not correct because I pasted the code badly... Do you have any ideas why this code doesn't work? 而且我知道缩进是不正确的,因为我粘贴的代码很糟糕...您是否知道为什么此代码不起作用? It gives two errors: "Should have subtitle controller already set" but I read that I don't need to mind that and "QCMediaPlayer mediaplayer NOT present" and I read that this means that my platform doesn't support QCMediaPlayer..? 它给出了两个错误:“应该已经设置了字幕控制器”,但是我读到我不需要介意,并且“ QCMediaPlayer媒体播放器不存在”,并且我读到这意味着我的平台不支持QCMediaPlayer。 Does this code record a file and it just can't play it or does it fail to record a file? 该代码是否记录了文件并且无法播放,还是记录失败? How can I record user voice and process the saved file? 如何录制用户声音并处理保存的文件?

I believe that problem is in playRecording() . 我相信问题在于playRecording() When you type MediaPlayer mPlayer = ... it creates a new variable with scope inside that method. 当您键入MediaPlayer mPlayer = ...它将在该方法内部创建一个具有范围的新变量。 You are not using your class MediaPlayer variable. 您没有使用MediaPlayer类变量。 Try to change 尝试改变

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM