Android 和 ACRCloud:在未初始化的 AudioRecord 上调用了 startRecording()
Android and ACRCloud: startRecording() called on an uninitialized AudioRecord
我正在使用此 SDK 设计一个简单的音乐识别应用程序 https://www.acrcloud.com/docs/audio-fingerprinting-sdks/android-sdk/
SDK提供的demo在我的phone上完美运行,目标Android SDK是18,我目前正在minSdkVersion 19
和targetSdkVersion 24
上开发这个应用。在代码方面,除了 Activity 的布局外,与演示没有什么不同。
这是我的代码:
import com.dd.CircularProgressButton;
import com.acrcloud.rec.sdk.ACRCloudConfig;
import com.acrcloud.rec.sdk.ACRCloudClient;
import com.acrcloud.rec.sdk.IACRCloudListener;
import com.acrcloud.rec.sdk.utils.ACRCloudLogger;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity implements IACRCloudListener {
//Main recognition class and config objects
private ACRCloudClient mClient;
private ACRCloudConfig mConfig;
private TextView successOrFail;
private CircularProgressButton recognitionButton;
//recognition flags
private boolean mProcessing = false;
private boolean initState = false;
//path for local db, if necessary
//private String path = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
recognitionButton = (CircularProgressButton) findViewById(R.id.startRecognition);
recognitionButton.setIndeterminateProgressMode(true);
recognitionButton.setText(R.string.rec_start);
recognitionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
start();
recognitionButton.setProgress(50);
}
});
final CircularProgressButton cancelButton = (CircularProgressButton) findViewById(R.id.stopRecogition);
cancelButton.setText(R.string.rec_cancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cancel();
cancelButton.setProgress(-1);
}
});
successOrFail = (TextView) findViewById(R.id.successOrFailView);
//Configure ACRCloud
this.mConfig = new ACRCloudConfig();
this.mConfig.acrcloudListener = this;
this.mConfig.context = this;
this.mConfig.host = "xxxxxxxxxxxxxx";
this.mConfig.accessKey = "xxxxxxxxxxxxxxxx";
this.mConfig.accessSecret = "xxxxxxxxxxxxxxxx";
this.mConfig.reqMode = ACRCloudConfig.ACRCloudRecMode.REC_MODE_REMOTE;
this.mClient = new ACRCloudClient();
this.initState = this.mClient.initWithConfig(this.mConfig);
if (this.initState) {
this.mClient.startPreRecord(3000);
}
}
//Start recognition
public void start() {
if (!this.initState) {
Toast.makeText(this, "init error", Toast.LENGTH_SHORT).show();
return;
}
if (!mProcessing) {
mProcessing = true;
if (this.mClient == null || !this.mClient.startRecognize()) {
mProcessing = false;
//mResult.setText("start error!");
}
}
}
//recognition canceled
protected void cancel(){
if(mProcessing && this.mClient != null){
mProcessing = false;
this.mClient.cancel();
}
}
//handle result
@Override
public void onResult(String result) {
if (this.mClient != null) {
this.mClient.cancel();
mProcessing = false;
}
try {
JSONObject j = new JSONObject(result);
JSONObject j1 = j.getJSONObject("status");
int j2 = j1.getInt("code");
if(j2 == 0){
successOrFail.setVisibility(View.VISIBLE);
successOrFail.setText(R.string.success);
recognitionButton.setProgress(100);
}else{
successOrFail.setVisibility(View.VISIBLE);
successOrFail.setText(R.string.error);
recognitionButton.setProgress(0);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onVolumeChanged(double volume){
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e("MainActivity", "release");
if (this.mClient != null) {
this.mClient.release();
this.initState = false;
this.mClient = null;
}
}
}
我收到了这个异常
08-31 16:38:20.119 29895-30448/musicmappr.abon219.com.musicmappr_prototype1 W/System.err: java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
08-31 16:38:20.119 29895-30448/musicmappr.abon219.com.musicmappr_prototype1 W/System.err: at android.media.AudioRecord.startRecording(AudioRecord.java:943)
08-31 16:38:20.119 29895-30448/musicmappr.abon219.com.musicmappr_prototype1 W/System.err: at com.acrcloud.rec.record.ACRCloudRecorder.startRecording(ACRCloudRecorder.java:64)
08-31 16:38:20.119 29895-30448/musicmappr.abon219.com.musicmappr_prototype1 W/System.err: at com.acrcloud.rec.sdk.worker.ACRCloudWorker.run(ACRCloudWorker.java:215)
08-31 16:38:20.139 29895-30448/musicmappr.abon219.com.musicmappr_prototype1 I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
08-31 16:38:20.139 29895-30448/musicmappr.abon219.com.musicmappr_prototype1 I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
08-31 16:38:21.789 29895-30448/musicmappr.abon219.com.musicmappr_prototype1 W/System.err: java.lang.IllegalStateException: stop() called on an uninitialized AudioRecord.
08-31 16:38:21.789 29895-30448/musicmappr.abon219.com.musicmappr_prototype1 W/System.err: at android.media.AudioRecord.stop(AudioRecord.java:996)
08-31 16:38:21.789 29895-30448/musicmappr.abon219.com.musicmappr_prototype1 W/System.err: at com.acrcloud.rec.record.ACRCloudRecorder.release(ACRCloudRecorder.java:133)
08-31 16:38:21.789 29895-30448/musicmappr.abon219.com.musicmappr_prototype1 W/System.err: at com.acrcloud.rec.sdk.worker.ACRCloudWorker.resumeRecognize(ACRCloudWorker.java:126)
08-31 16:38:21.789 29895-30448/musicmappr.abon219.com.musicmappr_prototype1 W/System.err: at com.acrcloud.rec.sdk.worker.ACRCloudWorker.run(ACRCloudWorker.java:222)
如有任何关于此问题的建议,我们将不胜感激
你用Android模拟器测试了吗? Android 模拟器不使用计算机的麦克风。
由于没有提供任何答案,我将提供自己的意见来帮助使用此 SDK 的任何人解决此问题。
超过 API 级别 23,您需要向用户请求权限。将权限添加到 AndroidManifest
是不够的。以下代码片段是一个示例:
if(ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
!= PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new String[{Manifest.permission.RECORD_AUDIO}, 1);
我正在使用此 SDK 设计一个简单的音乐识别应用程序 https://www.acrcloud.com/docs/audio-fingerprinting-sdks/android-sdk/
SDK提供的demo在我的phone上完美运行,目标Android SDK是18,我目前正在minSdkVersion 19
和targetSdkVersion 24
上开发这个应用。在代码方面,除了 Activity 的布局外,与演示没有什么不同。
这是我的代码:
import com.dd.CircularProgressButton;
import com.acrcloud.rec.sdk.ACRCloudConfig;
import com.acrcloud.rec.sdk.ACRCloudClient;
import com.acrcloud.rec.sdk.IACRCloudListener;
import com.acrcloud.rec.sdk.utils.ACRCloudLogger;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity implements IACRCloudListener {
//Main recognition class and config objects
private ACRCloudClient mClient;
private ACRCloudConfig mConfig;
private TextView successOrFail;
private CircularProgressButton recognitionButton;
//recognition flags
private boolean mProcessing = false;
private boolean initState = false;
//path for local db, if necessary
//private String path = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
recognitionButton = (CircularProgressButton) findViewById(R.id.startRecognition);
recognitionButton.setIndeterminateProgressMode(true);
recognitionButton.setText(R.string.rec_start);
recognitionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
start();
recognitionButton.setProgress(50);
}
});
final CircularProgressButton cancelButton = (CircularProgressButton) findViewById(R.id.stopRecogition);
cancelButton.setText(R.string.rec_cancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cancel();
cancelButton.setProgress(-1);
}
});
successOrFail = (TextView) findViewById(R.id.successOrFailView);
//Configure ACRCloud
this.mConfig = new ACRCloudConfig();
this.mConfig.acrcloudListener = this;
this.mConfig.context = this;
this.mConfig.host = "xxxxxxxxxxxxxx";
this.mConfig.accessKey = "xxxxxxxxxxxxxxxx";
this.mConfig.accessSecret = "xxxxxxxxxxxxxxxx";
this.mConfig.reqMode = ACRCloudConfig.ACRCloudRecMode.REC_MODE_REMOTE;
this.mClient = new ACRCloudClient();
this.initState = this.mClient.initWithConfig(this.mConfig);
if (this.initState) {
this.mClient.startPreRecord(3000);
}
}
//Start recognition
public void start() {
if (!this.initState) {
Toast.makeText(this, "init error", Toast.LENGTH_SHORT).show();
return;
}
if (!mProcessing) {
mProcessing = true;
if (this.mClient == null || !this.mClient.startRecognize()) {
mProcessing = false;
//mResult.setText("start error!");
}
}
}
//recognition canceled
protected void cancel(){
if(mProcessing && this.mClient != null){
mProcessing = false;
this.mClient.cancel();
}
}
//handle result
@Override
public void onResult(String result) {
if (this.mClient != null) {
this.mClient.cancel();
mProcessing = false;
}
try {
JSONObject j = new JSONObject(result);
JSONObject j1 = j.getJSONObject("status");
int j2 = j1.getInt("code");
if(j2 == 0){
successOrFail.setVisibility(View.VISIBLE);
successOrFail.setText(R.string.success);
recognitionButton.setProgress(100);
}else{
successOrFail.setVisibility(View.VISIBLE);
successOrFail.setText(R.string.error);
recognitionButton.setProgress(0);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onVolumeChanged(double volume){
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e("MainActivity", "release");
if (this.mClient != null) {
this.mClient.release();
this.initState = false;
this.mClient = null;
}
}
}
我收到了这个异常
08-31 16:38:20.119 29895-30448/musicmappr.abon219.com.musicmappr_prototype1 W/System.err: java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
08-31 16:38:20.119 29895-30448/musicmappr.abon219.com.musicmappr_prototype1 W/System.err: at android.media.AudioRecord.startRecording(AudioRecord.java:943)
08-31 16:38:20.119 29895-30448/musicmappr.abon219.com.musicmappr_prototype1 W/System.err: at com.acrcloud.rec.record.ACRCloudRecorder.startRecording(ACRCloudRecorder.java:64)
08-31 16:38:20.119 29895-30448/musicmappr.abon219.com.musicmappr_prototype1 W/System.err: at com.acrcloud.rec.sdk.worker.ACRCloudWorker.run(ACRCloudWorker.java:215)
08-31 16:38:20.139 29895-30448/musicmappr.abon219.com.musicmappr_prototype1 I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
08-31 16:38:20.139 29895-30448/musicmappr.abon219.com.musicmappr_prototype1 I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
08-31 16:38:21.789 29895-30448/musicmappr.abon219.com.musicmappr_prototype1 W/System.err: java.lang.IllegalStateException: stop() called on an uninitialized AudioRecord.
08-31 16:38:21.789 29895-30448/musicmappr.abon219.com.musicmappr_prototype1 W/System.err: at android.media.AudioRecord.stop(AudioRecord.java:996)
08-31 16:38:21.789 29895-30448/musicmappr.abon219.com.musicmappr_prototype1 W/System.err: at com.acrcloud.rec.record.ACRCloudRecorder.release(ACRCloudRecorder.java:133)
08-31 16:38:21.789 29895-30448/musicmappr.abon219.com.musicmappr_prototype1 W/System.err: at com.acrcloud.rec.sdk.worker.ACRCloudWorker.resumeRecognize(ACRCloudWorker.java:126)
08-31 16:38:21.789 29895-30448/musicmappr.abon219.com.musicmappr_prototype1 W/System.err: at com.acrcloud.rec.sdk.worker.ACRCloudWorker.run(ACRCloudWorker.java:222)
如有任何关于此问题的建议,我们将不胜感激
你用Android模拟器测试了吗? Android 模拟器不使用计算机的麦克风。
由于没有提供任何答案,我将提供自己的意见来帮助使用此 SDK 的任何人解决此问题。
超过 API 级别 23,您需要向用户请求权限。将权限添加到 AndroidManifest
是不够的。以下代码片段是一个示例:
if(ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
!= PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new String[{Manifest.permission.RECORD_AUDIO}, 1);