flutter_sound icon indicating copy to clipboard operation
flutter_sound copied to clipboard

[HELP]: Can't play file from seek position

Open RishiKar opened this issue 2 years ago • 5 comments

I need help for using flutter_sound: ^9.1.2

Here is my question :

I am playing a file that is being recorded and stored in the application's documents directory. Then I am using seekToPlayer to seek to a position set by user. All good until here. However, whenever I try to play/resume the file from the seek position, it always plays the file from the beginning. I have tried to use the example seek code as is and it still does not work. The only difference being I am playing the file not stored in assets.

Can anyone help me and tell me what am I missing here? Simplified and adapted code for help issue below:

Initialised with setSubscriptionDuration and listening to player progress

Future init() async { await _player.openPlayer(); await _player.setSubscriptionDuration(Duration(seconds: 1)); playerSubscription = _player.onProgress?.listen((event) { position = event.position.inSeconds; }); final session = await AudioSession.instance; //audio session config code }

Playing audio from file stored in local device

Future playUserAudio() async { final directory = await getApplicationDocumentsDirectory(); String appDocumentsPath = directory.path; audioFilePath = '$appDocumentsPath/None.aac'; await _player.startPlayer( fromURI: audioFilePath, codec: Codec.aacADTS, whenFinished: () { print('file playback complete'); });

Seeking to onChanged position of slider

Future seek(double seekPosition) async { await _player.seekToPlayer(Duration(milliseconds: seekPosition.floor())); position = seekPosition.floor(); }

Standard Pause, Resume and Stop function declarations

Now for the UI and triggering of the functions

IconButton( onPressed: () { if (_player.playing) { _player.stop(); } else { _player.playUserAudio(); }}, icon: Icon(_player.playing ? Icons.stop_circle : Icons.play_circle_fill, color: Color(0xFFC2CCF1), size: 60, )), Slider( value: _player.position.toDouble(), onChanged: ((value) { _player.seek(value); }), min: 0, max: 15, ),

Please note:

  • I have simplified and omitted some of the actual code that is using provider, callbacks etc
  • Also, I have tried using pause resume instead of simple play/stop
  • When seeked, while playing, the behavior observed is that it wants to update the state and start playback from start

RishiKar avatar Jun 17 '22 08:06 RishiKar

Seek into an audio file or into an audio buffer (i.e an asset) is probably not the problem. I am not sure that I read your code correctly, but it's possible that you seek into a pos < 15 milliseconds (almost the beginning).

Note: the seek position is reset to the beginning during the 'open' verb. If you don't reopen your player I don't see any reason your playback restart to the beginning.

Do you use the pause / resume verbs?

Larpoux avatar Jun 17 '22 09:06 Larpoux

@Larpoux Thanks for looking into my cry of help :) I am not opening the player again. It is only in the init() function. I did try pause and resume . They work perfectly but when seeked to a new position , it always resumes/plays from the beginning of the file. I have logged the position of the slider, and do not think pos to the beginning is the issue. Can you please help me understand something?

My understanding of the api is that

  • I can play/resume a file
  • I can stop/pause a file
  • I can seek to a new position (seek by itself does not call the play/resume verbs)
  • I can press play again and it will start playing from that position because seek was used

RishiKar avatar Jun 17 '22 10:06 RishiKar

My understanding of the api is that

  • I can play/resume a file
  • I can stop/pause a file
  • I can seek to a new position (seek by itself does not call the play/resume verbs)
  • I can press play again and it will start playing from that position because seek was used

This is correct. It should work.

Larpoux avatar Jun 17 '22 15:06 Larpoux

Maybe you recall 'startPlayer' after your seek. Do not recall 'startPlayer'. Only

  • seekPlayer
  • pause
  • resume

Perhaps there is a bug if you call seek during a pause mode. I have never tried this case.

Larpoux avatar Jun 17 '22 15:06 Larpoux

@Larpoux please give me a few more days to confirm if it is a bug or not. For now, I got everything working using audio player package but I would like it to also work with Fluttersound as it is a very important package for the flutter dev community

RishiKar avatar Jun 28 '22 12:06 RishiKar

This issue is stale because it has been open 90 days with no activity. Leave a comment or this will be closed in 7 days.

github-actions[bot] avatar Dec 03 '23 01:12 github-actions[bot]