play method
Implementation
Future<void> play(String filePath) async {
try {
_isPaused = false;
if (_currentPath == filePath && _isPlaying) {
await stop();
return;
}
if (_isPlaying) {
await stop();
}
_currentPath = filePath;
// Use native implementation on mobile platforms
if (Platform.isAndroid || Platform.isIOS) {
await AudioPlayerPlatform.play(
filePath: filePath,
onComplete: () {
_isPlaying = false;
_isPaused = false;
_listener?.onCompletion();
},
onProgressUpdate: (currentPosition, duration) {
_currentPosition = currentPosition;
_duration = duration;
_listener?.onProgressUpdate(currentPosition, duration);
},
onPlay: () {
_isPlaying = true;
_isPaused = false;
_listener?.onPlay();
},
onPause: () {
_isPlaying = false;
_isPaused = true;
_listener?.onPause();
},
onResume: () {
_isPlaying = true;
_isPaused = false;
_listener?.onResume();
},
onError: (errorMessage) {
debugPrint('AudioPlayer error: $errorMessage');
_isPlaying = false;
_isPaused = false;
_listener?.onError(errorMessage);
},
);
} else {
throw UnsupportedError('AudioPlayer only supports Android and iOS');
}
} catch (e) {
debugPrint('play failed: $e');
rethrow;
}
}