Last active
June 5, 2017 19:16
-
-
Save JakeSteam/55cd4384f92b76f8a6f1c82a32e04124 to your computer and use it in GitHub Desktop.
"Selectively Playing Tracks Whilst Game Is Active / Open" for GameDevAlgorithms.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BaseActivity extends Activity { | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
MusicHelper.getInstance(this).setMovingInApp(false); | |
} | |
@Override | |
public void onPause() { | |
super.onPause(); | |
if (!MusicHelper.getInstance(this).isMovingInApp()) { | |
MusicHelper.getInstance(this).stopMusic(); | |
} | |
} | |
public void close(View v) { | |
MusicHelper.getInstance(this).setMovingInApp(true); | |
finish(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MusicHelper { | |
private static MusicHelper mhInstance = null; | |
private boolean movingInApp = false; | |
private int currentTrack = 0; | |
private Context context; | |
private static Intent musicService; | |
private boolean musicServiceIsStarted = false; | |
public MusicHelper(Context context) { | |
this.context = context; | |
} | |
public static MusicHelper getInstance(Context ctx) { | |
if (mhInstance == null) { | |
mhInstance = new MusicHelper(ctx.getApplicationContext()); | |
} | |
return mhInstance; | |
} | |
public void playIfPossible(final int trackToPlay) { | |
new Thread(new Runnable() { | |
public void run() { | |
// If music should be playing, and it isn't, or is the wrong track, fix that! | |
if ((isIntroMusic(R.raw.time_passes) || Setting.getBoolean(Enums.Setting.Music)) && | |
(trackToPlay != currentTrack || !musicServiceIsStarted)) { | |
musicService = new Intent(context.getApplicationContext(), MusicService.class) | |
.putExtra("songId", trackToPlay); | |
currentTrack = trackToPlay; | |
context.startService(musicService); | |
musicServiceIsStarted = true; | |
} else if (!Setting.getBoolean(Enums.Setting.Music) && musicServiceIsStarted) { | |
context.stopService(musicService); | |
musicServiceIsStarted = false; | |
} | |
} | |
}).start(); | |
} | |
private boolean isIntroMusic(int track) { | |
return track == R.raw.time_passes; | |
} | |
public void stopMusic() { | |
if (musicServiceIsStarted) { | |
context.stopService(musicService); | |
musicServiceIsStarted = false; | |
} | |
} | |
public boolean isMovingInApp() { | |
return movingInApp; | |
} | |
public void setMovingInApp(boolean movingInApp) { | |
this.movingInApp = movingInApp; | |
} | |
public boolean isMusicServiceIsStarted() { | |
return musicServiceIsStarted; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Service; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.media.AudioManager; | |
import android.media.MediaPlayer; | |
import android.os.IBinder; | |
public class MusicService extends Service { | |
private MediaPlayer player; | |
public IBinder onBind(Intent arg0) { | |
return null; | |
} | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); | |
int volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); | |
int songId = intent.getIntExtra("songId", 0); | |
if (songId > 0) { | |
player = MediaPlayer.create(this, songId); | |
player.setLooping(true); | |
player.setVolume(volume, volume); | |
player.start(); | |
} | |
return Service.START_NOT_STICKY; | |
} | |
@Override | |
public void onDestroy() { | |
player.stop(); | |
player.release(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment