Android Open Source - fireflies_android Audio Controller






From Project

Back to project page fireflies_android.

License

The source code is released under:

MIT License

If you think the Android project fireflies_android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package donothingbox.game.controller;
/* w  w  w  .  jav a2  s  . co m*/
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import com.example.fireflies_android.*;

/*
 * Long term, this class will manage all audio, abstracting its mgmt from normal usage, as well as mem management 
 * 
 * Most of it's hacky for now, TODO make this "actually" work the way it should
 * 
 */

public class AudioController {
  
  private static SoundPool soundPool;
  private static int soundID;
  private static int soundIDMiss;
  private static int mMuiscId;
    private static MediaPlayer mp = new MediaPlayer();
  static boolean loaded = false;
  private static Context sContext;

  public static void init(final Context context){
    
    sContext = context;
    soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
      soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
              loaded = true;
            }
          });

      soundID = soundPool.load(sContext, R.raw.beep, 1);
      soundIDMiss = soundPool.load(sContext, R.raw.miss, 1);
  }

  public static void playAudio()
  {
        AudioManager audioManager = (AudioManager) sContext.getSystemService(android.content.Context.AUDIO_SERVICE);
        float actualVolume = (float) audioManager
            .getStreamVolume(AudioManager.STREAM_MUSIC);
        float maxVolume = (float) audioManager
            .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        float volume = actualVolume / maxVolume;
        soundPool.play(soundID, volume, volume, 1, 0, 1f);
  }
  

  public static void playMedia()
  {
      mp = MediaPlayer.create(sContext, mMuiscId); 
        mp.setLooping(true);
        mp.start();
  }
  
  public static void playMiss()
  {
        AudioManager audioManager = (AudioManager) sContext.getSystemService(android.content.Context.AUDIO_SERVICE);
        float actualVolume = (float) audioManager
            .getStreamVolume(AudioManager.STREAM_MUSIC);
        float maxVolume = (float) audioManager
            .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        float volume = actualVolume / maxVolume;
        soundPool.play(soundIDMiss, volume, volume, 1, 0, 1f);

  }
}




Java Source Code List

com.donothingbox.fireflies_android.CoreApp.java
com.donothingbox.fireflies_android.DynamicActivity.java
com.donothingbox.fireflies_android.GameSurfaceActivity.java
com.donothingbox.fireflies_android.MainActivity.java
donothingbox.game.controller.AudioController.java
donothingbox.game.controller.GameThread.java
donothingbox.game.controller.HUDController.java
donothingbox.game.controller.StateController.java
donothingbox.game.model.DepthSortComparator.java
donothingbox.game.utils.BitmapUtils.java
donothingbox.game.utils.Utils.java
donothingbox.game.view.CustomDrawableView.java
donothingbox.game.view.FireflySprite.java
donothingbox.game.view.GameLayout.java
donothingbox.game.view.GameSurfaceView.java
donothingbox.game.view.Sprite.java