Skip to content

Commit 0a2870d

Browse files
committed
HXMusicEngine: Fixed illegal state exception issues that could occur when pauseMusic() is called.
1 parent c6b078a commit 0a2870d

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.3.2'
8+
classpath 'com.android.tools.build:gradle:2.3.3'
99

1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class HXMusicEngine {
2626
/** CLASS VARIABLES ________________________________________________________________________ **/
2727

2828
// AUDIO VARIABLES:
29+
private boolean isInitialized; // Used to keep track of the initialization state of the current player.
2930
private int musicPosition; // Used for tracking the current music position.
3031
private Context context; // Context class used for initializing the MediaPlayer objects.
3132
private HXMusicItem musicItem; // References the current HXMusicItem that stores information about the current music.
@@ -157,6 +158,7 @@ private synchronized MediaPlayer prepareMediaPlayer(Context context) {
157158
try {
158159
player.setDataSource(context, Uri.parse(musicItem.getMusicUrl()));
159160
player.prepareAsync(); // Prepares the MediaPlayer object asynchronously.
161+
isInitialized = true;
160162
HXLog.d(LOG_TAG, "PREPARING: prepareMediaPlayer(): MediaPlayer URL was set, preparing MediaPlayer...");
161163
} catch (Exception e) {
162164
HXLog.e(LOG_TAG, "ERROR: prepareMediaPlayer(): An error occurred while loading the music from the specified URL: " + e.getLocalizedMessage());
@@ -169,6 +171,7 @@ else if (musicItem.getMusicResource() != 0) {
169171
AssetFileDescriptor asset = context.getResources().openRawResourceFd(musicItem.getMusicResource());
170172
player.setDataSource(asset.getFileDescriptor(), asset.getStartOffset(), asset.getLength());
171173
player.prepareAsync(); // Prepares the MediaPlayer object asynchronously.
174+
isInitialized = true;
172175
HXLog.d(LOG_TAG, "PREPARING: prepareMediaPlayer(): MediaPlayer resource was set, preparing MediaPlayer...");
173176
} catch (Exception e) {
174177
HXLog.e(LOG_TAG, "ERROR: prepareMediaPlayer(): An error occurred while loading the music resource: " + e.getLocalizedMessage());
@@ -249,20 +252,20 @@ public void onBufferingUpdate(MediaPlayer mp, int percent) {
249252

250253
// isPlaying(): Determines if a music is currently playing in the background.
251254
boolean isPlaying() {
252-
return currentPlayer != null && currentPlayer.isPlaying();
255+
return currentPlayer != null && isInitialized && currentPlayer.isPlaying();
253256
}
254257

255258
// pause(): Pauses any music playing in the background.
256259
int pauseMusic() {
257260

258261
// Checks to see if the MediaPlayer object has been initialized first before retrieving the
259262
// current music position and pausing the music.
260-
if (currentPlayer != null) {
263+
if (currentPlayer != null && isInitialized) {
261264

262265
musicPosition = currentPlayer.getCurrentPosition(); // Retrieves the current music position.
263266

264267
// Pauses the music only if there is a music is currently playing.
265-
if (currentPlayer != null && currentPlayer.isPlaying()) {
268+
if (currentPlayer.isPlaying()) {
266269

267270
removeNextMediaPlayer(); // Prevents nextPlayer from starting after currentPlayer has completed playback.
268271
currentPlayer.pause(); // Pauses the music.
@@ -283,12 +286,12 @@ int pauseMusic() {
283286

284287
// release(): Used to release the resources being used by the MediaPlayer object.
285288
synchronized boolean release() {
289+
isInitialized = false;
286290

287291
if (currentPlayer != null) {
288292
currentPlayer.reset();
289293
currentPlayer.release();
290294
currentPlayer = null;
291-
292295
HXLog.d(LOG_TAG, "RELEASE: release(): MediaPlayer object has been released.");
293296
return true;
294297
} else {

0 commit comments

Comments
 (0)