1. 启动屏幕分享出现如下 crash,怎么处理?
Caused by: java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
解决方案
在使用 SDK,将 targetSdkVersion 设置为 30,进行屏幕分享时会出现如下崩溃,这主要是因为谷歌隐私策略导致的,需要启动一个前台的 Service,并且
android:foregroundServiceType="mediaProjection"才可以解决,具体步骤如下:1. 创建一个 Service ,并绑定一个 Notification 使其作为前台 Service。
private void startNotification() {if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {return;}Intent notificationIntent = new Intent(this, TestService.class);// Android 12+ 必须指定 FLAG_IMMUTABLE 或 FLAG_MUTABLEint pendingIntentFlags = PendingIntent.FLAG_UPDATE_CURRENT;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {pendingIntentFlags |= PendingIntent.FLAG_IMMUTABLE;}PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, pendingIntentFlags);NotificationCompat.Builder notificationBuilder =new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_foreground)).setSmallIcon(R.drawable.ic_launcher_foreground).setContentTitle("Starting Service").setContentText("Starting monitoring service").setContentIntent(pendingIntent);Notification notification = notificationBuilder.build();NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,NOTIFICATION_CHANNEL_NAME,NotificationManager.IMPORTANCE_DEFAULT);channel.setDescription(NOTIFICATION_CHANNEL_DESC);NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);notificationManager.createNotificationChannel(channel);startForeground(1, notification);}
2. 在 AndroidManifest.xml 中配置:
2.1 加入权限。
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
2.2 注册 Service。
<serviceandroid:name=".TestService"android:foregroundServiceType="mediaProjection"android:enabled="true"android:exported="false" />
3. 在启动推流前,请先启动此 Service:
private void startNotification() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {// Android 8.0+ 创建通知渠道NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,NOTIFICATION_CHANNEL_NAME,NotificationManager.IMPORTANCE_DEFAULT);channel.setDescription(NOTIFICATION_CHANNEL_DESC);NotificationManager notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);notificationManager.createNotificationChannel(channel);}Intent notificationIntent = new Intent(this, TestService.class);int pendingIntentFlags = PendingIntent.FLAG_UPDATE_CURRENT;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {pendingIntentFlags |= PendingIntent.FLAG_IMMUTABLE;}PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, pendingIntentFlags);NotificationCompat.Builder notificationBuilder =new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_foreground)).setSmallIcon(R.drawable.ic_launcher_foreground).setContentTitle("Starting Service").setContentText("Starting monitoring service").setContentIntent(pendingIntent);Notification notification = notificationBuilder.build();startForeground(1, notification);}
注意:
2. 应用启动出现如下 crash,怎么处理?
java.lang.UnsatisfiedLinkError: No implementation found for byte[] com.tencent.liteav.basic.license.LicenceCheck.nativeIvParameterSpec(byte[]) (tried Java_com_tencent_liteav_basic_license_LicenceCheck_nativeIvParameterSpec and Java_com_tencent_liteav_basic_license_LicenceCheck_nativeIvParameterSpec___3B);
java.lang.UnsatisfiedLinkError: No implementation found for boolean com.tencent.liteav.sdk.common.LicenseChecker.nativeSetLicense
java.lang.UnsatisfiedLinkError: No implementation found for void com.tencent.liteav.basic.log.TXCLog.nativeLogInit()
java.lang.UnsatisfiedLinkError: No implementation found for void com.tencent.liteav.base.util.LiteavLog.nativeSetConsoleLogEnabled(boolean)
出现 java.lang.UnsatisfiedLinkError 异常,一般是 so 加载失败了。
解决方案
Android Studio 编译错误。
可能和 Android Studio 编译有关,so 没有正确编译到 APK 内,此时 Clean Build,然后重新编译即可。
设备 CPU 架构和打包到 APK 内的 so 架构不匹配。
请检查设备 CPU 架构和打包到 APK 内部的 so 是否匹配,如果不匹配,请在 build.gradle 中正确配置:
defaultConfig {ndk {abiFilters 'armeabi-v7a', 'arm64-v8a'}}