Reputation: 3
I am trying to make an application which should first record the user's voice and then calculate different things (e.g. 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..? 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?
Upvotes: 0
Views: 140
Reputation: 66
I believe that problem is in playRecording()
. When you type MediaPlayer mPlayer = ...
it creates a new variable with scope inside that method. You are not using your class MediaPlayer
variable. Try to change
MediaPlayer mPlayer = MediaPlayer.create(this, Uri.parse(mFileName));
to
mPlayer = MediaPlayer.create(this, Uri.parse(mFileName));
Upvotes: 1