ExoPlayer2.15.0无法解析createWithNotificationChannel符号如何解决?
问题根因
- 语法错误:
createWithNotificationChannel是PlayerNotificationManager的静态工厂方法,无需使用new关键字实例化,代码中多余的new会直接导致语法报错 - 依赖缺失:ExoPlayer从2.14.0版本开始将
PlayerNotificationManager相关组件从核心库拆分到exoplayer-ui独立模块,仅引入核心库无法找到对应类和方法 - 导包错误:如果导入了旧版本废弃路径的同名类,也会出现方法找不到的报错
修复步骤
- 补充依赖,在Module级别的build.gradle文件中添加和你当前核心库版本一致的ui依赖:
implementation 'com.google.android.exoplayer:exoplayer-ui:2.15.0'
添加完成后同步Gradle配置。
- 修正代码中的语法错误,删除方法调用前的
new关键字,完整修正后的代码如下:
PlayerNotificationManager playerNotificationManager = PlayerNotificationManager.createWithNotificationChannel( context, 1, "marakesh", 8, new PlayerNotificationManager.MediaDescriptionAdapter() { @Override public CharSequence getCurrentContentTitle(Player player) { return null; } @Nullable @Override public PendingIntent createCurrentContentIntent(Player player) { Intent intent = new Intent(context, MainActivity.class); return PendingIntent.getActivity( context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); } @Nullable @Override public CharSequence getCurrentContentText(Player player) { return null; } @Nullable @Override public Bitmap getCurrentLargeIcon(Player player, PlayerNotificationManager.BitmapCallback callback) { //return SampleStream.getBitmap(context, bitmapResource); return null; } }, new PlayerNotificationManager.NotificationListener(){ @Override public void onNotificationPosted(int notificationId, Notification notification, boolean ongoing) { startForeground(notificationId, notification); } @Override public void onNotificationCancelled(int notificationId, boolean dissmissedByuser){ stopSelf(); } }); playerNotificationManager.setPlayer(player);
- 确认类导入路径正确,导入的是拆分后的ui包下的类:
import com.google.android.exoplayer2.ui.PlayerNotificationManager;
- 清理项目缓存后重新构建即可解决报错。
内容的提问来源于stack exchange,提问作者MarioS
相关产品推荐
相关产品推荐

