Skip to content

Commit d247158

Browse files
committed
start to fix the sound length
1 parent 60a324b commit d247158

File tree

4 files changed

+112
-31
lines changed

4 files changed

+112
-31
lines changed

recipes/android/src/android_mixer.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,16 @@ def fadeout(self, time):
129129
self.stop()
130130

131131
def set_volume(self, left, right=None):
132-
# Not implemented.
133-
return
132+
sound.set_volume(self.id, left)
134133

135134
def get_volume(self):
136-
return 1.0
135+
return sound.get_volume(self.id)
136+
137+
def get_pos(self):
138+
return sound.get_pos(self.id)
139+
140+
def get_length(self):
141+
return sound.get_length(self.id)
137142

138143
def get_busy(self):
139144
return sound.busy(self.id)
@@ -214,10 +219,14 @@ def fadeout(self, time):
214219
self.stop()
215220

216221
def set_volume(self, left, right=None):
217-
return
222+
if self._channel is None:
223+
return
224+
self._channel.set_volume(left)
218225

219226
def get_volume(self):
220-
return 1.0
227+
if self._channel is None:
228+
return
229+
return self._channel.get_volume()
221230

222231
def get_num_channels(self):
223232
rv = 0
@@ -229,7 +238,14 @@ def get_num_channels(self):
229238
return rv
230239

231240
def get_length(self):
232-
return 1.0
241+
if self._channel is None:
242+
return 0
243+
return self._channel.get_length()
244+
245+
def get_pos(self):
246+
if self._channel is None:
247+
return -1
248+
return self._channel.get_pos()
233249

234250
music_channel = Channel(256)
235251
music_sound = None

recipes/android/src/android_sound.pyx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ cdef extern void android_sound_unpause(int)
99
cdef extern void android_sound_set_volume(int, float)
1010
cdef extern void android_sound_set_secondary_volume(int, float)
1111
cdef extern void android_sound_set_pan(int, float)
12+
cdef extern double android_sound_get_pos(int)
13+
cdef extern double android_sound_get_length(int)
1214

1315
cdef extern int android_sound_queue_depth(int)
1416

@@ -78,7 +80,7 @@ def busy(channel):
7880
return playing_name(channel) != None
7981

8082
def get_pos(channel):
81-
return 0
83+
return android_sound_get_pos(channel)
8284

8385
def set_volume(channel, volume):
8486
android_sound_set_volume(channel, volume)
@@ -96,6 +98,9 @@ def set_end_event(channel, event):
9698
def get_volume(channel):
9799
return volumes.get(channel, 1.0)
98100

101+
def get_length(channel):
102+
return android_sound_get_length(channel)
103+
99104
def init(freq, stereo, samples, status=False):
100105
return
101106

recipes/android/src/android_sound_jni.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,37 @@ void android_sound_unpause(int channel) {
248248
env, cls, mid,
249249
channel);
250250
}
251+
252+
double android_sound_get_pos(int channel) {
253+
static JNIEnv *env = NULL;
254+
static jclass *cls = NULL;
255+
static jmethodID mid = NULL;
256+
257+
if (env == NULL) {
258+
env = SDL_ANDROID_GetJNIEnv();
259+
aassert(env);
260+
cls = (*env)->FindClass(env, "org/renpy/android/RenPySound");
261+
aassert(cls);
262+
mid = (*env)->GetStaticMethodID(env, cls, "get_pos", "(I)D");
263+
aassert(mid);
264+
}
265+
266+
return (*env)->CallStaticDoubleMethod(env, cls, mid, channel);
267+
}
268+
269+
double android_sound_get_length(int channel) {
270+
static JNIEnv *env = NULL;
271+
static jclass *cls = NULL;
272+
static jmethodID mid = NULL;
273+
274+
if (env == NULL) {
275+
env = SDL_ANDROID_GetJNIEnv();
276+
aassert(env);
277+
cls = (*env)->FindClass(env, "org/renpy/android/RenPySound");
278+
aassert(cls);
279+
mid = (*env)->GetStaticMethodID(env, cls, "get_length", "(I)D");
280+
aassert(mid);
281+
}
282+
283+
return (*env)->CallStaticDoubleMethod(env, cls, mid, channel);
284+
}

src/src/org/renpy/android/RenPySound.java

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,39 @@
77
import java.util.HashMap;
88

