我正在尝试创建一个按钮,允许我通过服务录制音频,我希望按钮有文字:"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>
答案 0 :(得分:1)
您想要在服务中调用按钮,尝试使用BroadcastReceiver
答案 1 :(得分:0)
在您的活动中将您的按钮声明为静态,例如
Disabled
然后在你想要的服务中做这样的事情
public static Button recordButton;
public void onCreate(Bundle savedInstanceState) {
....
recordButton =(Button) findViewById(R.id.RecordButton);
....
}