I am trying to create a button that allows me to record audio through a service, i want the button to have text: "Start Recording"
. On the OnClick
event, i want the button text to change to: "Stop Recording"
.
I had this code working when it was in a class but now it is not working in a service, however, as I want the audio record to work as a service, I cannot seem to get the button's text to change. I am pretty new to coding so any help will be greatly appreciated!
My code is as follows:
Class:
public class Test extends AppCompatActivity {
public void Record(View view) {
Intent intent = new Intent(this, RecordService.class);
startService(intent);
}
public void Play(View view) {
Intent intent = new Intent(this, RecordService.class);
stopService(intent);
}
}
Service:
import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.widget.Button;
import android.view.View;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class RecordService extends Service {
MediaRecorder mRecorder;
public static String audioFilePath;
public boolean isRecording = false;
public RecordService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
public void onCreate () {
if(mRecorder == null){
SimpleDateFormat s = new SimpleDateFormat("ddMMyyyy_hhmmss");
String format = s.format(new Date());
audioFilePath = Environment.getExternalStorageDirectory().
getAbsolutePath() + "/" + format + ".3gpp";
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(audioFilePath);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
}
if (isRecording) {
try{
stopRecording();
isRecording = false;
((Button)view).setText("Start Recording");
}catch(Exception e){
e.printStackTrace();
}
} else {
try{
startRecording();
isRecording = true;
((Button)view).setText("Stop Recording");
}catch(Exception e){
e.printStackTrace();
}
}
}
public void startRecording() throws IllegalStateException, IOException{
mRecorder.prepare();
mRecorder.start();
}
public void stopRecording() throws IllegalStateException, IOException{
mRecorder.stop();
mRecorder.release();
}
public void onStartCommand()
{
SimpleDateFormat s = new SimpleDateFormat("ddMMyyyy_hhmmss");
String format = s.format(new Date());
audioFilePath = Environment.getExternalStorageDirectory().
getAbsolutePath() + "/" + format + ".3gpp";
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(audioFilePath);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mRecorder.start();
}
public void onDestroy()
{
super.onDestroy();
mRecorder.stop();
mRecorder.release();
}
}
Activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="kyr.com.knowyourrights.Test">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="Record"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RecordButton"
android:layout_alignParentTop="true"
android:onClick="Record">
</Button>
</RelativeLayout>