play method

Future<void> play(
  1. String filePath, {
  2. VoidCallback? onComplete,
})

Implementation

Future<void> play(String filePath, {VoidCallback? onComplete}) async {
  try {
    _isPaused = false;
    if (_currentPath == filePath && isPlaying) {
      await stop();
      return;
    }

    if (isPlaying) {
      await stop();
    }

    _onComplete = onComplete;
    _currentPath = filePath;

    await _player.setFilePath(filePath);

    _player.playerStateStream.listen((state) {
      if (state.processingState == just_audio.ProcessingState.completed) {
        _onComplete?.call();
      }
    });

    await _player.play();
  } catch (e) {
    debugPrint('play failed: $e');
    rethrow;
  }
}