我是Android开发的新手,我想制作一个录音机,当我想从我的主要活动中访问开始录制的方法时,它总是会出错。以下是我的代码。我希望你能帮助我:
这是mainActivity:
public class MainActivity extends AppCompatActivity {
Boolean isRecording = false;
Record record;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
record = new Record();
}
public void recordAudio(View view){
if(!isRecording)
{
isRecording = true;
record.startRecording();
}
else{
isRecording = false;
record.stopRecording();
}
}
以下是子类:
public class Record extends MainActivity {
public void startRecording() {
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, getBufferSize());
int i = recorder.getState();
if (i == 1)
recorder.startRecording();
isRecording = true;
recordingThread = new Thread(new Runnable() {
@Override
public void run() {
writeAudioDataToFile();
}
}, "AudioRecorder Thread");
recordingThread.start();
buttonRecord.setText(R.string.button_stop_record);
}
感谢您的帮助!
以下是确切的错误代码:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com, PID: 31778
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:6199)
at android.widget.TextView.performClick(TextView.java:11090)
at android.view.View$PerformClick.run(View.java:23647)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6199)
at android.widget.TextView.performClick(TextView.java:11090)
at android.view.View$PerformClick.run(View.java:23647)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.IllegalArgumentException: Invalid audio buffer size.
at android.media.AudioRecord.audioBuffSizeCheck(AudioRecord.java:751)
at android.media.AudioRecord.<init>(AudioRecord.java:385)
at android.media.AudioRecord.<init>(AudioRecord.java:289)
at com.Record.startRecording(Record.java:63)
at com.MainActivity.recordAudio(MainActivity.java:35)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6199)
at android.widget.TextView.performClick(TextView.java:11090)
at android.view.View$PerformClick.run(View.java:23647)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
答案 0 :(得分:1)
错误说com.Record.startRecording(Record.java:63)
导致:java.lang.IllegalArgumentException: Invalid audio buffer size.
请在实例化新的AudioRecord
之前调用AudioRecord.getMinBufferSize(int sampleRateInHz, int channelConfig, int audioFormat)
以确保缓冲区大小足够大。就像它在documentation中所说的那样。
请注意,您还应该通过调用getState()
检查您的AudioRecord
实例是否已正确初始化(并检查它是否返回STATE_INITIALIZED
)。
答案 1 :(得分:1)
在Android Activity
中通常会被用户代码盯着,但对象创建不是用户的责任,而是Android框架执行此操作。
您的案例有Record extends MainActivity
,而MainActivity是一项活动,记录也是一项活动。因此,您需要启动它,使其成为清单文件中的启动器主要活动,或使用startActivity()
(或startActivityForResult()
)。
代码record = new Record();
在这里,您也可以在父类Record
中自己创建MainActivity
的实例。从Android和Java面向对象的角度来看,这不是一个好主意。 (因此你可以但不应该选择这样做)
参考另一篇文章中的一个答案 https://stackoverflow.com/a/14956056/504133
我建议您使用两个或三个活动制作简单的Android应用程序,每个活动都有简单的UI布局。您可以在那里学习,然后将更复杂的应用程序的概念应用为AudioRecorder。