open method

void open(
  1. MediaSource source, {
  2. bool autoStart = true,
})

Opens a new media source into the player.

Takes a Media or Playlist to open in the player.

Starts playback itself by default. Pass autoStart: false to stop this from happening.

player.open(
  Media.file(
    File('C:/music.ogg'),
  ),
  autoStart: false,
);
player.open(
  Playlist(
    medias: [
      Media.file(
        File('C:/music.mp3'),
      ),
      Media.file(
        File('C:/audio.mp3'),
      ),
      Media.network('https://alexmercerind.github.io/music.mp3'),
    ],
  ),
);

Implementation

void open(MediaSource source, {bool autoStart = true}) {
  if (source is Media) {
    final args = <String>[
      source.mediaType.toString(),
      source.resource,
      source.startTime.argument('start-time'),
      source.stopTime.argument('stop-time'),
    ];
    // Parse [commandlineArguments] & convert to `char*[]`.
    final List<Pointer<Utf8>> pointers = args.map<Pointer<Utf8>>((e) {
      return e.toNativeUtf8();
    }).toList();
    final Pointer<Pointer<Utf8>> arr = calloc.allocate(args.join().length);
    for (int i = 0; i < args.length; i++) {
      arr[i] = pointers[i];
    }
    PlayerFFI.open(
      id,
      autoStart ? 1 : 0,
      arr,
      1,
    );
    // Freed the memory allocated for `char*[]`.
    calloc.free(arr);
    pointers.forEach(calloc.free);
  }
  if (source is Playlist) {
    List<String> medias = <String>[];
    for (var media in source.medias) {
      medias.addAll(
        <String>[
          media.mediaType.toString(),
          media.resource,
          media.startTime.argument('start-time'),
          media.stopTime.argument('stop-time'),
        ],
      );
    }
    // Parse [commandlineArguments] & convert to `char*[]`.
    final List<Pointer<Utf8>> pointers = medias.map<Pointer<Utf8>>((e) {
      return e.toNativeUtf8();
    }).toList();
    final Pointer<Pointer<Utf8>> arr = calloc.allocate(medias.join().length);
    for (int i = 0; i < medias.length; i++) {
      arr[i] = pointers[i];
    }
    PlayerFFI.open(
      id,
      autoStart ? 1 : 0,
      arr,
      source.medias.length,
    );
    // Freed the memory allocated for `char*[]`.
    calloc.free(arr);
    pointers.forEach(calloc.free);
  }
}