目录
背景
看到一些视频里面翻译机,或手机翻译功能,最近也是在做这方面的事.做一个记录.
ai语音识别的sdk有不少,腾讯,百度,讯飞,微软.我们用微软sdk,支持的语言多.费用也低一些.
讯飞只识别中文,据说他们自己的产品可以是多语言.
腾讯,百度这些,在国外的识别方面可能不如微软.支持的语言数量少.
目前主流的app是与硬件绑定的.所以商店里下载,但用不了,因为没有购买硬件,通常是蓝牙耳机.也有可能是ai眼镜(带蓝牙)
微软的sdk文档:
https://learn.microsoft.com/zh-cn/azure/ai-services/speech-service/how-to-translate-speech?tabs=terminal&pivots=programming-language-java
根据自己的需求,选择不同的语言.
用到的主要是两类,一类是多语音输入,一类是单语音输入.
单语音是最简单,也更便宜一些.准确率要高.
多语音,最多四路,就是四种,有一个缺点,识别的速度要低一些,准确率要低,会出现不翻译的情况,或不识别的情况.
代码:
官方文档非常详细地说明了如何使用了,那么我这里还是会写一个相对完整的示例.
1.集成sdk
我用android平台,clientSdk = "1.42.0", com.microsoft.cognitiveservices.speech:client-sdk.
这样就集成了.
2.申请权限.
首先要蓝牙权限.因为要绑定设备.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission
android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission
android:name="android.permission.BLUETOOTH_SCAN"
android:usesPermissionFlags="neverForLocation"
tools:targetApi="31" />
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"
tools:ignore="CoarseFineLocation" />
其实是录音权限.
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
不同sdk的申请可以使用权限sdk去申请,这个不贴代码了.用自己擅长的工具即可.
3.蓝牙适配
蓝牙不同版本会有一些差异.我又遇到荣耀手机和别的不一样了.操蛋的权限.
在首页要监听蓝牙的连接状态,如果硬件连接断了,app的入口就不能点击.
public class BluetoothConnector {
public interface BluetoothListener {
void onDeviceConnected(BluetoothDevice device);
}
private BluetoothAdapter bluetoothAdapter;
private BluetoothA2dp bluetoothA2dp;
private BluetoothListener bluetoothListener;
public void setBluetoothListener(BluetoothListener bluetoothListener) {
this.bluetoothListener = bluetoothListener;
}
public BluetoothConnector() {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
@SuppressLint("MissingPermission")
public void bind(Context context) {
if (bluetoothAdapter != null) {
bluetoothAdapter.getProfileProxy(context, new BluetoothProfile.ServiceListener() {
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == BluetoothProfile.A2DP) {
bluetoothA2dp = (BluetoothA2dp) proxy;
//这里如果不注释,荣耀手机会有问题.因为我调用这段代码前,其实权限是申请过的,所以这里注释了.
/*if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}*/
List<BluetoothDevice> connectedDevices = bluetoothA2dp.getConnectedDevices();
if (!connectedDevices.isEmpty()) {
BluetoothDevice device = connectedDevices.get(0);
// 这里可以处理连接成功后的逻辑
Timber.d("device:%s-%s", device, bluetoothListener);
if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
if (null != bluetoothListener) {
bluetoothListener.onDeviceConnected(device);
}
}
}
}
}
@Override
public void onServiceDisconnected(int profile) {
if (profile == BluetoothProfile.A2DP) {
bluetoothA2dp = null;
}
if (null != bluetoothListener) {
bluetoothListener.onDeviceConnected(null);
}
}
}, BluetoothProfile.A2DP);
}
}
public BluetoothA2dp getBluetoothA2dp() {
return bluetoothA2dp;
}
}
这段是进入首页后,扫描蓝牙连接的状态.
先在首页的activity中:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
registerBluetoothReceiver()
bluetoothConnector = BluetoothConnector()
.apply {
setBluetoothListener(
object : BluetoothConnector.BluetoothListener {
@SuppressLint("MissingPermission")
override fun onDeviceConnected(device: BluetoothDevice?) {
bluetoothDevice = device
if (isAdded) {
handleDeviceConnected(device)
}
}

最低0.47元/天 解锁文章
4591

被折叠的 条评论
为什么被折叠?