99
public class RenPySound {
10-
10+
1111
private static class Channel implements MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener {
1212

1313
// MediaPlayers for the currently playing and queued up
1414
// sounds.
1515
MediaPlayer player[];
16-
16+
1717
// Filenames for the currently playing and queued up sounds.
1818
String filename[];
1919

2020
// Is the corresponding player prepareD?
2121
boolean prepared[];
22-
22+
2323
// The volume for the left and right channel.
2424
double volume;
2525
double secondary_volume;
2626
double left_volume;
2727
double right_volume;
28-
28+
double duration;
29+
2930
Channel() {
3031
player = new MediaPlayer[2];
3132
filename = new String[2];
3233
prepared = new boolean[2];
33-
34+
3435
player[0] = new MediaPlayer();
3536
player[1] = new MediaPlayer();
3637

3738
volume = 1.0;
3839
secondary_volume = 1.0;
3940
left_volume = 1.0;
40-
right_volume = 1.0;
41+
right_volume = 1.0;
42+
duration = 0.0;
4143
}
4244

4345
/**
@@ -50,7 +52,7 @@ synchronized void queue(String fn, String real_fn, long base, long length) {
5052

5153
try {
5254
FileInputStream f = new FileInputStream(real_fn);
53-
55+
5456
if (length >= 0) {
5557
mp.setDataSource(f.getFD(), base, length);
5658
} else {
@@ -59,18 +61,18 @@ synchronized void queue(String fn, String real_fn, long base, long length) {
5961

6062
mp.setOnCompletionListener(this);
6163
mp.setOnPreparedListener(this);
62-
64+
6365
mp.prepareAsync();
64-
66+
6567
f.close();
6668
} catch (IOException e) {
6769
Log.w("RenPySound", e);
6870
return;
6971
}
70-
7172

72-
filename[1] = fn;
73-
73+
74+
filename[1] = fn;
75+
7476
}
7577

7678
/**
@@ -80,26 +82,26 @@ synchronized void play() {
8082
MediaPlayer tmp;
8183

8284
player[0].reset();
83-
85+
8486
tmp = player[0];
8587
player[0] = player[1];
8688
player[1] = tmp;
87-
89+
8890
filename[0] = filename[1];
8991
filename[1] = null;
9092

9193
prepared[0] = prepared[1];
9294
prepared[1] = false;
93-
95+
9496
if (filename[0] != null) {
9597
updateVolume();
9698

97-
if (prepared[0]) {
99+
if (prepared[0]) {
98100
player[0].start();
99101
}
100102
}
101103
}
102-
104+
103105
/**
104106
* Stop playback on this channel.
105107
*/
@@ -161,7 +163,7 @@ synchronized void set_pan(float pan) {
161163
left_volume = 1.0 - pan;
162164
right_volume = 1.0;
163165
}
164-
166+
165167
updateVolume();
166168
}
167169

@@ -187,7 +189,7 @@ synchronized public void onPrepared(MediaPlayer mp) {
187189
prepared[1] = true;
188190
}
189191
}
190-
192+
191193
/**
192194
* Called on completion.
193195
*/
@@ -197,7 +199,21 @@ synchronized public void onCompletion(MediaPlayer mp) {
197199
}
198200
}
199201

200-
202+
synchronized public double get_pos() {
203+
if (filename[0] == null)
204+
return 0;
205+
return player[0].getCurrentPosition();
206+
}
207+
208+
synchronized public double get_length() {
209+
if (duration != 0)
210+
return duration;
211+
if (filename[0] == null)
212+
return 0;
213+
duration = player[0].getDuration() / 1000.;
214+
return duration;
215+
}
216+
201217
}
202218

203219
// A map from channel number to channel object.
@@ -218,13 +234,13 @@ static Channel getChannel(int num) {
218234
return rv;
219235
}
220236

221-
static void queue(int channel, String filename, String real_fn, long base, long length) {
237+
static void queue(int channel, String filename, String real_fn, long base, long length) {
222238
Channel c = getChannel(channel);
223239
c.queue(filename, real_fn, base, length);
224240
if (c.filename[0] == null) {
225241
c.play();
226242
}
227-
243+
228244
}
229245

230246
static void play(int channel,
@@ -291,8 +307,18 @@ static void unpause(int channel) {
291307
c.unpause();
292308
}
293309

310+
static double get_pos(int channel) {
311+
Channel c = getChannel(channel);
312+
return c.get_pos();
313+
}
314+
315+
static double get_length(int channel) {
316+
Channel c = getChannel(channel);
317+
return c.get_length();
318+
}
319+
294320
static {
295321
new MediaPlayer();
296322
}
297-
298-
}
323+
324+
}

0 commit comments

Comments
 (0)