bluefireteam / audioplayers

A Flutter package to play multiple audio files simultaneously (Android/iOS/web/Linux/Windows/macOS)
https://pub.dartlang.org/packages/audioplayers
MIT License
2.02k stars 846 forks source link

[windows App crash] : Unhandled Exception: UnimplementedError: Event Method does not exist audio.onCurrentPosition #1846

Open paladinghub opened 1 month ago

paladinghub commented 1 month ago

Checklist

Current bug behaviour

when i used audioplayers 6.1.0 on windows11, some bugs reported and app crashed occured. when DeviceFileSource is loaded, it reported:Unhandled Exception: UnimplementedError: Event Method does not exist audio.onCurrentPosition. when back to last page,it reported:Unhandled Exception: Bad state: No element In addition, the play, pause, and other buttons can work normally. When returning to the previous page, the console repeatedly printed BUG information, which caused the APP crash

Expected behaviour

1:load DeviceFileSource will be ok 2: back to last page will be ok

Steps to reproduce

  1. Execute flutter run on the code sample
  2. ...
  3. ...

Code sample

Code sample ```dart import 'package:flutter/material.dart'; import 'package:audioplayers/audioplayers.dart'; class AudioPlayersPage extends StatefulWidget { const AudioPlayersPage({super.key}); @override State createState() => _AudioPlayersPageState(); } class _AudioPlayersPageState extends State { PlayerState? _playerState; late AudioPlayer _audioPlayer; Duration _position = Duration.zero; Duration? _duration; bool get _isPlaying => _playerState == PlayerState.playing; bool get _isPaused => _playerState == PlayerState.paused; @override void initState() { super.initState(); _audioPlayer = AudioPlayer(); playAudio(); } void playAudio() async { try { _audioPlayer.setSource(DeviceFileSource('D:/default_missingAudio.mp3')); _audioPlayer.onPlayerStateChanged.listen((state) { setState(() { _playerState = state; debugPrint('_playerState: $_playerState'); if (_playerState == PlayerState.completed) { debugPrint('重新播放'); playAudio(); } }); }); _audioPlayer.onPositionChanged.listen((position) { setState(() { _position = position; }); }); _audioPlayer.onDurationChanged.listen((duration) { _duration = duration; }); } catch (e) { // 处理异常 debugPrint('Error playing audio: $e'); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text(''), ), body: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ _duration != null ? SizedBox( child: Slider( value: (_duration != null && _position.inMilliseconds > 0 && _position.inMilliseconds < _duration!.inMilliseconds) ? _position.inMilliseconds / _duration!.inMilliseconds : 0.0, onChanged: _onSliderChanged, ), ) : Container(), Text('音频播放当前位置: ${_position.toString().split('.').first}'), Text('音频播放总时长: ${_duration?.toString().split('.').first ?? ''}'), Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ Expanded( child: IconButton( key: const Key('play_button'), onPressed: () { if (!_isPlaying) { _play(); } }, iconSize: 48.0, icon: const Icon(Icons.play_arrow), ), ), Expanded( child: IconButton( key: const Key('pause_button'), onPressed: () { if (_isPlaying) { _pause(); } }, iconSize: 48.0, icon: const Icon(Icons.pause), ), ), Expanded( child: IconButton( key: const Key('stop_button'), onPressed: () { if (_isPlaying || _isPaused) { _stop(); } }, iconSize: 48.0, icon: const Icon(Icons.stop), ), ), ], ) ], ), ); } void _onSliderChanged(double newValue) { if (_duration == null) { return; } final position = newValue * _duration!.inMilliseconds; _audioPlayer.seek(Duration(milliseconds: position.round())); } Future _play() async { await _audioPlayer.resume(); setState(() => _playerState = PlayerState.playing); } Future _pause() async { await _audioPlayer.pause(); setState(() => _playerState = PlayerState.paused); } Future _stop() async { await _audioPlayer.stop(); setState(() { _playerState = PlayerState.stopped; }); } @override void dispose() { super.dispose(); _audioPlayer.dispose(); } } ```

Affected platforms

Windows

Platform details

platform2:windows 11 23H2,operation system version:22631.4169

AudioPlayers Version

6.1.0

Build mode

release

Audio Files/URLs/Sources

No response

Screenshots

No response

Logs

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: UnimplementedError: Event Method does not exist audio.onCurrentPosition
#0      EventChannelAudioplayersPlatform.createEventStream.<anonymous closure> (package:audioplayers_platform_interface/src/audioplayers_platform.dart:273:13)
#1      _MapStream._handleData (dart:async/stream_pipe.dart:213:31)
#2      _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#3      _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#4      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:365:11)
#5      _DelayedData.perform (dart:async/stream_impl.dart:541:14)
#6      _PendingEvents.handleNext (dart:async/stream_impl.dart:646:11)
#7      _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:617:7)
#8      _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#9      _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)```

<details>
  <summary>Full Logs</summary>

  <!--   back to last page -->
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Bad state: No element
#0      Stream.firstWhere.<anonymous closure> (dart:async/stream.dart:1713:9)
#1      _RootZone.runGuarded (dart:async/zone.dart:1582:10)
#2      _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:418:13)
#3      _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:428:7)
#4      _BufferingStreamSubscription._close (dart:async/stream_impl.dart:317:7)
#5      _ForwardingStream._handleDone (dart:async/stream_pipe.dart:99:10)
#6      _ForwardingStreamSubscription._handleDone (dart:async/stream_pipe.dart:161:13)
#7      _RootZone.runGuarded (dart:async/zone.dart:1582:10)
#8      _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:418:13)
#9      _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:428:7)
#10     _BufferingStreamSubscription._close (dart:async/stream_impl.dart:317:7)
#11     _ForwardingStream._handleDone (dart:async/stream_pipe.dart:99:10)
#12     _ForwardingStreamSubscription._handleDone (dart:async/stream_pipe.dart:161:13)
#13     _RootZone.runGuarded (dart:async/zone.dart:1582:10)
#14     _DoneStreamSubscription._onMicrotask (dart:async/stream_impl.dart:777:15)
#15     _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#16     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)
  Flutter doctor:

Output of: flutter doctor -v


</details>

### Related issues / more information

_No response_

### Working on PR

no way
paladinghub commented 1 month ago

when i run the pub.dev-> Example,it has the same issue.Give me your help,thanks

palading123 commented 1 month ago

When i used another computer(Windows 11 professional edition) run the demo,bug information changed:【Error:flutter/shell/common/shell.cc(1055) the xyz.luan/audioplayers/events channel sent a message from native..But app has no crash. i guess the bug is related to the version of windows.the previous crash occurred on windows home edition.

palading123 commented 1 month ago

Is there anyone who can help me fix the bug? Thank you very much