wangchenyan / ponymusic

An Android online music player like NetEase Cloud Music, based on Media3 and ExoPlayer
https://juejin.cn/post/7294072229003952143
2.56k stars 469 forks source link

关于该项目运行在Android 9.0 系统上的适配问题。 #60

Closed EsZhangHome closed 8 months ago

EsZhangHome commented 5 years ago
  1. 需要适配 网络请求。 在manifest中Application中添加 android:networkSecurityConfig="@xml/network_security_config" 属性,其中network_security_config是 xml中的文件,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>
  1. 关于运行的时候需要添加前台的权限 在manifest 添加
  2. 运行的时候报如下错误:

19-02-21 15:56:30.431 12154-12154/me.wcy.music E/AndroidRuntime: FATAL EXCEPTION: main Process: me.wcy.music, PID: 12154 android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=273 pri=0 contentView=me.wcy.music/0x7f0c0046 vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2050) at android.os.Handler.dispatchMessage(Handler.java:109) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:7470) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)

解决方法是: 在Notifier 修改对应方法的代码如下:

private static final String CHANNELID = "me.wcy.music.application.1"; private static NotificationChannel mChannel = null; //通知栏 private static NotificationCompat.Builder builder; //通知和自定义通知 private Notification buildNotification(Context context, Music music, boolean isPlaying) { Intent intent = new Intent(context, MusicActivity.class); intent.putExtra(Extras.EXTRA_NOTIFICATION, true); intent.setAction(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); //修改这里的代码------------------------------------------------------------ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { builder = new NotificationCompat.Builder(context, CHANNELID); mChannel = notificationManager.getNotificationChannel(CHANNELID); if (mChannel == null) { mChannel = new NotificationChannel(CHANNELID, "音频播放", NotificationManager.IMPORTANCE_LOW); } mChannel.enableLights(false);//是否在桌面icon右上角展示小红点 mChannel.setSound(null, null); //设置通知声音 mChannel.setShowBadge(false);//是否在久按桌面图标时显示此渠道的通知 mChannel.enableVibration(false); if (notificationManager != null) { notificationManager.createNotificationChannel(mChannel); } } else { builder = new NotificationCompat.Builder(context); } builder.setContentIntent(pendingIntent) .setSmallIcon(R.drawable.ic_notification) .setCustomContentView(getRemoteViews(context, music, isPlaying)); builder.setOngoing(true); //--------------------------------------------------------------------------------------------------- return builder.build();

我运行的环境是 Android studio 3.3.1 希望对后面的小伙伴有用。

wangchenyan commented 5 years ago

感谢您的建议,该项目目前仅适配了 Android N,有时间会适配新版本。

wangchenyan commented 8 months ago

新版本已适配到 Android 14,欢迎体验