gpt4 book ai didi

android - java.lang.NoSuchMethodError : No virtual method getMicrophones() 错误

转载 作者:行者123 更新时间:2023-11-29 16:31:55 26 4
gpt4 key购买 nike

我有这个非常简单的枚举设备上的所有麦克风

public class MainActivity
extends AppCompatActivity
{
private static String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AudioManager audioManager =
(AudioManager) getSystemService(Context.AUDIO_SERVICE);
List<MicrophoneInfo> mics = null;
try
{
mics = audioManager.getMicrophones();
}
catch ( IOException e )
{
e.printStackTrace();
}
Log.d(TAG,"mics.size() = "+ mics.size());
for(MicrophoneInfo mic : mics)
{

}
}
}

但是当我在我的任何设备上运行应用程序时,我得到

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.micsrc, PID: 8721
java.lang.NoSuchMethodError: No virtual method getMicrophones()Ljava/util/List;
in class Landroid/media/AudioManager; or its super classes (declaration
of 'android.media.AudioManager' appears in /system/framework/framework.jar)
at com.example.micsrc.MainActivity.onCreate(MainActivity.java:27)
at android.app.Activity.performCreate(Activity.java:6285)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2408)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2541)
at android.app.ActivityThread.access$900(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1382)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5554)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)

最佳答案

你需要处理向后兼容:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
List<MicrophoneInfo> mics = am.getMicrophones();
...
} else {
AudioDeviceInfo[] deviceInfo = am.getDevices(AudioManager.GET_DEVICES_INPUTS);
...
}

“耳机”连接器不是麦克风 - 但对于双工耳机,它仍可能连接有麦克风……即使可能无法直接检测到该麦克风。 BT 耳机甚至扬声器也可能配备麦克风,这需要从 A2DP 接收器查询。

关于android - java.lang.NoSuchMethodError : No virtual method getMicrophones() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54488042/

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