Skip to content

Commit 0598e62

Browse files
committed
HXSound: Added new load() method for pre-loading sound resources into HXSound. Resolved reinitialization issues for Gingerbread devices. Renamed several method names in HXMusic() and HXSound().
1 parent ead2c04 commit 0598e62

11 files changed

Lines changed: 179 additions & 93 deletions

File tree

README.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ HXMusic.music()
3131
.play(this); // Plays the song. [REQUIRED]
3232
```
3333

34-
#### Play Music (URL):
34+
#### Play Music (Path):
3535

3636
```
3737
HXMusic.music()
38-
.load("http://some-fake-url.com/song.mp3") // Sets the URL location of the song. [REQUIRED]
38+
.load("http://some-fake-url.com/song.mp3") // Sets the path location of the song. [REQUIRED]
3939
.title("My Awesome Song") // Sets the title of the song. [OPTIONAL]
4040
.artist("Mr. Anonymous") // Sets the artist of the song. [OPTIONAL]
4141
.date("January 1, 1998") // Sets the date of the song. [OPTIONAL]
@@ -48,19 +48,19 @@ HXMusic.music()
4848
#### Pause Music:
4949

5050
```
51-
HXMusic.pauseMusic(); // Pauses any song that is playing in the background.
51+
HXMusic.pause(); // Pauses any song that is playing in the background.
5252
```
5353

5454
#### Resume Music:
5555

5656
```
57-
HXMusic.resumeMusic(this); // Resumes playback of the last played song at the position where it left off.
57+
HXMusic.resume(this); // Resumes playback of the last played song at the position where it left off.
5858
```
5959

6060
#### Stop Music:
6161

6262
```
63-
HXMusic.stopMusic(); // Stops all music playing in the background.
63+
HXMusic.stop(); // Stops all music playing in the background.
6464
```
6565

6666
#### Song Playing
@@ -124,15 +124,22 @@ HXSound.sound()
124124
#### Pause Sound:
125125

126126
```
127-
HXSound.pauseSounds(); // Pauses all looping sounds playing in the background.
127+
HXSound.pause(); // Pauses all looping sounds playing in the background.
128128
```
129129

130130
#### Resume Sound:
131131

132132
```
133-
HXSound.resumeSounds(); // Resumes playback of all looping sounds previously played in the background.
133+
HXSound.resume(); // Resumes playback of all looping sounds previously played in the background.
134134
```
135135

136+
#### Load Sound:
137+
138+
```
139+
HXSound.load(); // Pre-loads a list of song resources into HXSound.
140+
```
141+
142+
136143
#### Enable Multiple Sound Engines:
137144

