loadPlaylist method

Future<void> loadPlaylist(
  1. String playlist, {
  2. LoadPlaylistMode mode = LoadPlaylistMode.replace,
})

load a playlist file @param mode ->
LoadPlaylistMode.replace replace current playlist
LoadPlaylistMode.append append to current playist

Implementation

Future<void> loadPlaylist(String playlist,
    {LoadPlaylistMode mode = LoadPlaylistMode.replace}) async {
  playlist = path.joinAll([path.current, playlist]);
  checkStat() async {
    Completer completer = Completer();
    try {
      // check if mpv is running at all and reject the promise if not
      if (!running) {
        completer.completeError(
            _errorHandler.errorMessage(8, 'loadPlaylist()', args: [
          playlist,
          mode.name
        ], options: {
          'replace': 'replace the current playlist (default)',
          'append': 'append to the current playlist'
        }));
      }

      // check if the playlistfile exists
      FileStat fileStat = await FileStat.stat(playlist);
      if (verbose && debug) print("CHECK STAT ${playlist}");
      if (fileStat.type == FileSystemEntityType.file) {
        completer.complete();
      }
    } catch (e) {
      completer.completeError(
          _errorHandler.errorMessage(0, 'loadPlaylist()', args: [playlist]));
    }
    return completer.future;
  }

  return checkStat().then((value) async {
    Completer completer = Completer();
    Socket observeSocket = await Socket.connect(
        InternetAddress(socketURI, type: InternetAddressType.unix), 0);

    await command('loadlist', [playlist, mode.name]);
    if (mode == LoadPlaylistMode.append && !completer.isCompleted) {
      observeSocket.destroy();
      completer.complete();
    }
    // timeout
    int timeout = 0;
    // check if the file was started
    bool started = false;

    observeSocket.listen((event) {
      timeout += 1;
      List<String> messages = utf8.decode(event).split('\n');
      // check every message
      for (var message in messages) {
        // ignore empty messages
        if (message.isNotEmpty) {
          Map msgMap = jsonDecode(message);
          if (msgMap.containsKey("event")) {
            if (msgMap["event"] == 'start-file') {
              started = true;
            }
            // when the file has successfully been loaded resolve the promise
            else if (msgMap["event"] == 'file-loaded' &&
                started &&
                !completer.isCompleted) {
              observeSocket.destroy();
              // resolve the promise
              completer.complete();
            }
            // when the track has changed we don't need a seek event
            else if (msgMap["event"] == 'end-file' && started) {
              observeSocket.destroy();
              // get the filename of the not playable song
              getProperty("playlist/0/filename").then((filename) {
                return completer.complete(_errorHandler
                    .errorMessage(0, 'loadPlaylist()', args: [filename]));
              });
            }
          }
        }
      }
      // reject the promise if it took to long until the playback-restart happens
      // to prevent having sockets listening forever
      if (timeout > 10) {
        observeSocket.destroy();
        completer
            .completeError(_errorHandler.errorMessage(5, 'loadPlaylist()'));
      }
    });
    return completer.future;
  });
}