Skip to content

Commit

Permalink
HXMusicEngine: Addressed IllegalStateException issues when isPlaying(…
Browse files Browse the repository at this point in the history
…) and setLooping() is called.
  • Loading branch information
huhx0015 committed Apr 4, 2018
1 parent 0f23687 commit e45db0f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 42 deletions.
25 changes: 13 additions & 12 deletions app/app.iml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id=":app" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="HX_Game_Sound_Engine" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<module external.linked.project.id=":app" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
Expand Down Expand Up @@ -90,6 +90,7 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-safeguard" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/javaPrecompile" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
Expand All @@ -107,17 +108,17 @@
</content>
<orderEntry type="jdk" jdkName="Android API 25 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="com.android.support:animated-vector-drawable-25.4.0" level="project" />
<orderEntry type="library" name="com.android.support:support-annotations:25.4.0@jar" level="project" />
<orderEntry type="library" name="com.android.support:support-core-utils-25.4.0" level="project" />
<orderEntry type="library" name="com.android.support:support-v4-25.4.0" level="project" />
<orderEntry type="library" name="com.android.support:support-vector-drawable-25.4.0" level="project" />
<orderEntry type="library" name="com.android.support:support-fragment-25.4.0" level="project" />
<orderEntry type="library" name="com.android.support:cardview-v7-25.4.0" level="project" />
<orderEntry type="library" name="com.android.support:support-media-compat-25.4.0" level="project" />
<orderEntry type="library" name="com.android.support:appcompat-v7-25.4.0" level="project" />
<orderEntry type="library" name="com.android.support:support-core-ui-25.4.0" level="project" />
<orderEntry type="library" name="com.android.support:support-compat-25.4.0" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:appcompat-v7-25.4.0" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:support-fragment-25.4.0" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:animated-vector-drawable-25.4.0" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:support-compat-25.4.0" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:support-v4-25.4.0" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:support-media-compat-25.4.0" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:support-vector-drawable-25.4.0" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:support-core-ui-25.4.0" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:support-annotations:25.4.0@jar" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:cardview-v7-25.4.0" level="project" />
<orderEntry type="library" name="Gradle: com.android.support:support-core-utils-25.4.0" level="project" />
<orderEntry type="module" module-name="hxaudio" />
</component>
</module>
9 changes: 4 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion '26.0.2'
buildToolsVersion '27.0.3'

defaultConfig {
applicationId "com.huhx0015.huhxagse"
Expand All @@ -22,10 +22,9 @@ android {
dependencies {

// INTEGRATED LIBRARIES:
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':hxaudio')
implementation project(':hxaudio')

// ANDROID SUPPORT LIBRARIES:
compile 'com.android.support:appcompat-v7:25.4.0'
compile 'com.android.support:cardview-v7:25.4.0'
implementation 'com.android.support:appcompat-v7:25.4.0'
implementation 'com.android.support:cardview-v7:25.4.0'
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.android.tools.build:gradle:3.1.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Apr 03 18:19:38 PDT 2018
#Tue Apr 03 22:01:14 PDT 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
2 changes: 1 addition & 1 deletion hxaudio/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 27
buildToolsVersion "26.0.2"
buildToolsVersion '27.0.3'

defaultConfig {
minSdkVersion 9
Expand Down
8 changes: 7 additions & 1 deletion hxaudio/hxaudio.iml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id=":hxaudio" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="HX_Game_Sound_Engine" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<module external.linked.project.id=":hxaudio" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
Expand Down Expand Up @@ -92,9 +92,15 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-safeguard" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/intermediate-jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/javaPrecompile" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/packaged-aidl" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/packagedAssets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/packaged_res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/public_res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/shaders" />
Expand Down
45 changes: 25 additions & 20 deletions hxaudio/src/main/java/com/huhx0015/hxaudio/audio/HXMusicEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,31 @@ synchronized boolean initMusicEngine(HXMusicItem music, final int position, fina

@Override
public void onPrepared(MediaPlayer currentPlayer) {
try {

if (musicPosition != 0) {
currentPlayer.seekTo(musicPosition);
HXLog.d(LOG_TAG, "PREPARING: onPrepared(): MediaPlayer position set to: " + position);
}
if (musicPosition != 0) {
currentPlayer.seekTo(musicPosition);
HXLog.d(LOG_TAG, "PREPARING: onPrepared(): MediaPlayer position set to: " + position);
}

// GAPLESS: If gapless mode is enabled, the secondary MediaPlayer will begin
// immediate playback after playback on the current MediaPlayer has completed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && isGapless &&
isLooped) {
// GAPLESS: If gapless mode is enabled, the secondary MediaPlayer will begin
// immediate playback after playback on the current MediaPlayer has completed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && isGapless &&
isLooped) {

currentPlayer.setLooping(false); // Disables looping attribute.
currentPlayer.setLooping(false); // Disables looping attribute.

nextPlayer = prepareMediaPlayer(context);
nextPlayer.setOnPreparedListener(nextPlayerPreparedListener);
nextPlayer.setOnCompletionListener(nextPlayerCompletionListener);
nextPlayer.setOnBufferingUpdateListener(playerBufferingUpdateListener);
nextPlayer = prepareMediaPlayer(context);
nextPlayer.setOnPreparedListener(nextPlayerPreparedListener);
nextPlayer.setOnCompletionListener(nextPlayerCompletionListener);
nextPlayer.setOnBufferingUpdateListener(playerBufferingUpdateListener);

HXLog.d(LOG_TAG, "PREPARING: Gapless mode prepared.");
} else {
currentPlayer.setLooping(isLooped); // Sets the looping attribute.
HXLog.d(LOG_TAG, "PREPARING: onPrepared(): MediaPlayer looping status: " + isLooped);
}
HXLog.d(LOG_TAG, "PREPARING: Gapless mode prepared.");
} else {
currentPlayer.setLooping(isLooped); // Sets the looping attribute.
HXLog.d(LOG_TAG, "PREPARING: onPrepared(): MediaPlayer looping status: " + isLooped);
}

try {
currentPlayer.start(); // Begins playing the music.

// Invokes the associated listener call.
Expand Down Expand Up @@ -258,7 +258,12 @@ public void onBufferingUpdate(MediaPlayer mp, int percent) {

// isPlaying(): Determines if a music is currently playing in the background.
boolean isPlaying() {
return currentPlayer != null && isInitialized && currentPlayer.isPlaying();
try {
return currentPlayer != null && isInitialized && currentPlayer.isPlaying();
} catch (Exception e) {
HXLog.e(LOG_TAG, "ERROR: isPlaying(): " + e.getLocalizedMessage());
return false;
}
}

// pause(): Pauses any music playing in the background.
Expand Down

0 comments on commit e45db0f

Please sign in to comment.