Created
December 10, 2017 17:25
-
-
Save tito/8da5ee1d51cd81c556da947b225bcc2b to your computer and use it in GitHub Desktop.
Kivy Android audio player implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Kivy audio implementation for Android using native API | |
====================================================== | |
""" | |
from jnius import autoclass | |
from kivy.core.audio import Sound, SoundLoader | |
MediaPlayer = autoclass("android.media.MediaPlayer") | |
FileInputStream = autoclass("java.io.FileInputStream") | |
AudioManager = autoclass("android.media.AudioManager") | |
class SoundAndroidPlayer(Sound): | |
@staticmethod | |
def extensions(): | |
return ("mp3", "mp4", "aac", "3gp", "flac", "mkv", "wav", "ogg") | |
def __init__(self, **kwargs): | |
self._mediaplayer = None | |
super(SoundAndroidPlayer, self).__init__(**kwargs) | |
def load(self): | |
self.unload() | |
self._mediaplayer = MediaPlayer() | |
self._mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC); | |
self._mediaplayer.setDataSource(self.filename) | |
self._mediaplayer.prepare() | |
def unload(self): | |
self.stop() | |
self._mediaplayer = None | |
def play(self): | |
if not self._mediaplayer: | |
return | |
self._mediaplayer.start() | |
super(SoundAndroidPlayer, self).play() | |
def stop(self): | |
if not self._mediaplayer: | |
return | |
self._mediaplayer.reset() | |
def seek(self, position): | |
if not self._mediaplayer: | |
return | |
self._mediaplayer.seek(float(position)) | |
def get_pos(self): | |
if self._mediaplayer: | |
return self._mediaplayer.getCurrentPosition() / 1000. | |
return super(SoundAndroidPlayer, self).get_pos() | |
def on_volume(self, instance, volume): | |
if self._mediaplayer: | |
volume = float(volume) | |
self._mediaplayer.setVolume(volume, volume) | |
def _get_length(self): | |
if self._mediaplayer: | |
return self._mediaplayer.getDuration() / 1000. | |
return super(SoundAndroidPlayer, self)._get_length() | |
SoundLoader.register(SoundAndroidPlayer) |
mediaplayer has no seek(), it's seekTo()
when I am trying to run there is an error
Traceback (most recent call last):
File "kiv.py", line 10, in
MediaPlayer = autoclass("android.media.MediaPlayer")
File "build/bdist.linux-x86_64/egg/jnius/reflect.py", line 159, in autoclass
File "jnius/jnius_export_func.pxi", line 26, in jnius.find_javaclass
jnius.JavaException: Class not found 'android/media/MediaPlayer'how to fix this?
thanks in advance
it is supposed to run in Android ! if you run it in any other platform it will throw that error for sure
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
when I am trying to run there is an error
Traceback (most recent call last):
File "kiv.py", line 10, in
MediaPlayer = autoclass("android.media.MediaPlayer")
File "build/bdist.linux-x86_64/egg/jnius/reflect.py", line 159, in autoclass
File "jnius/jnius_export_func.pxi", line 26, in jnius.find_javaclass
jnius.JavaException: Class not found 'android/media/MediaPlayer'
how to fix this?
thanks in advance