Skip to content

Commit 8cb4eff

Browse files
committed
seek and get_length support for mediaplayer
1 parent 6d4aded commit 8cb4eff

File tree

4 files changed

+152
-36
lines changed

4 files changed

+152
-36
lines changed

recipes/android/src/android_mixer.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ def play(self, s, loops=0, maxtime=0, fade_ms=0):
113113
with condition:
114114
condition.notify()
115115

116+
def seek(self, position):
117+
sound.seek(self.id, position)
116118

117119
def stop(self):
118120
self.loop = None
@@ -159,6 +161,12 @@ def queue(self, s):
159161
def get_queue(self):
160162
return self.queued
161163

164+
def get_pos(self):
165+
return sound.get_pos(self.id)/1000.
166+
167+
def get_length(self):
168+
return sound.get_length(self.id)/1000.
169+
162170

163171
def Channel(n):
164172
"""
@@ -205,6 +213,7 @@ def play(self, loops=0, maxtime=0, fade_ms=0):
205213
channel.play(self, loops=loops)
206214
return channel
207215

216+
208217
def stop(self):
209218
for i in range(0, num_channels):
210219
if Channel(i).get_sound() is self:
@@ -254,6 +263,10 @@ def play(loops=0, start=0.0):
254263
def rewind():
255264
music_channel.play(music_sound)
256265

266+
@staticmethod
267+
def seek(position):
268+
music_channel.seek(position)
269+
257270
@staticmethod
258271
def stop():
259272
music_channel.stop()
@@ -284,7 +297,7 @@ def get_busy():
284297

285298
@staticmethod
286299
def get_pos():
287-
return 0
300+
return music_channel.get_pos()
288301

289302
@staticmethod
290303
def queue(filename):

recipes/android/src/android_sound.pyx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
cdef extern void android_sound_queue(int, char *, char *, long long, long long)
22
cdef extern void android_sound_play(int, char *, char *, long long, long long)
33
cdef extern void android_sound_stop(int)
4+
cdef extern void android_sound_seek(int, float)
45
cdef extern void android_sound_dequeue(int)
56
cdef extern void android_sound_playing_name(int, char *, int)
67
cdef extern void android_sound_pause(int)
@@ -11,15 +12,17 @@ cdef extern void android_sound_set_secondary_volume(int, float)
1112
cdef extern void android_sound_set_pan(int, float)
1213

1314
cdef extern int android_sound_queue_depth(int)
15+
cdef extern int android_sound_get_pos(int)
16+
cdef extern int android_sound_get_length(int)
1417

1518
channels = set()
1619
volumes = { }
1720

1821
def queue(channel, file, name, fadein=0, tight=False):
1922

2023
channels.add(channel)
21-
22-
real_fn = file.name
24+
25+
real_fn = file.name
2326
base = getattr(file, "base", -1)
2427
length = getattr(file, "length", -1)
2528

@@ -28,13 +31,16 @@ def queue(channel, file, name, fadein=0, tight=False):
2831
def play(channel, file, name, paused=False, fadein=0, tight=False):
2932

3033
channels.add(channel)
31-
34+
3235
real_fn = file.name
3336
base = getattr(file, "base", -1)
3437
length = getattr(file, "length", -1)
3538

3639
android_sound_play(channel, name, real_fn, base, length)
3740

41+
def seek(channel, position):
42+
android_sound_seek(channel, position)
43+
3844
def stop(channel):
3945
android_sound_stop(channel)
4046

@@ -70,20 +76,23 @@ def unpause_all():
7076
def pause_all():
7177
for i in channels:
7278
pause(i)
73-
79+
7480
def fadeout(channel, ms):
7581
stop(channel)
7682

7783
def busy(channel):
7884
return playing_name(channel) != None
7985

8086
def get_pos(channel):
81-
return 0
87+
return android_sound_get_pos(channel)
88+
89+
def get_length(channel):
90+
return android_sound_get_length(channel)
8291

8392
def set_volume(channel, volume):
8493
android_sound_set_volume(channel, volume)
8594
volumes[channel] = volume
86-
95+
8796
def set_secondary_volume(channel, volume):
8897
android_sound_set_secondary_volume(channel, volume)
8998

recipes/android/src/android_sound_jni.c

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ void android_sound_queue(int channel, char *filename, char *real_fn, long long b
2525
}
2626

2727
PUSH_FRAME;
28-
28+
2929
(*env)->CallStaticVoidMethod(
3030
env, cls, mid,
3131
channel,
3232
(*env)->NewStringUTF(env, filename),
3333
(*env)->NewStringUTF(env, real_fn),
3434
(jlong) base,
35-
(jlong) length);
35+
(jlong) length);
3636

3737
POP_FRAME;
3838
}
@@ -41,7 +41,7 @@ void android_sound_play(int channel, char *filename, char *real_fn, long long ba
4141
static JNIEnv *env = NULL;
4242
static jclass *cls = NULL;
4343
static jmethodID mid = NULL;
44-
44+
4545
if (env == NULL) {
4646
env = SDL_ANDROID_GetJNIEnv();
4747
aassert(env);
@@ -52,7 +52,7 @@ void android_sound_play(int channel, char *filename, char *real_fn, long long ba
5252
}
5353

5454
PUSH_FRAME;
55-
55+
5656
(*env)->CallStaticVoidMethod(
5757
env, cls, mid,
5858
channel,
@@ -64,6 +64,26 @@ void android_sound_play(int channel, char *filename, char *real_fn, long long ba
6464
POP_FRAME;
6565
}
6666

67+
void android_sound_seek(int channel, float position){
68+
static JNIEnv *env = NULL;
69+
static jclass *cls = NULL;
70+
static jmethodID mid = NULL;
71+
72+
if (env == NULL) {
73+
env = SDL_ANDROID_GetJNIEnv();
74+
aassert(env);
75+
cls = (*env)->FindClass(env, "org/renpy/android/RenPySound");
76+
aassert(cls);
77+
mid = (*env)->GetStaticMethodID(env, cls, "seek", "(IF)V");
78+
aassert(mid);
79+
}
80+
81+
(*env)->CallStaticVoidMethod(
82+
env, cls, mid,
83+
channel,
84+
(jfloat) position);
85+
}
86+
6787
void android_sound_stop(int channel) {
6888
static JNIEnv *env = NULL;
6989
static jclass *cls = NULL;
@@ -128,7 +148,7 @@ void android_sound_playing_name(int channel, char *buf, int buflen) {
128148

129149
jobject s = NULL;
130150
char *jbuf;
131-
151+
132152
if (env == NULL) {
133153
env = SDL_ANDROID_GetJNIEnv();
134154
aassert(env);
@@ -139,7 +159,7 @@ void android_sound_playing_name(int channel, char *buf, int buflen) {
139159
}
140160

141161
PUSH_FRAME;
142-
162+
143163
s = (*env)->CallStaticObjectMethod(
144164
env, cls, mid,
145165
channel);
@@ -248,3 +268,41 @@ void android_sound_unpause(int channel) {
248268
env, cls, mid,
249269
channel);
250270
}
271+
272+
int android_sound_get_pos(int channel) {
273+
static JNIEnv *env = NULL;
274+
static jclass *cls = NULL;
275+
static jmethodID mid = NULL;
276+
277+
if (env == NULL) {
278+
env = SDL_ANDROID_GetJNIEnv();
279+
aassert(env);
280+
cls = (*env)->FindClass(env, "org/renpy/android/RenPySound");
281+
aassert(cls);
282+
mid = (*env)->GetStaticMethodID(env, cls, "get_pos", "(I)I");
283+
aassert(mid);
284+
}
285+
286+
(*env)->CallStaticIntMethod(
287+
env, cls, mid,
288+
channel);
289+
}
290+
291+
int android_sound_get_length(int channel) {
292+
static JNIEnv *env = NULL;
293+
static jclass *cls = NULL;
294+
static jmethodID mid = NULL;
295+
296+
if (env == NULL) {
297+
env = SDL_ANDROID_GetJNIEnv();
298+
aassert(env);
299+
cls = (*env)->FindClass(env, "org/renpy/android/RenPySound");
300+
aassert(cls);
301+
mid = (*env)->GetStaticMethodID(env, cls, "get_length", "(I)I");
302+
aassert(mid);
303+
}
304+
305+
(*env)->CallStaticIntMethod(
306+
env, cls, mid,
307+
channel);
308+
}

0 commit comments

Comments
 (0)