Audio

Interface :: Audio >> Audio.java

package toxin.schnee.framework;

public interface Audio
{
  public abstract Music getMusic(String fileName);
  public abstract Sound getSound(String fileName);
}
Concrete :: Audio >> AudioImpl.java
package toxin.schnee.framework.impl;

import java.io.IOException;

import android.app.Activity;
import android.content.res.AssetManager;
import android.content.res.AssetFileDescriptor;
import android.media.SoundPool;
import android.media.AudioManager;

import toxin.schnee.framework.Audio;
import toxin.schnee.framework.Music;
import toxin.schnee.framework.Sound;

public final class AudioImpl implements Audio
{
  private SoundPool soundPool;
  private AssetManager assets;

  public AudioImpl(Activity activity){
    activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
    this.assets = activity.getAssets();
    this.soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
  }

  @Override
  public final Music getMusic(String fileName){
    try{
      AssetFileDescriptor assetDescriptor = assets.openFd(fileName);
      return new MusicImpl(assetDescriptor);
    }catch(IOException e){
      throw new RuntimeException("Couldn't load music: '" + fileName + "'");
    }
  }

  @Override
  public final Sound getSound(String fileName){
    try{
      AssetFileDescriptor assetDescriptor = assets.openFd(fileName);
      int soundId = soundPool.load(assetDescriptor, 0);
      return new SoundImpl(soundPool, soundId);
    }catch(IOException e){
      throw new RuntimeException("Couldn't load sound: '" + fileName + "'");
    }
  }
}

Remove all ads