Android Open Source - OpenHSK Sound Manager






From Project

Back to project page OpenHSK.

License

The source code is released under:

This work is licensed under a Creative Commons Attribution 3.0 Unported License. Original author of word lists: http://lingomi.com/ Original author of definitions: http://cc-cedict.org Original autho...

If you think the Android project OpenHSK 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 edu.openhsk.utils;
//  w ww  .ja va2s .c om
import java.util.HashMap;

import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.util.Log;

public class SoundManager {
  private static final String LOG_TAG = SoundManager.class.getSimpleName();

  //SoundPool playback constants
  private static final int STREAM_ERROR = 0;
  private static final float RATE = 1f;
  private static final int LOOP = 0;
  private static final int PRIORITY = 1;
  private static final float RIGHT_VOLUME = 1f;
  private static final float LEFT_VOLUME = 1f;

  private SoundPool soundPool;
  private String filePath = "hsk1_sounds/";

  /** This hashmap maps soundfiles to soundId's. */
  private HashMap<String, Integer> soundMap;
  private final AssetManager assetManager;

  public SoundManager(AssetManager assetManager) {
    this.assetManager = assetManager;
    soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
    soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
      public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
        if (status == 0 && sampleId != 0) {
          if (soundPool.play(sampleId, LEFT_VOLUME, 
              RIGHT_VOLUME, PRIORITY, LOOP, 
                RATE) == STREAM_ERROR) {
            Log.e(LOG_TAG, "Playback error for file with " +
                "soundId " + sampleId);
          }
        } else {
          Log.e(LOG_TAG, "Soundpool error, status code: " + status);
        }
      }
    });
    
    soundMap = new HashMap<String, Integer>();
  }
  
  public SoundManager(AssetManager assetManager, String filePath) {
    this(assetManager);
    this.filePath = filePath;
  }
  
  public void playSoundFile(String filename) {
    try {
      int soundID = 0;
      try {
        soundID = soundMap.get(filename);
        if (soundID == 0) {
          throw new Exception("Error loading soundfile " + filename);
        }
        if (soundPool.play(soundID, LEFT_VOLUME, RIGHT_VOLUME, 
            PRIORITY, LOOP, RATE) == STREAM_ERROR) {
          throw new Exception("Playback error for file " + 
            filename + " with soundId " + soundID);
        }
        Log.d(LOG_TAG, "Played file: " + filePath + filename);
      } catch (NullPointerException e) {
        soundID = loadSound(filename);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private int loadSound(String filename) {
    AssetFileDescriptor afd;
    try {
      afd = assetManager.openFd(filePath  + filename);
      int soundId = soundPool.load(afd, PRIORITY);
      if (soundId != 0) {
        soundMap.put(filename, soundId);
        return soundId;
      } else {
        Log.e(LOG_TAG, "Error loading file " + filename + 
            " into soundpool");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return 0;
  }
}




Java Source Code List

edu.openhsk.CharacterListActivity.java
edu.openhsk.CharacterViewActivity.java
edu.openhsk.CreateWordActivity.java
edu.openhsk.CreateWordListActivity.java
edu.openhsk.ExamActivity.java
edu.openhsk.ExamResultActivity.java
edu.openhsk.MainActivity.java
edu.openhsk.QuizActivity.java
edu.openhsk.WordListSelectionActivity.java
edu.openhsk.adapters.WordListViewBinder.java
edu.openhsk.adapters.WordViewBinder.java
edu.openhsk.models.ExamResult.java
edu.openhsk.models.Hanzi.java
edu.openhsk.models.QuizHanzi.java
edu.openhsk.models.WordList.java
edu.openhsk.repository.DatabaseHelper.java
edu.openhsk.repository.DatabaseMetadata.java
edu.openhsk.repository.HanziRepository.java
edu.openhsk.repository.HelpfulCursor.java
edu.openhsk.repository.QuizRepository.java
edu.openhsk.repository.SimpleCursorLoader.java
edu.openhsk.repository.WordListRepository.java
edu.openhsk.service.QuizService.java
edu.openhsk.utils.AsyncSoundPlayer.java
edu.openhsk.utils.CSVExporter.java
edu.openhsk.utils.CSVParser.java
edu.openhsk.utils.PinyinReplacer.java
edu.openhsk.utils.SoundManager.java
edu.openhsk.views.BarGraph.java
edu.openhsk.views.Bar.java
edu.openhsk.views.ExamResultPieChart.java