Trouble using delegates/protocols

31 views
Skip to first unread message

Rasmus Kreiner

unread,
Sep 4, 2018, 5:37:23 PM9/4/18
to Kivy users support
Hi List,

I'm trying to use the AVAudioPlayerDelegate-protocol in an app. If I compare my method with the delgate.py file in the example directory (https://github.com/kivy/pyobjus/blob/master/examples/delegate.py) it looks like I'm doing exactly what i'm is supposed to, but somehow the two functions audioPlayerBeginInterruption and audioPlayerDidFinishPlaying are not called. Does any of you Hawkeye type of people see what seems to be the trouble:

from kivy.event import EventDispatcher
from kivy.properties import StringProperty, NumericProperty, OptionProperty, BooleanProperty
from kivy.clock import Clock
from pyobjus import autoclass, protocol
from pyobjus.dylib_manager import load_framework, INCLUDE

load_framework
(INCLUDE.AVFoundation)
AVAudioPlayer = autoclass("AVAudioPlayer")
AVAudioSession = autoclass("AVAudioSession")
MPRemoteCommandCenter = autoclass("MPRemoteCommandCenter")
MPRemoteCommand = autoclass("MPRemoteCommand")
MPNowPlayingInfoCenter = autoclass("MPNowPlayingInfoCenter")

NSURL
= autoclass("NSURL")
NSString = autoclass("NSString")
UIApplication = autoclass("UIApplication")


class SoundLoader(EventDispatcher):
 source
= StringProperty("")
 volume
= NumericProperty(1.)
 state
= OptionProperty('pause', options=('stop', 'play', 'pause'))
 loop
= BooleanProperty(False)
 _avplayer
= None
 _commandCenter
= None

 
def __init__(self, *args,**kwargs):
 
super(SoundLoader, self).__init__(**kwargs)
 
self.state = 'pause'
 
self.register_event_type('on_play')
 
self.register_event_type('on_stop')
 
self.register_event_type('on_pause')
 
self.register_event_type('on_finish')

 
def play(self):
 
self.state = 'play'
 
self._avplayer.prepareToPlay()
 
self._avplayer.play(2)
 
self.dispatch('on_play')

 
def stop(self):
 
if not self._avplayer:
 
return
 
print ("Trying to stop file: {}".format(self.source))
 
self.state = 'stop'
 
self._avplayer.stop()
 
self.dispatch('on_stop')

 
def pause(self):
 
self.state = 'pause'
 
self.stop()
 
self.dispatch('on_pause')

 
def load(self, filename):
 
self.unload()
 fn
= NSString.alloc().initWithUTF8String_(str(filename))
 url
= NSURL.alloc().initFileURLWithPath_(fn)
 
self._avplayer = AVAudioPlayer.alloc().initWithContentsOfURL_error_(
 url
, None)
 
self._avplayer.prepareToPlay()
 
self._avplayer.delegate = self._avplayer
 
UIApplication.sharedApplication().beginReceivingRemoteControlEvents()

# # TODO KOMMET HERTIL ---> Skal have fundet frem til addTargets og handler paa self._avplayer

 
def unload(self):
 
self.stop()
 
self._avplayer = None
 
try:
 
self.keepAliveThread.join()
 
self.killObject = False
 
except:
 
pass

 
def _get_length(self, dt=1):
 
if not self._avplayer:
 
return
 
self.length = self._avplayer.duration
 
return self.length

 
def get_pos(self, dt=1):
 
if self._avplayer.isPlaying():
 
self.currentPos = self._avplayer.currentTime
 
if self.currentPos > (self._get_length() - .1):
 
self.dispatch('on_finish')
 
return self.currentPos

 
def seek(self, position):
 
print position
 
self._avplayer.currentTime = position

 
def on_play(self):
 
self.state = "play"
 
self.clock = Clock.schedule_interval(self.get_pos,1)

 
def on_stop(self):
 
try: ##TODO TRACKING MECHANISM STOP
 
self.clock.cancel()
 
except:
 
pass
 
self.state = "stop"

 
def on_pause(self):
 
self.state = "pause"

 
def on_finish(self):
 
pass

 
@protocol('AVAudioPlayerDelegate')
 
def audioPlayerBeginInterruption(self):
 
print ("Im here at the protocol - Interrupted")

 
@protocol('AVAudioPlayerDelegate')
 
def audioPlayerDidFinishPlaying(self, successfully):
 
print ("Im here at the protocol - Finished ")
 
print successfully

class SoundSessionManager():
 
def __init__(self, *args, **kwargs):
 
self._avAudioSession = AVAudioSession.sharedInstance()
 category_playback
= NSString.alloc().initWithUTF8String_(str("AVAudioSessionCategoryPlayback"))
 
self._avAudioSession.setCategory_withOptions_error_(category_playback, 0, [])

Reply all
Reply to author
Forward
0 new messages