Back to project page OpenHSK.
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.
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; } }