Skip to content

Commit

Permalink
Version 1.6
Browse files Browse the repository at this point in the history
music bugfixes:
  ringer mode is now taken into account
  added ringer mode broadcast receiver
  added headset plug/unplug broadcast receiver
  • Loading branch information
vocollapse committed May 6, 2013
1 parent e6604d9 commit 89248ee
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 91 deletions.
18 changes: 11 additions & 7 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.blockinger.game"
android:versionCode="9"
android:versionName="1.5" >
android:versionCode="10"
android:versionName="1.6" >

<uses-sdk
android:minSdkVersion="8"
Expand Down Expand Up @@ -69,11 +69,7 @@
android:name="org.blockinger.game.activities.GameActivity"
android:label="@string/title_activity_game"
android:theme="@style/AppTheme" >

<!-- android:theme="@style/FullscreenTheme"> -->

</activity>
<!-- android:configChanges="orientation|keyboardHidden|screenSize" -->
<activity
android:name="org.blockinger.game.activities.SettingsActivity"
android:icon="@drawable/ic_settings"
Expand All @@ -93,11 +89,19 @@
android:name="org.blockinger.game.activities.HelpActivity"
android:label="@string/title_activity_help" >
</activity>
<receiver android:name="org.blockinger.game.components.NoiseBroadcastReceiver">

<!-- <receiver android:name="org.blockinger.game.components.NoiseBroadcastReceiver">
<intent-filter>
<action android:name="android.media.AUDIO_BECOMING_NOISY" />
</intent-filter>
</receiver>
<receiver android:name="org.blockinger.game.components.RingerModeChangeReceiver">
<intent-filter>
<action android:name="AudioManager.RINGER_MODE_CHANGED_ACTION" />
</intent-filter>
</receiver> -->

</application>

</manifest>
18 changes: 11 additions & 7 deletions bin/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.blockinger.game"
android:versionCode="9"
android:versionName="1.5" >
android:versionCode="10"
android:versionName="1.6" >

<uses-sdk
android:minSdkVersion="8"
Expand Down Expand Up @@ -69,11 +69,7 @@
android:name="org.blockinger.game.activities.GameActivity"
android:label="@string/title_activity_game"
android:theme="@style/AppTheme" >

<!-- android:theme="@style/FullscreenTheme"> -->

</activity>
<!-- android:configChanges="orientation|keyboardHidden|screenSize" -->
<activity
android:name="org.blockinger.game.activities.SettingsActivity"
android:icon="@drawable/ic_settings"
Expand All @@ -93,11 +89,19 @@
android:name="org.blockinger.game.activities.HelpActivity"
android:label="@string/title_activity_help" >
</activity>
<receiver android:name="org.blockinger.game.components.NoiseBroadcastReceiver">

<!-- <receiver android:name="org.blockinger.game.components.NoiseBroadcastReceiver">
<intent-filter>
<action android:name="android.media.AUDIO_BECOMING_NOISY" />
</intent-filter>
</receiver>
<receiver android:name="org.blockinger.game.components.RingerModeChangeReceiver">
<intent-filter>
<action android:name="AudioManager.RINGER_MODE_CHANGED_ACTION" />
</intent-filter>
</receiver> -->

</application>

</manifest>
2 changes: 1 addition & 1 deletion res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
<string name="pref_accelerationV_title">Soft Drop Acceleration</string>


<string name="pref_version_summary">1.5\nTouch here to view the public repository of this project</string>
<string name="pref_version_summary">1.6\nTouch here to view the public repository of this project</string>
<string name="pref_version_title">Version</string>
<string name="repository_url">https://github.com/vocollapse/Blockinger</string>

