play method

Future<void> play()

Implementation

Future<void> play() async {
  var idle = await getProperty<bool>('idle-active');
  var playlistSize = await getPlaylistSize();
  // get the filename of the first item in the playlist for error handling purposes
  var fname = await getProperty('playlist/0/filename');

  bool started = false;

  if (idle && playlistSize > 0) {
    Socket observeSocket = await Socket.connect(
        InternetAddress(socketURI, type: InternetAddressType.unix), 0);

    await setProperty('playlist-pos', 0);

    observeSocket.listen((event) {
      var messages = utf8.decode(event).split("\n");
      for (var message in messages) {
        if (message.isNotEmpty) {
          Map messageMap = jsonDecode(message);
          if (messageMap.containsKey("event")) {
            if (messageMap["event"] == 'start-file') {
              started = true;
            }
            // when the file has successfully been loaded resolve the promise
            else if (messageMap["event"] == 'file-loaded' && started) {
              observeSocket.destroy();
              // resolve the promise
              return;
            }
            // when the track has changed we don't need a seek event
            else if (messageMap["event"] == 'end-file' && started) {
              observeSocket.destroy();
              // return reject();
              throw _errorHandler.errorMessage(0, 'play()', args: [fname]);
            }
          }
        }
      }
    });
  }
  // if mpv is not idle and has files queued just set the pause state to false
  else {
    await setProperty('pause', false);
  }
}