Sounds was one of the biggest problems when developing this game. First i was using ApWidgets with multiple MediaPlayers. But later in process i find out that this was very laggy on some devices. After days of searching and studying i found way to use SoundPool and get contents from /data folder ( /assets after export ). AssetManager was the solution.
Music files was too big for the SoundPool, so i had to use MediaPlayer instead.
Here is example how to use SoundPool for sound effects and MediaPlayer for musics:
- final String MENU_MUSIC = "musics/menu_music.mp3";
- final String GAME_MUSIC = "musics/game_music.mp3";
- final String BLOCK_SOUND = "sounds/block_destroy.mp3";
- final String BOMB_SOUND = "sounds/bomb.mp3";
- final int BLOCK_S = 1;
- final int BOMB_S = 2;
- final int MENU_M = 1;
- final int GAME_M = 2;
- SoundPool soundPool;
- MediaPlayer mediaPlayer;
- AssetManager assetManager;
- AssetFileDescriptor music_Menu = null;
- AssetFileDescriptor music_Game = null;
- int sound_Block;
- int sound_Bomb;
- void soundsInit() {
- soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
- mediaPlayer = new MediaPlayer();
- assetManager = this.getAssets();
-
- try {
- sound_Block = soundPool.load(assetManager.openFd(BLOCK_SOUND), 1);
- sound_Bomb = soundPool.load(assetManager.openFd(BOMB_SOUND), 1);
- music_Menu = assetManager.openFd(MENU_MUSIC);
- music_Game = assetManager.openFd(GAME_MUSIC);
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- void soundsPlay(int soundKey) {
- if (soundKey == BLOCK_S) {
- soundPool.play(sound_Block, 1, 1, 0, 0, 1);
- }
- else if (soundKey == BOMB_S) {
- soundPool.play(sound_Bomb, 1, 1, 0, 0, 1);
- }
- }
- void musicPlay(int musicKey) {
- if (musicKey == MENU_M) {
- try {
- mediaPlayer.reset();
- mediaPlayer.setDataSource(music_Menu.getFileDescriptor(), music_Menu.getStartOffset(), music_Menu.getLength());
- mediaPlayer.setLooping(true);
- mediaPlayer.setVolume(1.0, 1.0);
- mediaPlayer.prepare();
- mediaPlayer.start();
- }
- catch (IOException e) {
- }
- }
- else if (musicKey == GAME_M) {
- try {
- mediaPlayer.reset();
- mediaPlayer.setDataSource(music_Game.getFileDescriptor(), music_Game.getStartOffset(), music_Game.getLength());
- mediaPlayer.setLooping(true);
- mediaPlayer.setVolume(1.0, 1.0);
- mediaPlayer.prepare();
- mediaPlayer.start();
- }
- catch (IOException e) {
- }
- }
- }
- void musicPause() {
- mediaPlayer.pause();
- }
- void musicResume() {
- mediaPlayer.start();
- }
- // The MediaPlayer must be released when the app closes
- void soundsRelease() {
- soundPool.release();
- mediaPlayer.release();
- }
Use documentation for more detailed information:
I also noticed strange error when i was using soundpool. I use my sketch in landscape mode with line:
That caused a crash if i kept my phone upright, but it runs without problem if phone was in horizontal plane.
Solution was: move the forced orientation into manifest.xml file:
- <activity android:name="SoundPool example" android:screenOrientation="landscape">
I hope this was useful for people here, because i have spend hours with this to make it work. :)