Android Open Source - Android-SMSDetector Audio Helper






From Project

Back to project page Android-SMSDetector.

License

The source code is released under:

Apache License

If you think the Android project Android-SMSDetector 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

//
//  Licensed to the Apache Software Foundation (ASF) under one
//  or more contributor license agreements.  See the NOTICE file
//  distributed with this work for additional information
//  regarding copyright ownership.  The ASF licenses this file
//  to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
//  with the License.  You may obtain a copy of the License at
////from  www .java2  s  .  c  om
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing,
//  software distributed under the License is distributed on an
//  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
//  KIND, either express or implied.  See the License for the
//  specific language governing permissions and limitations
//  under the License.
//

package helpers.audio;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.NoSuchElementException;
import java.util.Vector;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import exceptions.EmptyArgumentException;
import exceptions.InvalidResourceIdException;
import exceptions.PreviouslyInitializedVariableException;

public class AudioHelper {
  private static AudioHelper instance = null;

  private SoundPool soundPool = null;
  private int maxConcurrentSounds = 1;

  private Context context = null;

  private Hashtable<String, String> soundsResourcesIdsMapping = null;
  private Hashtable<String, String> streamResourcesIdsMapping = null;

  private AudioHelper() {
    // initializations
    soundPool = new SoundPool(maxConcurrentSounds,
        AudioManager.STREAM_MUSIC, 0);

    streamResourcesIdsMapping = new Hashtable<String, String>();
  }

  private synchronized static void createInstance() {
    if (instance == null) {
      instance = new AudioHelper();
    }
  }

  public static AudioHelper getInstance() {
    if (instance == null) {
      createInstance();
    }
    return instance;
  }

  public void release() {
    soundPool.release();
    soundPool = null;

    context = null;
    soundsResourcesIdsMapping = null;
    maxConcurrentSounds = 1;

    instance = null;
  }

  private void loadSoundsForConfiguredIds(Vector<String> soundsResourcesId) {
    // internal state check
    if (soundsResourcesIdsMapping != null) {
      throw new PreviouslyInitializedVariableException();
    }

    if (soundPool == null) {
      throw new IllegalStateException(
          "Audio Helper bad state. You can not load sounds if pool has not been initialized.");
    }

    // load sounds for configured resources ids
    soundsResourcesIdsMapping = new Hashtable<String, String>();

    for (Enumeration<String> elements = soundsResourcesId.elements(); elements
        .hasMoreElements();) {
      String resourceIdString = elements.nextElement();
      int resourceIdInt = Integer.parseInt(resourceIdString);

      if (resourceIdInt == 0) {
        throw new InvalidResourceIdException();
      }

      int soundLoadedId = soundPool.load(context, resourceIdInt, 1);

      soundsResourcesIdsMapping.put(resourceIdString,
          String.valueOf(soundLoadedId));

    }
  }

  public void configureWithSoundsResourcesIds(Vector<String> soundsResourcesId) {

    // preconditions checks
    if (soundsResourcesId == null) {
      throw new NullPointerException();
    } else if (soundsResourcesId.isEmpty()) {
      throw new EmptyArgumentException();
    }

    if (soundsResourcesIdsMapping != null) {
      throw new PreviouslyInitializedVariableException();
    }

    if (context == null) {
      throw new IllegalStateException(
          "Audio helper bas internal state. You must set the context to use nefore calling this method.");
    }

    // configuration code
    this.loadSoundsForConfiguredIds(soundsResourcesId);
  }

  public void playSoundsWithResourceId(int resourceId, int looping,
      float volume) {
    // preconditions checks
    if (soundsResourcesIdsMapping.get(String.valueOf(resourceId)) == null) {
      throw new NoSuchElementException(
          "AudioHelper - playSoundsWithResourceId - You are trying to player a sound that has not been loaded yet.");
    }

    // play the sound
    String resourceIdString = String.valueOf(resourceId);
    String soundIDString = soundsResourcesIdsMapping.get(resourceIdString);
    int soundID = Integer.parseInt(soundIDString);

    int streamId = soundPool.play(soundID, volume, volume, 1, looping, 1f);

    // save the sound-stream mapping if sound is played in loop
    if (looping != 0) {
      String streamIdString = String.valueOf(streamId);
      streamResourcesIdsMapping.put(resourceIdString, streamIdString);
    }

  }

  public void stopSoundWithResourceId(int resourceId) {
    String resourceIdString = String.valueOf(resourceId);
    String streamIDString = streamResourcesIdsMapping.get(resourceIdString);

    if (streamIDString != null) {
      int streamID = Integer.parseInt(streamIDString);

      soundPool.stop(streamID);

      // remove the sounds-stream mapping
      streamResourcesIdsMapping.remove(resourceIdString);

    }
  }

  public int getMaxConcurrentSounds() {
    return maxConcurrentSounds;
  }

  public void setMaxConcurrentSounds(int maxConcurrentSounds) {
    // argument checks
    if (maxConcurrentSounds < 1) {
      throw new IllegalArgumentException(
          "You can not use a negative value as maximum concurrent sounds value.");
    }

    this.maxConcurrentSounds = maxConcurrentSounds;
  }

  public void setContext(Context context) {
    this.context = context;
  }

  @Override
  public Object clone() throws CloneNotSupportedException {
    throw new CloneNotSupportedException(
        "You can not clone AudioHelper class since it is a Singleton instance");
  }
}




Java Source Code List

exceptions.EmptyArgumentException.java
exceptions.InvalidResourceIdException.java
exceptions.PreviouslyInitializedVariableException.java
fiesta.share.ShareActivity.java
fiesta.smsreceiver.SMSReceiver.java
helpers.admob.AdMobHelper.java
helpers.audio.AudioHelper.java
helpers.facebook.BaseDialogListener.java
helpers.facebook.BaseRequestListener.java
helpers.facebook.LoginButton.java
helpers.facebook.SessionEvents.java
helpers.facebook.SessionStore.java
helpers.fonts.FontsHelper.java
messages.detector.ImportantMessagesDetectorActivity.java