我想創建一個按鈕,允許我通過服務錄製音頻,我希望按鈕有文字:"Start Recording"
。在OnClick
事件中,我希望按鈕文本更改爲:"Stop Recording"
。((按鈕)視圖)裏面的服務不工作
我有這段代碼工作,當它在一個類,但現在它不是在一個服務工作,但是,因爲我想音頻記錄作爲服務工作,我似乎無法得到按鈕的文本更改。我很新的編碼,所以任何幫助將不勝感激!
我的代碼如下:
類別:
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);
}
}
服務:
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();
}
}
活動:
<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>
如何在我現有的代碼中鏈接它?我在哪裏寫runOnUiThread方法,因爲我不能把它放在我的布爾值中,也不能用它創建一個新的方法? @hoanngothanh – Viverson
@Viverson像Xema評論,嘗試使用otto庫 – hoanngothanh