Skip to content

Commit 0dc46b5

Browse files
tctrdanoli3
authored andcommitted
ofAndroidSoundPlayer : removing unused funcntions in ofxAndroidSoundPlayer.h .cpp - adding a few modifications found in the iOS v12.0 version to the Java source
(cherry picked from commit 5c3f9de)
1 parent 9a748a1 commit 0dc46b5

3 files changed

Lines changed: 79 additions & 32 deletions

File tree

addons/ofxAndroid/Java/cc/openframeworks/OFAndroidSoundPlayer.java

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package cc.openframeworks;
22

3+
import android.media.AudioAttributes;
34
import android.media.AudioManager;
45
import android.media.MediaPlayer;
56
import android.media.SoundPool;
67
import android.util.FloatMath;
78
import android.util.Log;
89

9-
public class OFAndroidSoundPlayer extends OFAndroidObject{
10+
public class OFAndroidSoundPlayer extends OFAndroidObject implements MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener{
1011
OFAndroidSoundPlayer(){
1112
pan = 0.f;
1213
volume = leftVolume = rightVolume = 1;
@@ -18,15 +19,45 @@ public class OFAndroidSoundPlayer extends OFAndroidObject{
1819
streamID = -1;
1920
multiPlay = false;
2021
}
22+
23+
public void onDestroy() {
24+
stop();
25+
unloadSound();
26+
if (player != null) {player.stop(); player.release(); }
27+
if(pool != null) pool.release(); pool = null;
28+
if(attributes != null) attributes = null;
29+
}
30+
31+
protected void setContentType(int contentType){
32+
if(loop) contentType = AudioAttributes.CONTENT_TYPE_MUSIC;
33+
attributes = new AudioAttributes.Builder()
34+
.setUsage(AudioAttributes.USAGE_GAME)
35+
.setContentType(contentType)
36+
.build();
37+
38+
}
39+
40+
protected void createSoundPool(){
41+
if(pool == null) {
42+
pool = new SoundPool.Builder()
43+
.setAudioAttributes(attributes)
44+
.setMaxStreams(128)
45+
.build();
46+
}
47+
}
2148

2249
void loadSound(String fileName, boolean stream){
2350
try {
51+
setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION);
2452
if(stream){
2553
if(player!=null) unloadSound();
2654
player = new MediaPlayer();
55+
player.setOnErrorListener(this);
56+
player.setAudioAttributes(attributes);
2757
player.setDataSource(fileName);
2858
player.prepare();
2959
}else{
60+
createSoundPool();
3061
if(soundID!=-1) unloadSound();
3162
soundID = pool.load(fileName, 1);
3263
}
@@ -41,13 +72,14 @@ void loadSound(String fileName, boolean stream){
4172

4273
void unloadSound(){
4374
if(player!=null){
75+
player.stop();
4476
player.reset();
4577
player.release();
4678
player = null;
4779
}
4880

4981
if(soundID!=-1){
50-
pool.unload(soundID);
82+
if(pool != null) pool.unload(soundID);
5183
}
5284
fileName = null;
5385
soundID = -1;
@@ -59,9 +91,10 @@ void unloadSound(){
5991
void play(){
6092
if(stream){
6193
if(player==null) return;
62-
if(getIsPlaying()) setPosition(0);
94+
if(getIsPlaying() && getPosition() != 0) setPosition(0);
6395
player.start();
6496
}else{
97+
if(pool == null) return;
6598
if(!multiPlay){
6699
pool.stop(streamID);
67100
}
@@ -72,8 +105,13 @@ void play(){
72105
void stop(){
73106
if(stream){
74107
if(player==null) return;
75-
player.stop();
108+
if(player.isPlaying()) {
109+
//player.stop();
110+
player.pause();
111+
player.seekTo(0);
112+
}
76113
}else if(streamID!=-1){
114+
if(pool == null) return;
77115
pool.stop(streamID);
78116
bIsPlaying = false;
79117
}
@@ -98,7 +136,7 @@ void setVolume(float vol){
98136
if(stream){
99137
if(player!=null) player.setVolume(leftVolume, rightVolume);
100138
}else if(streamID!=-1){
101-
pool.setVolume(streamID, leftVolume, rightVolume);
139+
if(pool != null) pool.setVolume(streamID, leftVolume, rightVolume);
102140
}
103141
}
104142

@@ -116,7 +154,9 @@ void setSpeed(float spd){
116154
if(spd<0.5) spd = 0.5f;
117155
if(spd>2) spd=2;
118156
speed = spd;
119-
if(!stream && streamID!=-1) pool.setRate(streamID, spd);
157+
if(!stream && streamID!=-1) {
158+
if(pool != null) pool.setRate(streamID, spd);
159+
}
120160
}
121161

122162
void setPaused(boolean bP){
@@ -127,6 +167,7 @@ void setPaused(boolean bP){
127167
else
128168
player.start();
129169
}else if(streamID!=-1){
170+
if(pool == null) return;
130171
if(bP){
131172
pool.pause(streamID);
132173
}else{
@@ -141,7 +182,7 @@ void setLoop(boolean bLp){
141182
if(player!=null)
142183
player.setLooping(bLp);
143184
}else{
144-
if(streamID!=-1) pool.setLoop(streamID, -1);
185+
if(streamID!=-1 && pool != null) pool.setLoop(streamID, -1);
145186
}
146187
loop = bLp;
147188
}
@@ -157,22 +198,28 @@ void setMultiPlay(boolean bMp){
157198
}
158199

159200
void setPosition(float pct){
160-
if(stream && player!=null) player.seekTo((int) (player.getDuration()*pct)); // 0 = start, 1 = end;
201+
if(stream && player!=null && player.isPlaying()) {
202+
if(getPositionMS() != pct)
203+
player.seekTo((int) (player.getDuration() * pct)); // 0 = start, 1 = end;
204+
}
161205
}
162206

163207
void setPositionMS(int ms){
164-
if(stream && player!=null) player.seekTo(ms); // 0 = start, 1 = end;
208+
if(stream && player!=null && player.isPlaying()) {
209+
if(getPosition() != ms)
210+
player.seekTo(ms); // 0 = start, 1 = end;
211+
}
165212
}
166213

167214
float getPosition(){
168-
if(stream && player!=null)
215+
if(stream && player!=null && player.isPlaying())
169216
return ((float)player.getCurrentPosition())/(float)player.getDuration();
170217
else
171218
return 0;
172219
}
173220

174221
int getPositionMS(){
175-
if(stream && player!=null)
222+
if(stream && player!=null && player.isPlaying())
176223
return player.getCurrentPosition();
177224
else
178225
return 0;
@@ -206,23 +253,40 @@ protected void appPause() {
206253
unloadSound();
207254
fileName = currFileName;
208255
bIsLoaded = currIsLoaded;
256+
if(pool != null) pool.autoPause();
209257
}
210258

211259
@Override
212260
protected void appResume() {
213261
if(bIsLoaded){
214262
loadSound(fileName, stream);
215263
}
264+
if(pool != null) pool.autoResume();
216265
}
217266

218267
@Override
219268
protected void appStop() {
220269
appPause();
221270
}
222-
223-
271+
272+
@Override
273+
public void onPrepared(MediaPlayer mediaPlayer) {
274+
275+
}
276+
277+
@Override
278+
public boolean onError(MediaPlayer mp, int what, int extra) {
279+
// ... react appropriately ...
280+
// The MediaPlayer has moved to the Error state, must be reset!
281+
Log.w("OF", "onError The MediaPlayer has moved to the Error state, must be reset!");
282+
return true;
283+
}
284+
285+
286+
224287
private MediaPlayer player;
225-
private static SoundPool pool = new SoundPool(128, AudioManager.STREAM_MUSIC, 0);
288+
private static SoundPool pool = null;
289+
AudioAttributes attributes;
226290
private float pan;
227291
private float volume;
228292
private boolean bIsLoaded;

addons/ofxAndroid/src/ofxAndroidSoundPlayer.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ bool ofxAndroidSoundPlayer::load(const of::filesystem::path& fileName, bool stre
5656
return false;
5757
}
5858

59-
of::filesystem::path absolutePath = ofToDataPath(fileName,true);
60-
61-
jstring javaFileName = ofGetJNIEnv()->NewStringUTF(absolutePath.c_str());
59+
jstring javaFileName = ofGetJNIEnv()->NewStringUTF(ofToDataPath(fileName,true).c_str());
6260
env->CallVoidMethod(javaSoundPlayer,javaLoadMethod,javaFileName,stream?1:0);
6361
env->DeleteLocalRef((jobject)javaFileName);
6462
return true;
@@ -475,13 +473,3 @@ bool ofxAndroidSoundPlayer::isLoaded() const{
475473

476474
return env->CallBooleanMethod(javaSoundPlayer,javaIsLoadedMethod);
477475
}
478-
479-
void ofxAndroidSoundPlayer::audioIn(ofSoundBuffer &) const {
480-
481-
}
482-
483-
void ofxAndroidSoundPlayer::audioOut(ofSoundBuffer &) const {
484-
485-
}
486-
487-

addons/ofxAndroid/src/ofxAndroidSoundPlayer.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include "ofSoundBaseTypes.h"
44
#include <jni.h>
55

6-
class ofSoundBuffer;
7-
86
class ofxAndroidSoundPlayer: public ofBaseSoundPlayer{
97
public:
108
ofxAndroidSoundPlayer();
@@ -33,9 +31,6 @@ class ofxAndroidSoundPlayer: public ofBaseSoundPlayer{
3331
float getVolume() const;
3432
bool isLoaded() const;
3533

36-
void audioIn(ofSoundBuffer&) const;
37-
void audioOut(ofSoundBuffer&) const;
38-
3934
private:
4035
jobject javaSoundPlayer;
4136
jclass javaClass;

0 commit comments

Comments
 (0)