flutter - 如何创建一个可以正确访问android原生AudioManager的flutter插件
问题描述
我正在创建一个名为“newplugin”的插件。我想做的是AudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION)
让应用程序通过手机的听筒输出音频。(这适用于本机 Java Android 应用程序)但它不适用于颤振。我怎样才能正确实施它?
package com.newplugin.newplugin;
import android.content.Context;
import android.media.AudioManager;
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
/** NewpluginPlugin */
public class NewpluginPlugin implements FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private MethodChannel channel;
private AudioManager audioManager;
private Context context;
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "newplugin");
channel.setMethodCallHandler(this);
context = flutterPluginBinding.getApplicationContext();
audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("setComMode")) {
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
}
else {
result.notImplemented();
}
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}
}
解决方案
推荐阅读
- object - 3D 图像中的对象识别
- apache-spark - 在 spark 中跳过 hive 表中丢失的文件以避免 FileNotFoundException
- c# - 无法将“System.Web.UI.WebControls.ContentPlaceHolder”类型的对象转换为“System.Web.UI.WebControls.GridViewRow”类型。在按钮点击
- apache - Apache 关闭意外无法启动
- javascript - 如何在odoo中实现recaptcha
- asp.net-core - 如何解决保单索赔?
- linux-kernel - 如何在 Linux 中设置串口以 PIO 模式(不是 DMA)运行
- laravel - Vagrant:多盒 laravel/宅基地。如何通过 macOS 命令行删除旧的?
- c# - join 子句中表达式之一的类型不正确 type interface failed in call to join
- regex - 如果字符串第一次出现点,则正则表达式匹配?