Code example for AudioManager

Methods: getRingerModerequestAudioFocus

0
		if(prefs == null) {
			noFocus = true;
			return; 
		} 
		if(prefs.getInt("pref_musicvolume", 60) > 0) {
			int result = audioCEO.requestAudioFocus(this,
		                // Use the music stream. 
		                AudioManager.STREAM_MUSIC,
		                // Request permanent focus. 
		                AudioManager.AUDIOFOCUS_GAIN);
			if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
				noFocus = false;
			} else 
				noFocus = true;
		} 
	} 
 
	public void setInactive(boolean b) {
		isInactive = b;
	} 
	 
	public void loadEffects() { 
		soundID_tetrisSoundPlayer = soundPool.load(host, R.raw.tetris_free, 1);
		soundID_dropSoundPlayer = soundPool.load(host, R.raw.drop_free, 1);
		soundID_buttonSoundPlayer = soundPool.load(host, R.raw.key_free, 1);
		soundID_clearSoundPlayer = soundPool.load(host, R.raw.clear2_free, 1);
		soundID_gameOverPlayer = soundPool.load(host, R.raw.gameover2_free, 1);
	} 
	 
	public void loadMusic(int type, int startTime) {
		 
		/* Reset previous Music */ 
		isMusicReady = false;
		if(musicPlayer != null)
			musicPlayer.release();
		musicPlayer = null;
		 
		/* Check if Music is allowed to start */ 
		requestFocus(); 
		if(noFocus)
			return; 
		if(isInactive)
			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);
				break; 
			case GAME_MUSIC :
				musicPlayer = MediaPlayer.create(host, R.raw.sadrobot01);
				break; 
			default : 
				musicPlayer = new MediaPlayer();
				break; 
		} 
		musicPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		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);
		isMusicReady = true;
	} 
	 
	public void startMusic(int type, int startTime) {
		/* Check if Music is allowed to start */ 
		requestFocus(); 
		if(noFocus)
			return; 
		if(isInactive)
			return; 
		 
		if(isMusicReady) {
			/* NOP */ 
		} else { 
			loadMusic(type,startTime);
		} 
		if(isMusicReady) {
			if(audioCEO.getRingerMode() != AudioManager.RINGER_MODE_NORMAL)
				return; 
			 
			musicPlayer.setVolume(0.01f * PreferenceManager.getDefaultSharedPreferences(host).getInt("pref_musicvolume", 60), 0.01f * PreferenceManager.getDefaultSharedPreferences(host).getInt("pref_musicvolume", 60));
			musicPlayer.start();
		}