Expand Down
2 changes: 1 addition & 1 deletion src/org/blockinger/game/activities/GameActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected void onCreate(Bundle savedInstanceState) {

/* Init Components */
if(game.isResumable())
sound.loadMusic(Sound.GAME_MUSIC, game.getSongtime());
sound.startMusic(Sound.GAME_MUSIC, game.getSongtime());
sound.loadEffects();
if(b!=null){
value = b.getInt("mode");
Expand Down
4 changes: 3 additions & 1 deletion src/org/blockinger/game/activities/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected void onCreate(Bundle savedInstanceState) {

/* Create Music */
sound = new Sound(this);
sound.loadMusic(Sound.MENU_MUSIC, 0);
sound.startMusic(Sound.MENU_MUSIC, 0);

/* Database Management */
Cursor mc;
Expand Down Expand Up @@ -249,6 +249,7 @@ public void onClickResume(View view) {
@Override
protected void onStop() {
super.onStop();
sound.setActivity(false);
sound.pause();
datasource.close();
};
Expand All @@ -264,6 +265,7 @@ protected void onDestroy() {
@Override
protected void onResume() {
super.onResume();
sound.setActivity(true);
sound.resume();
datasource.open();
Cursor cursor = datasource.getCursor();
Expand Down
62 changes: 0 additions & 62 deletions src/org/blockinger/game/components/NoiseBroadcastReceiver.java

This file was deleted.

130 changes: 118 additions & 12 deletions src/org/blockinger/game/components/Sound.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.blockinger.game.R;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.media.AudioManager;
Expand All @@ -59,10 +60,14 @@ public class Sound implements OnAudioFocusChangeListener {
private int soundID_buttonSoundPlayer;
private MediaPlayer musicPlayer;
private boolean noFocus;
private IntentFilter intentFilter;
private NoiseBroadcastReceiver noisyAudioStreamReceiver;
private boolean isMusicReady;
private BroadcastReceiver noisyAudioStreamReceiver;
private BroadcastReceiver ringerModeReceiver;
private BroadcastReceiver headsetPlugReceiver;
private SoundPool soundPool;
private int songtime;
private int musicType;
private boolean isInactive;

public static final int NO_MUSIC = 0x0;
public static final int MENU_MUSIC = 0x1;
Expand All @@ -83,9 +88,53 @@ public Sound(Activity c) {
} else
noFocus = true;

IntentFilter intentFilter;
/*Noise Receiver (when unplugging headphones) */
intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
noisyAudioStreamReceiver = new NoiseBroadcastReceiver();
noisyAudioStreamReceiver = new BroadcastReceiver() {
public void onReceive(Context context, android.content.Intent intent) {
Sound.this.pauseMusic();
}
};
c.registerReceiver(noisyAudioStreamReceiver, intentFilter);

/* Headphone Receiver (whenheadphone state changes) */
intentFilter = new IntentFilter(android.content.Intent.ACTION_HEADSET_PLUG );
headsetPlugReceiver = new BroadcastReceiver() {

public void onReceive(Context context, android.content.Intent intent) {
if (intent.getAction().equals(android.content.Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
// Headset is unplugged
//Sound.this.pauseMusic();
break;
case 1:
// Headset is plugged
Sound.this.startMusic(musicType,songtime);
break;
default:
// I have no idea what the headset state is
}
}
}

};
c.registerReceiver(headsetPlugReceiver, intentFilter);

/* Ringer Mode Receiver (when the user changes audio mode to silent or back to normal) */
intentFilter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
ringerModeReceiver = new BroadcastReceiver() {

public void onReceive(Context context, android.content.Intent intent) {
songtime = getSongtime();
Sound.this.pauseMusic();
Sound.this.startMusic(musicType,songtime);
}

};
c.registerReceiver(ringerModeReceiver,intentFilter);

soundPool = new SoundPool(c.getResources().getInteger(R.integer.audio_streams),AudioManager.STREAM_MUSIC,0);

Expand All @@ -96,6 +145,13 @@ public Sound(Activity c) {
soundID_buttonSoundPlayer = -1;

songtime = 0;
musicType = 0;
isMusicReady = false;
isInactive = false;
}

public void setActivity(boolean b) {
isInactive = !b;
}

public void loadEffects() {
Expand All @@ -107,7 +163,24 @@ public void loadEffects() {
}

public void loadMusic(int type, int startTime) {
if(isInactive)
return;

/* Reset previous Music */
isMusicReady = false;
if(musicPlayer != null)
musicPlayer.release();
musicPlayer = null;

/* Check if Music is allowed to start */
if(noFocus)
return;
if(audioCEO.getRingerMode() != AudioManager.RINGER_MODE_NORMAL)
return;

/* Start Music */
songtime = startTime;
musicType = type;
switch(type) {
case MENU_MUSIC :
musicPlayer = MediaPlayer.create(host, R.raw.lemmings03);
Expand All @@ -123,7 +196,27 @@ public void loadMusic(int type, int startTime) {
musicPlayer.setLooping(true);
musicPlayer.setVolume(0.01f * PreferenceManager.getDefaultSharedPreferences(host).getInt("pref_musicvolume", 60), 0.01f * PreferenceManager.getDefaultSharedPreferences(host).getInt("pref_musicvolume", 60));
musicPlayer.seekTo(songtime);
musicPlayer.start();
isMusicReady = true;
}

public void startMusic(int type, int startTime) {
if(isInactive)
return;

if(isMusicReady) {
/* NOP */
} else {
loadMusic(type,startTime);
}
if(isMusicReady) {
/* Check if Music is allowed to start */
if(noFocus)
return;
if(audioCEO.getRingerMode() != AudioManager.RINGER_MODE_NORMAL)
return;

musicPlayer.start();
}
}

public void clearSound() {
Expand Down Expand Up @@ -205,34 +298,47 @@ public void gameOverSound() {
}

public void resume() {
if(isInactive)
return;

soundPool.autoResume();
startMusic(musicType,songtime);
}

public void pauseMusic() {
if(isInactive)
return;

isMusicReady = false;
if(musicPlayer != null)
try{
musicPlayer.start();
musicPlayer.pause();
isMusicReady = true;
} catch(IllegalStateException e) {

isMusicReady = false;
}
}

public void pause() {
if(isInactive)
return;

soundPool.autoPause();
if(musicPlayer != null)
try{
musicPlayer.pause();
} catch(IllegalStateException e) {

}
pauseMusic();
}

public void release() {
soundPool.autoPause();
soundPool.release();
soundPool = null;
isMusicReady = false;
if(musicPlayer != null)
musicPlayer.release();
musicPlayer = null;

host.unregisterReceiver(noisyAudioStreamReceiver);
host.unregisterReceiver(ringerModeReceiver);
host.unregisterReceiver(headsetPlugReceiver);
audioCEO.abandonAudioFocus(this);
audioCEO = null;
host = null;
Expand Down

0 comments on commit 89248ee

Please sign in to comment.