Audio Seek and get position work around

148 views
Skip to first unread message
Assigned to rasmus....@gmail.com by me

Rasmus Kreiner

unread,
Aug 7, 2018, 12:56:19 AM8/7/18
to kivy-...@googlegroups.com
Hi All,

I’m writing an interactive podcast application. Everything works fine on macOS (using gstream), but when compiling to Android I discovered that the get_pos, seek  and get_length methods do not work.
I saw that Tyler posted a workaround for Android, which I think I’m able to adapt to, but what about iOS? Does this also have the same set back as on Android? If yes, is there someone here that have a workaround for iOS?

Thanks!

Best,
Rasmus

Rasmus Kreiner

unread,
Aug 13, 2018, 6:03:54 PM8/13/18
to Kivy users support
Hi all,

Just a small update to bump the post. I did succes in using pyjnius and Androids native Mediaplayer. That said I'm still stuck with no solution on iOS. And so to repeat the question: Is there a possibility to get this to work on iOS? Or am I forced to write a native language app (using swift or obj-c) in order to retrieve these functions?

Best,
Rasmus

Tyler

unread,
Aug 15, 2018, 9:23:41 AM8/15/18
to Kivy users support
Hi, I haven't done audio in iOS, but it seems that AVPlayer is already supported in default kivy audio providers, it's implemented through pyobjus library and has full media transport control (including seek and get_pos). Try it out and if it works, there's no need for obj-c hassle.

Rasmus Kreiner

unread,
Aug 15, 2018, 11:26:31 AM8/15/18
to kivy-...@googlegroups.com
Excellent news! Will most definitely give it try.

Thanks!

This message was send from a mobile device)
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To post to this group, send email to kivy-...@googlegroups.com.
Visit this group at https://groups.google.com/group/kivy-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/effc3bc7-4912-461d-82f5-155851315e76%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rasmus Kreiner

unread,
Aug 17, 2018, 8:15:28 AM8/17/18
to Kivy users support
This definitely seems like a path to pursuit. That said I'm struggling to keep a reference to the AVAudioPlayer instance in the code beneath. If I run this I'm able to get the sound to play, but if I loose the last while loop the AVAudioPlayer nstance will return None just after being instantiated. I suppose that this has to deal with the Object C's automatic reference counting, but I'm not entirely sure. Does anyone know what is causing this? And hopefully how to to solve it?

Best,
Rasmus

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

load_framework
(INCLUDE.AVFoundation)
AVAudioPlayer = autoclass("AVAudioPlayer")
NSURL
= autoclass("NSURL")
NSString = autoclass("NSString")

class SoundLoader(EventDispatcher):
 source
= StringProperty("")

 volume
= NumericProperty(1.)

 state
= OptionProperty('pause', options=('stop', 'play', 'pause'))

 loop
= BooleanProperty(False)
 _avplayer
= 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()
 
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_(filename) ##TODO HVORFOR RETURNERE DENNE NIL?
 url
= NSURL.alloc().initFileURLWithPath_(fn)
 
self._avplayer = AVAudioPlayer.alloc().initWithContentsOfURL_error_(
 url
, None)

 
def unload(self):
 
self.stop()
 
self._avplayer = None

 
def _get_length(self):
 
self.length = self._avplayer.duration
 
return self.length

 
def get_pos(self, dt=1):
 
if self._avplayer.isPlaying():
 
self.currentPos = self._avplayer.currentTime
 
return self.currentPos

 
def seek(self, position):
 
if not self._avplayer:
 
return
 
self._avplayer.playAtTime_(float(position))

 
def on_play(self):
 
print("Playing")
 
self.state = "play"

 
def on_stop(self):
 
print("Stopping")
 
self.state = "stop"

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

 
def on_finish(self):
 
print("Finishing!")
 
# self.stop()

if __name__ == '__main__':
 test
= SoundLoader()
 test
.load("Podcasts/Audio/QuestionsGeneric/Q_BackgroundMusic.m4a")
 test
.play()
 
while True:
 test
.get_pos()





onsdag den 15. august 2018 kl. 17.26.31 UTC+2 skrev Rasmus Kreiner:
Excellent news! Will most definitely give it try.

Thanks!

This message was send from a mobile device)

Den 15. aug. 2018 kl. 15.23 skrev Tyler <griml...@gmail.com>:

Hi, I haven't done audio in iOS, but it seems that AVPlayer is already supported in default kivy audio providers, it's implemented through pyobjus library and has full media transport control (including seek and get_pos). Try it out and if it works, there's no need for obj-c hassle.

On Tuesday, August 14, 2018 at 12:03:54 AM UTC+2, Rasmus Kreiner wrote:
Hi all,

Just a small update to bump the post. I did succes in using pyjnius and Androids native Mediaplayer. That said I'm still stuck with no solution on iOS. And so to repeat the question: Is there a possibility to get this to work on iOS? Or am I forced to write a native language app (using swift or obj-c) in order to retrieve these functions?

Best,
Rasmus

tirsdag den 7. august 2018 kl. 06.56.19 UTC+2 skrev Rasmus Kreiner:
Hi All,

I’m writing an interactive podcast application. Everything works fine on macOS (using gstream), but when compiling to Android I discovered that the get_pos, seek  and get_length methods do not work.
I saw that Tyler posted a workaround for Android, which I think I’m able to adapt to, but what about iOS? Does this also have the same set back as on Android? If yes, is there someone here that have a workaround for iOS?

Thanks!

Best,
Rasmus

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+unsubscribe@googlegroups.com.

Alexander Ko

unread,
Aug 24, 2020, 8:27:54 PM8/24/20
to Kivy users support
Did you find an answer to this? I'm trying to use AVPlayer for a podcast app in iOS too, and it seems that the problem is that in seek(), self._avplayer.playAtTime_() doesn't work.

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages