![](/img/trans.png)
[英]How to combine flutter audio_service and icymetadata streambuilder?
[英]Flutter audio_service stream update notification with IcyMetadata
我想為一個流/網址創建一個流應用程序。
在 audio_service 示例中,在 AudioPlayerHandler 中添加了一個 MediaItem。
這很有效,但是當 IcyMedata 更新通知時……顯然……有來自添加的 MediaItem 的標題和內容。 在應用程序中,我可以根據 _audioHandler.playbackstate 和 _player.icyMetadata..info..title 進行更新! 還有一些 sting.splits(' - ') 我可以使用 StreamBilder 更新應用程序中的信息。
還嘗試了一個骯臟的技巧,通過更改 audio_service 並添加標題作為屬性並在自定義 AudioHandler 中使用 _player.icy 連接它來在 PlaybackState object 中添加標題。
但是必須有一種適當的方法來設置新的元數據或由該框架本身設置。 _audioHandler.updateMediaItem 到目前為止對通知信息什么也沒做,這就是我需要的!
我找到了一個 addStream ......我的嘗試(片段)
Stream<MediaItem> _item() async* { MediaItem(
id: "http://stream.drumandbass.fm:9012",
title: "TEST",
artist: "Hello",
album: "Album",
duration: const Duration(milliseconds: 5739820),
genre: 'Drum and Bass',
artUri: Uri.parse('test.jpg'));
}
Future<AudioPlayerHandler> initAH() async {
return await AudioService.init(
builder: () => AudioPlayerHandler(),
config: const AudioServiceConfig(
androidNotificationChannelId: 'com.ryanheise.myapp.channel.audio',
androidNotificationChannelName: 'Audio playback',
androidNotificationOngoing: true,
),
);
class AudioPlayerHandler extends BaseAudioHandler {
/// Initialise our audio handler.
AudioPlayerHandler() {
// So that our clients (the Flutter UI and the system notification) know
// what state to display, here we set up our audio handler to broadcast all
// playback state changes as they happen via playbackState...
_player.playbackEventStream.map(_transformEvent).pipe(playbackState);
// ... and also the current media item via mediaItem.
mediaItem.addStream(_item());
// Load the player.
_player.setAudioSource(AudioSource.uri(Uri.parse(mediaItem.value!.id)));
}
// some play stop the rest of the example of audio_service
小部件:
FutureBuilder<AudioPlayerHandler>(
future: initAH(),
builder: (BuildContext context,
AsyncSnapshot<AudioPlayerHandler> snapshot) {
if (!snapshot.hasData) {
// while data is loading:
return Center(
child: CircularProgressIndicator(),
);
} else {
_audioHandler = snapshot.data!;
loaded = true;
// data loaded:
return Container();
}
},
),
… 使用 _player.add(MediaItem()); 就像在示例中一樣,我得到了一個 snapshot.hasData 但加載的 addStream 保持為假。
我感謝任何提示以正確處理此問題。
謝謝,
湯姆
因為你沒有看到通知。 請檢查您的 android 配置。
<manifest xmlns:tools="http://schemas.android.com/tools" ...>
<!-- ADD THESE TWO PERMISSIONS -->
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<application ...>
...
<!-- EDIT THE android:name ATTRIBUTE IN YOUR EXISTING "ACTIVITY" ELEMENT -->
<activity android:name="com.ryanheise.audioservice.AudioServiceActivity" ...>
...
</activity>
<!-- ADD THIS "SERVICE" element -->
<service android:name="com.ryanheise.audioservice.AudioService"
android:exported="true" tools:ignore="Instantiatable">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
</service>
<!-- ADD THIS "RECEIVER" element -->
<receiver android:name="com.ryanheise.audioservice.MediaButtonReceiver"
android:exported="true" tools:ignore="Instantiatable">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
</application>
</manifest>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.