Flutter just_audio:类型“MediaItem”不是类型强制转换中类型“String?”的子类型

zte4gxcn  于 2023-04-07  发布在  Flutter
关注(0)|答案(1)|浏览(173)

我从API接收音轨,并将它们放在一个列表中。我想将音轨添加到音频播放器,然后我写我的代码如下

playlist = ConcatenatingAudioSource(
          children: [
            ClippingAudioSource(
              child: tracs[0].isDownload
                  ? AudioSource.uri(Uri.file(adr))
                  : AudioSource.uri(Uri.parse(tracs[0].sound)),
              tag: MediaItem(
                id: '${_nextMediaId++}',
                title: tracs[0].text,
                displaySubtitle: text,
                artUri: Uri.parse(
                  art,
                ),
              ),
            ),
          ],
        );

        for (var i in tracs) {
          adr = _appDocumentsDirectory + '/' + i.id.toString() + '.mp3';
          if (i.sound != tracs[0].sound) {
            if (i.isDownload) {
              playlist.add(AudioSource.uri(
                Uri.file(adr),
                tag: MediaItem(
                    id: '${_nextMediaId++}',
                    title: i.text,
                    artUri: Uri.parse(art),
                    genre: i.video),
              ));
            } else {
              playlist.add(
                AudioSource.uri(
                  Uri.parse(i.sound),
                  tag: MediaItem(
                      id: '${_nextMediaId++}',
                      title: i.text,
                      artUri: Uri.parse(art),
                      genre: i.video),
                ),
              );
            }
          }
        }

await audioPlayer.setAudioSource(playlist);

当audioPlayer.setAudioSource抛出错误:_CastError(类型'MediaItem'不是类型强制转换中类型'String?'的子类型)
audioPlayer是Just_Audio。AudioPlayer播放列表是Just_Audio.ConcatenatingAudioSource
just_audio:^0.9.32
音频播放器:^3.0.1
just_audio_background:^0.0.1-β .9

h5qlskok

h5qlskok1#

要修复此问题,可以尝试执行以下代码:

playlist = ConcatenatingAudioSource(
  children: [
    ClippingAudioSource(
      child: tracs[0].isDownload
          ? AudioSource.uri(Uri.file(adr))
          : AudioSource.uri(Uri.parse(tracs[0].sound)),
      tag: MediaItem(
        id: '${_nextMediaId++}',
        title: tracs[0].text,
        displaySubtitle: text,
        artUri: Uri.parse(
          art,
        ),
      ).toJson(), // Serialize the MediaItem to a Map
    ),
  ],
);

for (var i in tracs) {
  adr = _appDocumentsDirectory + '/' + i.id.toString() + '.mp3';
  if (i.sound != tracs[0].sound) {
    if (i.isDownload) {
      playlist.add(AudioSource.uri(
        Uri.file(adr),
        tag: MediaItem(
            id: '${_nextMediaId++}',
            title: i.text,
            artUri: Uri.parse(art),
            genre: i.video).toJson(), // Serialize the MediaItem to a Map
      ));
    } else {
      playlist.add(
        AudioSource.uri(
          Uri.parse(i.sound),
          tag: MediaItem(
              id: '${_nextMediaId++}',
              title: i.text,
              artUri: Uri.parse(art),
              genre: i.video).toJson(), // Serialize the MediaItem to a Map
        ),
      );
    }
  }
}

await audioPlayer.setAudioSource(playlist);

在这段代码中,我们使用toJson()方法将MediaItem对象序列化为Map,然后将其传递给tag参数。这确保了MediaItem作为Map传递,而不是作为String处理。

相关问题