我正在尝试创建一个应该首先记录用户语音,然后从录制的文件中计算不同内容(例如频谱)的应用程序。我无法播放录制的文件(我无法找到该文件 - 它是否记录了?)。这是我的代码:
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();
}
}
}
我知道缩进不正确,因为我严重粘贴了代码...你有什么想法为什么这段代码不起作用?它给出了两个错误:"应该已经设置了字幕控制器"但是我读到我并不需要介意这个问题,并且" QCMediaPlayer媒体播放器不存在"我读到这意味着我的平台不支持QCMediaPlayer ..? 此代码是否记录了一个文件而它无法播放或无法录制文件?如何录制用户语音并处理保存的文件?
答案 0 :(得分:1)
我认为问题出在playRecording()
。当您键入MediaPlayer mPlayer = ...
时,它会在该方法内创建一个带有范围的新变量。您没有使用班级MediaPlayer
变量。尝试改变
MediaPlayer mPlayer = MediaPlayer.create(this, Uri.parse(mFileName));
到
mPlayer = MediaPlayer.create(this, Uri.parse(mFileName));