Converting A List of MediaItem to ConcatenatingAudioSource Error: Instance of 'ConcatenatingAudioSource'

Converting A List of MediaItem to ConcatenatingAudioSource Error: Instance of 'ConcatenatingAudioSource'

我正在尝试 parse/convert MediaItem 列表到 ConcatenatingAudioSource,但我的单元测试一直给我一个错误,即转换后的 ConcatenatingAudioSource 不匹配了解目标来源。我的测试是

List<MediaItem> audioSource = <MediaItem>[
      MediaItem(
        // This can be any unique id, but we use the audio URL for convenience.
        id: "https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3",
        album: "Science Friday",
        title: "A Salute To Head-Scratching Science",
        artist: "Science Friday and WNYC Studios",
        duration: Duration(milliseconds: 5739820),
        artUri: Uri.parse(
            "https://media.wnyc.org/i/1400/1400/l/80/1/ScienceFriday_WNYCStudios_1400.jpg"),
      ),
      MediaItem(
        id: "https://s3.amazonaws.com/scifri-segments/scifri201711241.mp3",
        album: "Science Friday",
        title: "From Cat Rheology To Operatic Incompetence",
        artist: "Science Friday and WNYC Studios",
        duration: Duration(milliseconds: 2856950),
        artUri: Uri.parse(
            "https://media.wnyc.org/i/1400/1400/l/80/1/ScienceFriday_WNYCStudios_1400.jpg"),
      ),
];

test('mediaItem2ConcatenatingAudioSource test', () {
      expect(
        // Here's where the conversion happens
        ConcatenatingAudioSource(
          children: audioSource
              .map((item) => AudioSource.uri(Uri.parse(item.id)))
              .toList(),
        ),
        ConcatenatingAudioSource(children: [
          AudioSource.uri(Uri.parse(
              'https://s3.amazonaws.com/scifri-episodes/scifri20181123-episode.mp3')),
          AudioSource.uri(Uri.parse(
              "https://s3.amazonaws.com/scifri-segments/scifri201711241.mp3")),
        ]),
      );
});

我得到的错误如下。我相信这是在说这两个 ConcatenatingAudioSource 不一样,它们应该在哪里。

Expected: <Instance of 'ConcatenatingAudioSource'>
  Actual: <Instance of 'ConcatenatingAudioSource'>

我从字面上复制了 audio_service 示例(第 362 行)中的转换部分,所以我真的很困惑这里出了什么问题。 (当我的实际应用程序是 运行 时,此转换代码也不起作用。我将测试的 actual 部分放在 setSource() 方法中,然后 play(),但没有任何反应.)

如有任何帮助,我们将不胜感激!谢谢!

just_audio 不认为两个音频源相等,即使它们包含相同的元数据。这是因为每个副本都有自己的内部缓冲区和不同的内部状态。

您所能做的就是检查音频源中元数据的相等性。例如比较播放列表 ab:

ConcatenatingAudioSource a = ...;
ConcatenatingAudioSource b = ...;

expect(
  a.children.map((s) => (s as UriAudioSource).uri).toList(),
  b.children.map((s) => (s as UriAudioSource).uri).toList(),
);