这个问题通常发生在flutter_audio_service插件中,具体原因是在通知构建器中返回了一个空的通知对象。可以通过在构建器中设置通知标题、描述、图标等属性,来避免这个问题。以下是一些示例代码:
class MyAudioService extends BackgroundAudioTask {
// ...
Notification _buildNotification() {
final mediaItem = mediaItem.value!;
return Notification(
// set notification title
title: mediaItem.title,
// set notification description
subtitle: mediaItem.artist,
// set notification icon
android: (AudioService.kAndroidIsolateService)
? AudioService.notificationIcon
: 'drawable/ic_notification',
//...
);
}
// ...
}
此外,还可以使用MediaControl
类中的一些静态方法来设置通知,例如:
Notification _buildNotification() {
final mediaItem = mediaItem.value!;
return Notification(
//...
// set notification title
title: mediaItem.title,
// set notification description
subtitle: mediaItem.artist,
// set notification icon
android: (AudioService.kAndroidIsolateService)
? AudioService.notificationIcon
: 'drawable/ic_notification',
// set notification controls
customControls: [
MediaControl.skipToPrevious,
if (playbackState.playing)
MediaControl.pause
else
MediaControl.play,
MediaControl.skipToNext,
],
//...
);
}
这个问题的另一个可能原因是在应用程序代码中使用了一个空的通知文本,例如:
AudioServiceBackground.setMediaItem(MediaItem(
id: 'audio_1',
album: '',
title: 'Audio 1',
artist: '',
artUri: Uri.http('example.com', '/audio_1.jpg'),
));
为了解决这个问题,可以在应用程序代码中设置合适的通知文本,例如:
AudioServiceBackground.setMediaItem(MediaItem(
id: 'audio_1',
album: 'Album',
title: 'Audio 1',
artist: 'Artist',
artUri: Uri.http('example.com', '/audio_1.jpg'),
));