138145
```
@@ -148,7 +155,7 @@ HXSound.enable(true); // Enables/disables sound playback.
148155
#### Re-Initialize Sound:
149156

150157
```
151-
HXSound.reinitialize(); // Manual re-initialization of sound engines. Only for Android API 9 - 10.
158+
HXSound.reinitialize(this); // Manual re-initialization of sound engines. Only for Android API 9 - 10.
152159
```
153160

154161
#### Logging:

apk/HXAudioPlayer_Demo.apk

32 KB
Binary file not shown.

app/src/main/java/com/huhx0015/hxaudiodemo/activity/HXAudioPlayerDemoActivity.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class HXAudioPlayerDemoActivity extends AppCompatActivity implements HXMu
3636
protected void onCreate(Bundle savedInstanceState) {
3737
super.onCreate(savedInstanceState);
3838

39+
HXMusic.logging(true); // Enables logging for HXAudio.
3940
loadPreferences(); // Loads the settings values from the main SharedPreferences object.
4041
initView(); // Sets up layout for the activity.
4142
HXMusic.setListener(this); // Sets the HXMusicListener interface to this activity.
@@ -49,7 +50,7 @@ protected void onResume() {
4950

5051
// Checks to see if songs were playing in the background previously; this call resumes
5152
// the audio playback.
52-
HXMusic.resumeMusic(this);
53+
HXMusic.resume(this);
5354
HXAudioPlayerUtils.enableSystemSound(false, this); // Temporarily disables the physical button's sound effects.
5455
}
5556

@@ -60,7 +61,7 @@ protected void onPause(){
6061
super.onPause();
6162

6263
HXAudioPlayerUtils.enableSystemSound(true, this); // Re-enables the physical button's sound effects.
63-
HXMusic.pauseMusic(); // Pauses any song that is playing in the background.
64+
HXMusic.pause(); // Pauses any song that is playing in the background.
6465
}
6566

6667
// onStop(): This function runs when screen is no longer visible and the activity is in a
@@ -70,7 +71,7 @@ protected void onStop() {
7071
super.onStop();
7172

7273
// Refreshes the SoundPool object for Android 2.3 (GINGERBREAD) devices.
73-
HXSound.reinitialize();
74+
HXSound.reinitialize(this);
7475
}
7576

7677
// onDestroy(): This function runs when the activity has terminated and is being destroyed.
@@ -136,13 +137,13 @@ public void onMusicCompletion(HXMusicItem music) {
136137
@Override
137138
public void onMusicBufferingUpdate(HXMusicItem music, int percent) {}
138139

139-
// onMusicPause(): Called when HXMusic's pauseMusic() method has been called.
140+
// onMusicPause(): Called when HXMusic's pause() method has been called.
140141
@Override
141142
public void onMusicPause(HXMusicItem music) {
142143
Toast.makeText(this, "MUSIC PAUSED: " + music.getMusicTitle(), Toast.LENGTH_SHORT).show();
143144
}
144145

145-
// onMusicStop(): Called when HXMusic's stopMusic() method has been called.
146+
// onMusicStop(): Called when HXMusic's stop() method has been called.
146147
@Override
147148
public void onMusicStop(HXMusicItem music) {
148149
Toast.makeText(this, "MUSIC STOPPED: " + music.getMusicTitle(), Toast.LENGTH_SHORT).show();
@@ -304,7 +305,7 @@ public void onClick(View v) {
304305

305306
// Plays the last selected song.
306307
else {
307-
HXMusic.resumeMusic(HXAudioPlayerDemoActivity.this);
308+
HXMusic.resume(HXAudioPlayerDemoActivity.this);
308309
}
309310
}
310311
});
@@ -317,7 +318,7 @@ public void onClick(View v) {
317318

318319
// Pauses the song that is currently playing in the background.
319320
if (HXMusic.isPlaying()) {
320-
HXMusic.pauseMusic();
321+
HXMusic.pause();
321322
}
322323
}
323324
});
@@ -330,7 +331,7 @@ public void onClick(View v) {
330331

331332
// Stops the song that is currently playing in the background.
332333
if (HXMusic.isPlaying()) {
333-
HXMusic.stopMusic();
334+
HXMusic.stop();
334335
currentSong = "NONE"; // Indicates no song has been selected.
335336
toggleStar(0); // Updates the star song toggles.
336337
}
@@ -351,7 +352,7 @@ public void onClick(View v) {
351352

352353
// Stops song playback if song is currently playing.
353354
if (HXMusic.isPlaying()) {
354-
HXMusic.stopMusic();
355+
HXMusic.stop();
355356
currentSong = "NONE"; // Indicates no song has been selected.
356357
toggleStar(0); // Updates the song star toggles.
357358
}
@@ -390,7 +391,7 @@ public void onClick(View v) {
390391
else {
391392

392393
// Refreshes the SoundPool object for Android 2.3 (GINGERBREAD) devices.
393-
HXSound.reinitialize();
394+
HXSound.reinitialize(HXAudioPlayerDemoActivity.this);
394395

395396
soundOn = true;
396397
soundEnableButton.setText("SOUND ON");

hxaudio/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
defaultConfig {
88
minSdkVersion 9
99
targetSdkVersion 25
10-
versionCode 8
11-
versionName "3.1.6"
10+
versionCode 9
11+
versionName "3.1.7"
1212
}
1313
buildTypes {
1414
release {

hxaudio/src/main/java/com/huhx0015/hxaudio/audio/HXMusic.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public synchronized void initMusic(HXMusicItem music, int position, boolean isGa
9797
private synchronized boolean checkStatus(HXMusicItem music) {
9898

9999
if (!isEnabled) {
100-
HXLog.e(LOG_TAG, "ERROR: checkStatus(): Music has been currently disabled.");
100+
HXLog.d(LOG_TAG, "DISABLED: checkStatus(): Music has been currently disabled.");
101101
return false;
102102
} else if (music == null) {
103103
HXLog.e(LOG_TAG, "ERROR: checkStatus(): Music item was null.");
@@ -107,7 +107,7 @@ private synchronized boolean checkStatus(HXMusicItem music) {
107107
return false;
108108
} else if (hxMusicItem != null && (hxMusicItem.getMusicResource() == music.getMusicResource())) {
109109
if (hxMusicEngine != null && hxMusicEngine.isPlaying()) {
110-
HXLog.e(LOG_TAG, "ERROR: checkStatus(): Specified song is already playing!");
110+
HXLog.d(LOG_TAG, "PLAYING: checkStatus(): Specified song is already playing!");
111111
return false;
112112
}
113113
}
@@ -152,7 +152,7 @@ public void onMusicEngineBufferingUpdate(int percent) {
152152
}
153153
}
154154

155-
// onMusicEnginePause(): Called when HXMusicEngine's pauseMusic() method has been called.
155+
// onMusicEnginePause(): Called when HXMusicEngine's pause() method has been called.
156156
@Override
157157
public void onMusicEnginePause() {
158158
hxMusic.hxMusicStatus = HXMusicStatus.PAUSED; // Indicates that the music is currently paused.
@@ -163,7 +163,7 @@ public void onMusicEnginePause() {
163163
}
164164
}
165165

166-
// onMusicStop(): Called when HXMusicEngine's stopMusic() method has been called.
166+
// onMusicStop(): Called when HXMusicEngine's stop() method has been called.
167167
@Override
168168
public void onMusicEngineStop() {
169169
hxMusic.hxMusicStatus = HXMusicStatus.STOPPED;
@@ -181,18 +181,18 @@ public static boolean isPlaying() {
181181
return hxMusic.hxMusicEngine != null && hxMusic.hxMusicEngine.isPlaying();
182182
}
183183

184-
// pauseMusic(): Pauses any music playing in the background.
185-
public static void pauseMusic() {
184+
// pause(): Pauses any music playing in the background.
185+
public static void pause() {
186186
if (hxMusic != null && hxMusic.hxMusicEngine != null) {
187187
hxMusic.musicPosition = hxMusic.hxMusicEngine.pauseMusic();
188188
}
189189
}
190190

191-
// resumeMusic(): Resumes playback of the current music.
192-
public static void resumeMusic(final Context context) {
191+
// resume(): Resumes playback of the current music.
192+
public static void resume(final Context context) {
193193

194194
if (context == null || context.getApplicationContext() == null) {
195-
HXLog.e(LOG_TAG, "ERROR: resumeMusic(): Context cannot be null.");
195+
HXLog.e(LOG_TAG, "ERROR: resume(): Context cannot be null.");
196196
} else if (hxMusic != null && hxMusic.hxMusicStatus.equals(HXMusicStatus.PAUSED) &&
197197
hxMusic.hxMusicEngine != null) {
198198
Thread playThread = new Thread(new Runnable() {
@@ -205,16 +205,16 @@ public void run() {
205205
});
206206
playThread.start();
207207
} else {
208-
HXLog.e(LOG_TAG, "ERROR: resumeMusic(): Music could not be resumed.");
208+
HXLog.e(LOG_TAG, "ERROR: resume(): Music could not be resumed.");
209209
}
210210
}
211211

212-
// stopMusic(): Stops any music playing in the background.
213-
public static void stopMusic() {
212+
// stop(): Stops any music playing in the background.
213+
public static void stop() {
214214
if (hxMusic != null && hxMusic.hxMusicEngine != null) {
215215
hxMusic.hxMusicEngine.stopMusic();
216216
} else {
217-
HXLog.e(LOG_TAG, "ERROR: stopMusic(): Music could not be stopped.");
217+
HXLog.e(LOG_TAG, "ERROR: stop(): Music could not be stopped.");
218218
}
219219
}
220220

hxaudio/src/main/java/com/huhx0015/hxaudio/audio/HXMusicEngine.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ private synchronized void removeNextMediaPlayer() {
189189
currentPlayer.setNextMediaPlayer(null);
190190
nextPlayer = null;
191191
} catch (Exception e) {
192-
HXLog.e(LOG_TAG, "ERROR: pauseMusic(): " + e.getLocalizedMessage());
192+
HXLog.e(LOG_TAG, "ERROR: pause(): " + e.getLocalizedMessage());
193193
}
194194
}
195195
}
@@ -252,7 +252,7 @@ boolean isPlaying() {
252252
return currentPlayer != null && currentPlayer.isPlaying();
253253
}
254254

255-
// pauseMusic(): Pauses any music playing in the background.
255+
// pause(): Pauses any music playing in the background.
256256
int pauseMusic() {
257257

258258
// Checks to see if the MediaPlayer object has been initialized first before retrieving the
@@ -272,12 +272,12 @@ int pauseMusic() {
272272
musicEngineListener.onMusicEnginePause();
273273
}
274274

275-
HXLog.d(LOG_TAG, "MUSIC: pauseMusic(): Music playback has been paused.");
275+
HXLog.d(LOG_TAG, "MUSIC: pause(): Music playback has been paused.");
276276
return musicPosition;
277277
}
278278
}
279279

280-
HXLog.e(LOG_TAG, "ERROR: pauseMusic(): Music could not be paused.");
280+
HXLog.e(LOG_TAG, "ERROR: pause(): Music could not be paused.");
281281
return 0;
282282
}
283283

@@ -297,7 +297,7 @@ synchronized boolean release() {
297297
}
298298
}
299299

300-
// stopMusic(): Stops any music playing in the background.
300+
// stop(): Stops any music playing in the background.
301301
boolean stopMusic() {
302302

303303
if (currentPlayer != null) {
@@ -309,10 +309,10 @@ boolean stopMusic() {
309309
musicEngineListener.onMusicEngineStop();
310310
}
311311

312-
HXLog.d(LOG_TAG, "MUSIC: stopMusic(): Music playback has been stopped.");
312+
HXLog.d(LOG_TAG, "MUSIC: stop(): Music playback has been stopped.");
313313
return true;
314314
} else {
315-
HXLog.e(LOG_TAG, "ERROR: stopMusic(): Cannot stop music, as MediaPlayer object is already null.");
315+
HXLog.e(LOG_TAG, "ERROR: stop(): Cannot stop music, as MediaPlayer object is already null.");
316316
return false;
317317
}
318318
}

0 commit comments

Comments
 (0)