Skip to content

Commit

Permalink
Fixes avplayer seek and state on iOS (not tested on OSX)
Browse files Browse the repository at this point in the history
The previous implementation was using playAtTime_, but 100% sure it was
not working as playAtTime_ schedules the audio to play at a specific
time, not to seek within the current audio file. To seek to a specific
position in the audio, we should use the currentTime property of
AVAudioPlayer.

In addition, i added a delegate to correctly catch when the sound is
stopping when reaching eof, which make the state property correctly
work.
  • Loading branch information
Mathieu Virbel authored and Mathieu Virbel committed May 26, 2024
1 parent 3223256 commit 346cbce
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions kivy/core/audio/audio_avplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
__all__ = ('SoundAvplayer', )

from kivy.core.audio import Sound, SoundLoader
from pyobjus import autoclass
from pyobjus import autoclass, protocol
from pyobjus.dylib_manager import load_framework, INCLUDE

load_framework(INCLUDE.AVFoundation)
Expand Down Expand Up @@ -40,19 +40,25 @@ def unload(self):
def play(self):
if not self._avplayer:
return
self._avplayer.delegate = self
self._avplayer.play()
super(SoundAvplayer, self).play()

def stop(self):
if not self._avplayer:
return
self._avplayer.delegate = None
self._avplayer.stop()
super(SoundAvplayer, self).stop()

def seek(self, position):
if not self._avplayer:
return
self._avplayer.playAtTime_(float(position))
avplayer = self._avplayer
avplayer.stop()
avplayer.currentTime = float(position)
if self.state == 'play':
avplayer.play()

def get_pos(self):
if self._avplayer:
Expand All @@ -68,5 +74,9 @@ def _get_length(self):
return self._avplayer.duration
return super(SoundAvplayer, self)._get_length()

@protocol("AVAudioPlayerDelegate")
def audioPlayerDidFinishPlaying_successfully_(self, player, flag):
self.stop()


SoundLoader.register(SoundAvplayer)

0 comments on commit 346cbce

Please sign in to comment.