How can I play sound as stream<list<int>>

54 Views Asked by At

I am trying to get a video from YouTube.

class MyCustomSource extends StreamAudioSource { final List<int> bytes; MyCustomSource(this.bytes);

@override Future<StreamAudioResponse> request([int? start, int? end]) async { start ??= 0; end ??= bytes.length; return StreamAudioResponse( sourceLength: bytes.length, contentLength: end - start, offset: start, stream: Stream.value(bytes.sublist(start, end)), contentType: 'video/mp4', ); } }

  var get = await videoUrl.first;
  var getAsUInt8List = Uint8List.fromList(get);
  AudioPlayer player = AudioPlayer();
  await player.setAudioSource(MyCustomSource(getAsUInt8List));
  player.play();

Errors :

E/ExoPlayerImplInternal( 5995): at com.google.android.exoplayer2.extractor.mp4.Mp4Extractor.read(Mp4Extractor.java:254) E/ExoPlayerImplInternal( 5995): at com.google.android.exoplayer2.source.BundledExtractorsAdapter.read(BundledExtractorsAdapter.java:127) E/ExoPlayerImplInternal( 5995): at com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:1039) E/ExoPlayerImplInternal( 5995): at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:412) E/ExoPlayerImplInternal( 5995): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) E/ExoPlayerImplInternal( 5995): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) E/ExoPlayerImplInternal( 5995): at java.lang.Thread.run(Thread.java:764) E/AudioPlayer( 5995): TYPE_SOURCE: null I/ExoPlayerImpl( 5995): Release 2b7745 [ExoPlayerLib/2.18.7] [generic_x86, Android SDK built for x86, Google, 27] [goog.exo.core, goog.exo.exoplayer, goog.exo.decoder, goog.exo.datasource, goog.exo.extractor] I/flutter ( 5995): err : (0) Source error

I used other audio services, but I can't fix it ...

1

There are 1 best solutions below

1
Jemis Goti On

it seems that you are trying to play video data as audio using stream source. When you read bytes of the video at that time it will give video data instead of the separate. You need to separate audio then you can play it.