I'm trying to create a button that allows me to record audio through a service, I want the button to have the text: "Start Recording" . In the OnClick event OnClick I want the button text to change to: "Stop Recording" .
I had this code working when it was in the class, but now it doesnβt work in the service, however, since I want the sound recording to work as a service, I canβt get the button text to change. I am new to coding, so any help would be greatly appreciated!
My code is as follows:
Grade:
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); } }
Services:
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) {
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>
source share