This repository was archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathandroid_media_player.py
66 lines (50 loc) · 2.01 KB
/
android_media_player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from kivy.logger import Logger
from jnius import autoclass, PythonJavaClass, java_method
MediaPlayer = autoclass('android.media.MediaPlayer')
KivyCompletionListener = autoclass('org.kivy.player.KivyCompletionListener')
Uri = autoclass('android.net.Uri')
class AndroidMediaPlayer():
# self.player.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK)
def init_player(self, service_callback):
self.callback_wrapper = CallbackWrapper(service_callback,
self.error_handler)
self.player = MediaPlayer()
self.player.setOnCompletionListener(
KivyCompletionListener(self.callback_wrapper))
def start(self, mActivity, uri):
try:
self.player.reset()
self.player.setDataSource(mActivity, Uri.parse(uri))
self.player.prepare()
self.player.start()
except Exception as e:
Logger.warning('Android Media Player prepare() failed.\n' +\
str(e))
def pause(self):
self.player.pause()
def resume(self):
self.player.start()
def stop(self):
self.player.stop()
def release(self):
self.player.release()
def error_handler(self):
Logger.warning('Android Media Player was reset due to an error.')
self.player.reset()
class CallbackWrapper(PythonJavaClass):
__javacontext__ = 'app'
__javainterfaces__ = ['org/kivy/player/CallbackWrapper']
def __init__(self, service_callback, error_callback):
super().__init__()
self.service_callback = service_callback
self.error_callback = error_callback
@java_method('()V')
def on_completion(self):
if self.service_callback:
self.service_callback()
@java_method('()V')
def on_error(self):
if self.error_callback:
self.error_callback()
if self.service_callback:
self.service_callback()