gpt4 book ai didi

java - 服务内的((按钮) View )不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:14:55 26 4
gpt4 key购买 nike

我正在尝试创建一个允许我通过服务录制音频的按钮,我希望该按钮具有文本:“开始录制”。在 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();

}


}

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>

最佳答案

这确实不能在这里工作,因为您的((按钮) View )没有在您的 onCreate() 中实例化。

我的建议是从服务中调用 Activity 中的一个函数来执行您想要执行的操作(此处,更改文本)。您可以使用 BroadcastReceiver 实现此目的。

为此,您可以执行以下操作:

在你的 Activity 中:

@Override
public void onResume() {
super.onResume();

// Register mMessageReceiver to receive messages.
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("change-text"));
}

// handler for received Intents for the "change-text" event
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Extract data included in the Intent
String message = intent.getStringExtra("message");
button.setText(message); //Assuming you have instantiate properly your button inside your onCreate()
}
};

@Override
protected void onPause() {
// Unregister since the activity is not visible
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onPause();
}

在你的服务中:

// Send an Intent with an action named "change-text". 
private void sendMessage(boolean startRecording) {
Intent intent = new Intent("change-text");
// add data
if (startRecording) intent.putExtra("message", "Start Recording");
else intent.putExtra("message","Stop Recording");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

要从您的服务更改文本按钮,您只需调用 sendMessage 函数,并根据您想要执行的操作将 true 或 false 作为参数!

您可以在此处查看我根据您的情况改编的代码源: http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html#ownreceiver_localbroadcastmanager

关于java - 服务内的((按钮) View )不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37185572/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com