gpt4 book ai didi

java - 录音机应用

转载 作者:行者123 更新时间:2023-12-02 04:51:45 25 4
gpt4 key购买 nike

我目前正在开发一个 Android 应用程序,它是一个简单的录音机应用程序,现在我想在其中添加一项功能。谁能帮我制作一个录音机,可以录制2分钟的音频 fragment 。

 public void intervalRecorder(long addOfThree){

//This code is not working properly

new Handler().postDelayed(new Runnable() {
@Override
public void run() {

mIntervalRecorder.setEnabled(false);
mStopRecordBtn.setEnabled(false);
mRecordBtn.setEnabled(false);


k=1;
testPath=UUID.randomUUID().toString() + "_audio_record.3gp";
pathSave = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/Recorder Application/" +testPath;

setupMediaRecorder12();
try {
mediaRecorder12.prepare();
mediaRecorder12.start();
} catch (IOException e) {
e.printStackTrace();
}

Toast.makeText(RecordActivity.this,"Recording...",Toast.LENGTH_SHORT).show();
}
},addOfThree);
mediaRecorder12.stop();
uploadAudio();
Toast.makeText(RecordActivity.this,"Stop...",Toast.LENGTH_SHORT).show();
k=0;

mIntervalRecorder.setEnabled(true);
mStopRecordBtn.setEnabled(true);
mRecordBtn.setEnabled(true);
}

`

private void setupMediaRecorder12() {
mediaRecorder12 = new MediaRecorder();

mediaRecorder12.setAudioSource(MediaRecorder.AudioSource.MIC);

mediaRecorder12.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

mediaRecorder12.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);

mediaRecorder12.setOutputFile(pathSave);
}

我不知道为什么会收到 NullPointerException

Logcat 错误

  java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaRecorder.stop()' on a null object reference
at com.shashank.recorderapplication.RecordActivity.intervalRecorder(RecordActivity.java:299)
at com.shashank.recorderapplication.RecordActivity$5.onClick(RecordActivity.java:242)
at android.view.View.performClick(View.java:6935)
at android.widget.TextView.performClick(TextView.java:12752)
at android.view.View$PerformClick.run(View.java:26211)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7000)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

最佳答案

//Start 功能必须位于处理程序外部,而 Stop 功能必须位于处理程序内部

public void intervalRecorder(long addOfThree){

g=1;
k=1;
testPath=UUID.randomUUID().toString() + "Custom_audio_record.3gp";
pathSave = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/Recorder Application/" +testPath;

setupMediaRecorder();
try {
mIntervalRecorder.setEnabled(false);
mStopRecordBtn.setEnabled(false);
mRecordBtn.setEnabled(false);

mediaRecorder.prepare();
mediaRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}

Toast.makeText(RecordActivity.this,"Recording...",Toast.LENGTH_SHORT).show();

new Handler().postDelayed(new Runnable() {
@Override
public void run() {


try{
PackageManager p = getPackageManager();
ComponentName componentName = new ComponentName(RecordActivity.this, MainActivity.class);
p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

}catch (Exception ex){
ex.printStackTrace();
}


mediaRecorder.stop();
uploadAudio12();
Toast.makeText(RecordActivity.this,"Stop...",Toast.LENGTH_SHORT).show();
k=0;
g=0;
mIntervalRecorder.setEnabled(true);
mStopRecordBtn.setEnabled(true);
mRecordBtn.setEnabled(true);

}
},addOfThree);
}

关于java - 录音机应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56433701/

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