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?
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));
Related
I have successfully recorded the sound from microphone and can draw recorded sound in wav form. What I need is to draw graph dynamically when I record the sound (Like heart beat monitoring graph). Below is my code to record sound
package com.example.hp.audiorecording2;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
public class RecordActivity extends Activity {
Button buttonRecord;
Button buttonPlay;
String mFileName;
MediaRecorder mediaRecorder;
private MediaPlayer mPlayer = null;
private static final String LOG_TAG = "AudioRecordTest";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record);
buttonRecord = (Button) findViewById(R.id.buttonRecord);
buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonRecord.setOnClickListener(new View.OnClickListener() {
boolean mStartRecording = true;
#Override
public void onClick(View v) {
onRecord(mStartRecording);
if (mStartRecording) {
buttonRecord.setText("Stop");
} else {
buttonRecord.setText("Start");
}
mStartRecording = !mStartRecording;
}
});
buttonPlay.setOnClickListener(new View.OnClickListener() {
boolean mStartPlaying = true;
#Override
public void onClick(View v) {
onPlay(mStartPlaying);
if (mStartPlaying) {
buttonPlay.setText("Stop playing");
} else {
buttonPlay.setText("Start playing");
}
mStartPlaying = !mStartPlaying;
}
});
}
private void onRecord(boolean start) {
if (start) {
startRecording();
} else {
stopRecording();
}
}
private void onPlay(boolean start) {
if (start) {
startPlaying();
} else {
stopPlaying();
}
}
private void startRecording() {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setOutputFile(mFileName);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaRecorder.start();
}
private void stopRecording() {
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
}
private void startPlaying() {
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mFileName);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
private void stopPlaying() {
mPlayer.release();
mPlayer = null;
}
public RecordActivity() {
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.3gp";
}
}
you could use getMaxAmplitude as explained in link
I know how to record an audio file using MediaRecorder.
I want to upload the audio file on server, but before that, I want to compress it.
NOTE: I am recording audio in .3gpp format.
This is my class:
package com.iotaconcepts.task3;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import java.io.File;
import java.io.IOException;
public class MainActivity extends AppCompatActivity
{
MediaPlayer mediaPlayer;
MediaRecorder recorder;
String OUTPUT_FILE;
Button beginRecording, stopRecording, playRecording, stopPlayback;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OUTPUT_FILE = Environment.getExternalStorageDirectory() + "/audiorecorder.3gpp";
beginRecording = (Button)findViewById(R.id.bt_begin);
stopRecording = (Button)findViewById(R.id.bt_stop_recording);
playRecording = (Button)findViewById(R.id.bt_play_recording);
stopPlayback = (Button)findViewById(R.id.bt_stop_playback);
beginRecording.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
beginRecording();
} catch (IOException e) {
e.printStackTrace();
}
}
});
stopRecording.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopRecording();
}
});
playRecording.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
playRecording();
} catch (IOException e) {
e.printStackTrace();
}
}
});
stopPlayback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlayback();
}
});
}
public void beginRecording() throws IOException {
// If media recorder is working ... stop it
ditchMediaRecorder();
File outFile = new File(OUTPUT_FILE);
if(outFile.exists())
outFile.delete();
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(OUTPUT_FILE);
recorder.prepare();
recorder.start();
}
public void stopRecording()
{
if (recorder != null)
recorder.stop();
}
public void playRecording() throws IOException
{
ditchMediaPlayer();
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(OUTPUT_FILE);
mediaPlayer.prepare();
mediaPlayer.start();
}
public void stopPlayback()
{
if (mediaPlayer != null)
{
mediaPlayer.stop();
}
}
public void ditchMediaRecorder()
{
if(recorder!=null)
{
recorder.release();
}
}
public void ditchMediaPlayer()
{
if (mediaPlayer != null)
{
mediaPlayer.release();
}
}
}
I have already included the required permissions in manifest.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
If you are looking for fast lossless compression have a look at LZ4: https://github.com/jpountz/lz4-java. It provides two compression methods: fast scan (LZ4) and high compression (LZ4 HC). The jar file can be found here: http://java2s.com/Code/Jar/l/Downloadlz4120jar.htm
I've been having an issue with using AudioRecord for Android. I've read as much as I can find online about it, but I cannot seem to get a good initialization.
My code to create an AudioRecord object is like this:
int bufferSize = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
if (bufferSize < 4096)
bufferSize = 4096;
mBuffer = new short[bufferSize];
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, 44100, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize);
and I had acquired the correct permission:
but whenever I run the app, it will show this error:
08-12 11:56:05.669: E/AudioRecord(10689): Could not get audio input
for record source 1
08-12 11:56:05.669: E/AudioRecord-JNI(10689):
Error creating AudioRecord instance: initialization check failed.
08-12 11:56:05.669: E/AudioRecord-Java(10689): [
android.media.AudioRecord ] Error code -20 when initializing native
AudioRecord object.
So here is what you have to do to record audio files in android.
First you have to add some permissions to your Androidmanifest.xml file. The application needs to have the permission to write to external storage Audio recording for android
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Next you have to import the required classes and functions
import android.app.Activity;
import android.widget.LinearLayout;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.util.Log;
import android.media.MediaRecorder;
import android.media.MediaPlayer;
import java.io.IOException;
Create an instance of the Android.media.MediaRecorder.
private void startRecording() {
mRecorder = new MediaRecorder();
Next you have to set the source for the audio input, in most of the cases you would be using the MIC of your android device to give audio input to your application for recording audio files in android. In this case you would have to set audio source to : MediaRecorder.AudioSource.MIC
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
Next you have to define the output format for your recorder file. Android supports a variety for formats for audio. For this you have to use MediaRecorder.SetOutputFormat(); function.
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
Now you have to name a file which will hold your audio recording in your specified format. For that you have to use MediaRecorder.SetOutputfile() function.
mRecorder.setOutputFile(mFileName);
Now set the audio encoder using MediaRecorder.setAudioEncoder() function.
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
To preapre your application for recording the audio input call MediaRecorder.prepare()
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
Now everything is set up and ready, all you have to do is initiate the audio capturing process. For that you will have to do this: MediaRecorder.start();
class RecordButton extends Button {
boolean mStartRecording = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onRecord(mStartRecording);
if (mStartRecording) {
setText("Stop recording");
} else {
setText("Start recording");
}
mStartRecording = !mStartRecording;
}
};
public RecordButton(Context ctx) {
super(ctx);
setText("Start recording");
setOnClickListener(clicker);
}
}
To stop the recording, call MediaRecorder.stop()
At the end, when you are done with recording you have to release the resources byt calling MediaRecorder.release()
mPlayer.release();
Below is an example of application that records an audio and then plays it back.
package com.android.audiorecordtest;
import android.app.Activity;
import android.widget.LinearLayout;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.util.Log;
import android.media.MediaRecorder;
import android.media.MediaPlayer;
import java.io.IOException;
public class AudioRecordTest extends Activity
{
private static final String LOG_TAG = "AudioRecordTest";
private static String mFileName = null;
private RecordButton mRecordButton = null;
private MediaRecorder mRecorder = null;
private PlayButton mPlayButton = null;
private MediaPlayer mPlayer = null;
private void onRecord(boolean start) {
if (start) {
startRecording();
} else {
stopRecording();
}
}
private void onPlay(boolean start) {
if (start) {
startPlaying();
} else {
stopPlaying();
}
}
private void startPlaying() {
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mFileName);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
private void stopPlaying() {
mPlayer.release();
mPlayer = null;
}
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
}
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
class RecordButton extends Button {
boolean mStartRecording = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onRecord(mStartRecording);
if (mStartRecording) {
setText("Stop recording");
} else {
setText("Start recording");
}
mStartRecording = !mStartRecording;
}
};
public RecordButton(Context ctx) {
super(ctx);
setText("Start recording");
setOnClickListener(clicker);
}
}
class PlayButton extends Button {
boolean mStartPlaying = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onPlay(mStartPlaying);
if (mStartPlaying) {
setText("Stop playing");
} else {
setText("Start playing");
}
mStartPlaying = !mStartPlaying;
}
};
public PlayButton(Context ctx) {
super(ctx);
setText("Start playing");
setOnClickListener(clicker);
}
}
public AudioRecordTest() {
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.3gp";
}
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout ll = new LinearLayout(this);
mRecordButton = new RecordButton(this);
ll.addView(mRecordButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
mPlayButton = new PlayButton(this);
ll.addView(mPlayButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
setContentView(ll);
}
#Override
public void onPause() {
super.onPause();
if (mRecorder != null) {
mRecorder.release();
mRecorder = null;
}
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
}
or use this amazing tutorial
I am trying to make a video player in android.
It plays the 3GP format videos.
But it does not support the mp4 video.Below is my code in android for the same.Why does it not support the mp4 format on the device and emulator?
package com.example.videoplayer;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.URLUtil;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.VideoView;
public class VideoViewDemo extends Activity {
private static final String TAG = "VideoViewDemo";
private VideoView mVideoView;
private EditText mPath;
private ImageButton mPlay;
private ImageButton mPause;
private ImageButton mReset;
private ImageButton mStop;
private String current;
#SuppressLint({ "NewApi", "NewApi", "NewApi" })
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mPath = (EditText) findViewById(R.id.path);
mPath.setText("ooklnet.com/files/368/368007/video.mp4");
mPlay = (ImageButton) findViewById(R.id.play);
mPause = (ImageButton) findViewById(R.id.pause);
mReset = (ImageButton) findViewById(R.id.reset);
mStop = (ImageButton) findViewById(R.id.stop);
mPlay.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
playVideo();
}
});
mPause.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.pause();
}
}
});
mReset.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
mVideoView.seekTo(0);
}
}
});
mStop.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mVideoView != null) {
current = null;
mVideoView.stopPlayback();
}
}
});
/*runOnUiThread(new Runnable(){
public void run() {
sleep(2000);
playVideo();
}
});*/
Thread _trd1 = new Thread() {
public void run() {
try {
sleep(2000);
runOnUiThread(new Runnable() {
public void run() {
playVideo();
}
});
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
_trd1.start();
// new DoBackgroundTask().execute();
}
public class DoBackgroundTask extends AsyncTask
<String, Void, String> {
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
}
protected String doInBackground(String... locationNames) {
playVideo();
return null;
}
protected void onPostExecute(String addresses){
}
}
private void playVideo() {
try {
final String path = mPath.getText().toString();
Log.v(TAG, "path: " + path);
if (path == null || path.length() == 0) {
Toast.makeText(VideoViewDemo.this, "File URL/path is empty",
Toast.LENGTH_LONG).show();
} else {
// If the path has not changed, just start the media player
if (path.equals(current) && mVideoView != null) {
mVideoView.start();
mVideoView.requestFocus();
return;
}
current = path;
mVideoView.setVideoPath(getDataSource("ooklnet.com/files/368/368007/video.mp4"));
mVideoView.start();
mVideoView.requestFocus();
}
} catch (Exception e) {
Log.e(TAG, "error: " + e.getMessage(), e);
if (mVideoView != null) {
mVideoView.stopPlayback();
}
}
}
private String getDataSource(String path) throws IOException {
if (!URLUtil.isNetworkUrl(path)) {
return path;
} else {
URL url = new URL(path);
URLConnection cn = url.openConnection();
cn.connect();
InputStream stream = cn.getInputStream();
if (stream == null)
throw new RuntimeException("stream is null");
File temp = File.createTempFile("mediaplayertmp", "dat");
temp.deleteOnExit();
String tempPath = temp.getAbsolutePath();
FileOutputStream out = new FileOutputStream(temp);
byte buf[] = new byte[128];
do {
int numread = stream.read(buf);
if (numread <= 0)
break;
out.write(buf, 0, numread);
} while (true);
try {
stream.close();
} catch (IOException ex) {
Log.e(TAG, "error: " + ex.getMessage(), ex);
}
return tempPath;
}
}
}
Please advise as soon as possible.
Thanks.
The problem might be with the video encoding. Android Froyo and Gingerbread doesn't support H264 formats other than "Baseline" H264. So if your video is Mp4 & H264 encoded make sure its "AVC baseline" encoded. Use some tools like "Media info" in windows/Linux and check your video encoding. Convert the video to Baseline if possible.
An alternative workaround is to skip the Videoview and use a video play intent and redirect the playback to an app. User will be prompted to pick a player to handle the playback. Obviously if the video view cant play the file, the default player also wont be able to handle the file. you can choose some other installed player like Mx-Player which will stream the file perfectly.
Hope that solved your issue.
Try correct video url with protocol: http://www.ooklnet.com/files/368/368007/video.mp4
package com.Project_recording;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Project_recordingActivity extends Activity {
private static final String APP_TAG = "com.hascode.android.soundrecorder";
private MediaRecorder recorder = new MediaRecorder();
private MediaPlayer player = new MediaPlayer();
private Button btRecord;
private Button btPlay;
private TextView resultView;
private boolean recording = false;
private boolean playing = false;
private File outfile = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
resultView = (TextView) findViewById(R.id.output);
try {
// the soundfile
File storageDir = new File(Environment
.getExternalStorageDirectory(), "com.hascode.recorder");
storageDir.mkdir();
Log.d(APP_TAG, "Storage directory set to " + storageDir);
outfile = File.createTempFile("hascode", ".3gp", storageDir);
// init recorder
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(outfile.getAbsolutePath());
// init player
player.setDataSource(outfile.getAbsolutePath());
} catch (IOException e) {
Log.w(APP_TAG, "File not accessible ", e);
} catch (IllegalArgumentException e) {
Log.w(APP_TAG, "Illegal argument ", e);
} catch (IllegalStateException e) {
Log.w(APP_TAG, "Illegal state, call reset/restore", e);
}
btRecord = (Button) findViewById(R.id.btRecord);
btRecord.setOnClickListener(handleRecordClick);
btPlay = (Button) findViewById(R.id.btPlay);
btPlay.setOnClickListener(handlePlayClick);
}
private final OnClickListener handleRecordClick = new OnClickListener() {
#Override
public void onClick(View view) {
if (!recording) {
startRecord();
} else {
stopRecord();
}
}
};
private final OnClickListener handlePlayClick = new OnClickListener() {
#Override
public void onClick(View view) {
if (!playing) {
startPlay();
} else {
stopPlay();
}
}
};
private void startRecord() {
Log.d(APP_TAG, "start recording..");
printResult("start recording..");
try {
recorder.prepare();
recorder.start();
recording = true;
} catch (IllegalStateException e) {
Log
.w(APP_TAG,
"Invalid recorder state .. reset/release should have been called");
} catch (IOException e) {
Log.w(APP_TAG, "Could not write to sd card");
}
}
private void stopRecord() {
Log.d(APP_TAG, "stop recording..");
printResult("stop recording..");
recorder.stop();
recorder.reset();
recorder.release();
recording = false;
}
private void startPlay() {
Log.d(APP_TAG, "starting playback..");
printResult("start playing..");
try {
playing = true;
player.prepare();
player.start();
} catch (IllegalStateException e) {
Log.w(APP_TAG, "illegal state .. player should be reset");
} catch (IOException e) {
Log.w(APP_TAG, "Could not write to sd card");
}
}
private void stopPlay() {
Log.d(APP_TAG, "stopping playback..");
printResult("stop playing..");
player.stop();
player.reset();
player.release();
playing = false;
}
private void printResult(String result) {
resultView.setText(result);
}
}
When I press the record button, Is starts recording. When I press the play button, It starting playing. When I again press the play button, I stops playing. The important issue which I am facing is the sound is not heared..? Please do me a needful. I am new to android..
Why you are resetting the recorder object immediately after recording .