From 4046da277da6da7a18f412ef0daafc3c0a59ac44 Mon Sep 17 00:00:00 2001 From: JustWe Date: Sat, 6 Feb 2016 15:37:24 +0800 Subject: [PATCH] Update Music --- .../lfk/justweengine/Utils/Music/Music.java | 28 ++++ .../justweengine/Utils/Music/MusicPlayer.java | 121 ++++++++++++++ .../Utils/Music/SoundManager.java | 140 ++++++++++++++++ .../justweengine/Utils/Music/SoundPlayer.java | 150 ++++++++++++++++++ 4 files changed, 439 insertions(+) create mode 100644 engine/src/main/java/com/lfk/justweengine/Utils/Music/Music.java create mode 100644 engine/src/main/java/com/lfk/justweengine/Utils/Music/MusicPlayer.java create mode 100644 engine/src/main/java/com/lfk/justweengine/Utils/Music/SoundManager.java create mode 100644 engine/src/main/java/com/lfk/justweengine/Utils/Music/SoundPlayer.java diff --git a/engine/src/main/java/com/lfk/justweengine/Utils/Music/Music.java b/engine/src/main/java/com/lfk/justweengine/Utils/Music/Music.java new file mode 100644 index 0000000..d6dac2c --- /dev/null +++ b/engine/src/main/java/com/lfk/justweengine/Utils/Music/Music.java @@ -0,0 +1,28 @@ +package com.lfk.justweengine.Utils.music; + +/** + * music interface + * + * @author liufengkai + * Created by liufengkai on 16/2/5. + */ +public interface Music { + void play(); + + void stop(); + + void pause(); + + void setLooping(boolean isLooping); + + void setVolume(float volume); + + float getVolume(); + + boolean isPlaying(); + + boolean isLooping(); + + void dispose(); + +} diff --git a/engine/src/main/java/com/lfk/justweengine/Utils/Music/MusicPlayer.java b/engine/src/main/java/com/lfk/justweengine/Utils/Music/MusicPlayer.java new file mode 100644 index 0000000..9549c02 --- /dev/null +++ b/engine/src/main/java/com/lfk/justweengine/Utils/Music/MusicPlayer.java @@ -0,0 +1,121 @@ +package com.lfk.justweengine.Utils.music; + +import android.content.Context; +import android.content.res.AssetFileDescriptor; +import android.media.MediaPlayer; + +import java.io.IOException; + +/** + * MusicPlayer + * + * @author liufengkai + * Created by liufengkai on 16/2/5. + */ +public class MusicPlayer implements Music, MediaPlayer.OnCompletionListener { + private MediaPlayer player; + // 准备好? + private boolean isPrepared; + // 是否循环 + private boolean isLooping; + // 音量 + private float volume; + + public MusicPlayer(Context context, String fileName) { + player = new MediaPlayer(); + // init + initMusicPlayer(); + try { + // get file descriptor + AssetFileDescriptor descriptor = context.getAssets().openFd(fileName); + // setData + player.setDataSource(descriptor.getFileDescriptor(), + descriptor.getStartOffset(), + descriptor.getLength()); + player.prepare(); + isPrepared = true; + player.setOnCompletionListener(this); + } catch (IOException e) { + e.printStackTrace(); + throw new RuntimeException("Couldn't load music"); + } + } + + private void initMusicPlayer() { + volume = 10; + isPrepared = false; + isLooping = false; + } + + @Override + public void play() { + if (player.isPlaying()) + return; + + try { + synchronized (this) { + if (isPrepared) { + player.prepare(); + } + player.start(); + } + } catch (IOException e) { + e.printStackTrace(); + } + + } + + @Override + public void stop() { + player.stop(); + synchronized (this) { + isPrepared = false; + } + } + + @Override + public void pause() { + if (player.isPlaying()) + player.pause(); + } + + @Override + public void setLooping(boolean isLooping) { + this.isLooping = isLooping; + } + + @Override + public void setVolume(float volume) { + this.volume = volume; + } + + @Override + public float getVolume() { + return volume; + } + + @Override + public boolean isPlaying() { + return player.isPlaying(); + } + + @Override + public boolean isLooping() { + return isLooping; + } + + @Override + public void dispose() { + if (player.isPlaying()) { + player.stop(); + } + player.release(); + } + + @Override + public void onCompletion(MediaPlayer mp) { + synchronized (this) { + isPrepared = false; + } + } +} diff --git a/engine/src/main/java/com/lfk/justweengine/Utils/Music/SoundManager.java b/engine/src/main/java/com/lfk/justweengine/Utils/Music/SoundManager.java new file mode 100644 index 0000000..b79206a --- /dev/null +++ b/engine/src/main/java/com/lfk/justweengine/Utils/Music/SoundManager.java @@ -0,0 +1,140 @@ +package com.lfk.justweengine.Utils.music; + +import android.content.res.AssetManager; +import android.media.AudioManager; +import android.media.SoundPool; + +import com.lfk.justweengine.Engine.Engine; +import com.lfk.justweengine.Info.UIdefaultData; +import com.lfk.justweengine.Utils.logger.Logger; + +import java.io.IOException; +import java.util.HashMap; + +/** + * SoundManager + * + * @author liufengkai + * Created by liufengkai on 16/2/5. + */ +public class SoundManager { + private AssetManager assetManager; + // sound pool + private SoundPool soundPool; + + // musicName musicID + private HashMap musicMap; + + public SoundManager(Engine engine, int size) { + engine.setVolumeControlStream(AudioManager.STREAM_MUSIC); + // init + this.assetManager = engine.getAssets(); + this.soundPool = new SoundPool(size, AudioManager.STREAM_MUSIC, 0); + this.musicMap = new HashMap<>(); + } + + public void addSound(String musicName) { + try { + musicMap.put(musicName, soundPool.load(assetManager.openFd(musicName), 0)); + } catch (IOException e) { + e.printStackTrace(); + Logger.e("Couldn't found music"); + } + } + + public void removeSound(String musicName) { + if (musicMap.containsKey(musicName)) { + soundPool.unload(musicMap.get(musicName)); + musicMap.remove(musicName); + } else { + Logger.e("Couldn't found music"); + } + } + + /** + * play sound + * + * @param musicName name in assets + * @param volume sound's volume + */ + public void play(String musicName, float volume) { + if (musicMap.containsKey(musicName)) { + soundPool.play(musicMap.get(musicName), volume, volume, 0, 0, 1); + } + } + + /** + * play sound with defaultMusicVolume + * + * @param musicName name in assets + */ + public void play(String musicName) { + if (musicMap.containsKey(musicName)) { + soundPool.play(musicMap.get(musicName), + UIdefaultData.defaultMusicVolume, + UIdefaultData.defaultMusicVolume, + 0, 0, 1); + } + } + + /** + * play sound with musicID in SoundPool + * + * @param musicID musicID in SoundPool + */ + public void play(int musicID) { + if (musicMap.containsValue(musicID)) { + soundPool.play(musicID, + UIdefaultData.defaultMusicVolume, + UIdefaultData.defaultMusicVolume, + 0, 0, 1); + } + } + + /** + * play sound with musicID in SoundPool + * + * @param musicID musicID in SoundPool + * @param volume volume + */ + public void play(int musicID, float volume) { + if (musicMap.containsValue(musicID)) { + soundPool.play(musicID, + volume, + volume, + 0, 0, 1); + } + } + + /** + * Is music in map? + * + * @param soundName soundName + * @return Is music in map? + */ + public boolean containSound(String soundName) { + return musicMap.containsKey(soundName); + } + + /** + * Is music in map? + * + * @param soundID soundID + * @return Is music in map? + */ + public boolean containSoundID(int soundID) { + return musicMap.containsValue(soundID); + } + + /** + * get Music from Name + * + * @param soundName soundName + * @return soundID + */ + public int getSoundID(String soundName) { + return musicMap.get(soundName); + } + + +} diff --git a/engine/src/main/java/com/lfk/justweengine/Utils/Music/SoundPlayer.java b/engine/src/main/java/com/lfk/justweengine/Utils/Music/SoundPlayer.java new file mode 100644 index 0000000..2e3986c --- /dev/null +++ b/engine/src/main/java/com/lfk/justweengine/Utils/Music/SoundPlayer.java @@ -0,0 +1,150 @@ +package com.lfk.justweengine.Utils.music; + +import java.util.ArrayList; +import java.util.Timer; +import java.util.TimerTask; + +/** + * SoundPlayer + * + * @author liufengkai + * Created by liufengkai on 16/2/5. + */ +public class SoundPlayer { + // get SoundManager from + private SoundManager manager; + // musicID list + private ArrayList musicList; + // Sound clock + private SoundClock soundClock; + + private boolean isPlaying; + + private long delay; + + private int numTicks; + + /** + * Sound player + * + * @param manager a soundManager have a soundPool. + * @param delay The number of milliseconds to delay between ticks. + * @param numTicks Starts the clock running for a specific number of ticks. + */ + public SoundPlayer(SoundManager manager, + long delay, + int numTicks) { + this.manager = manager; + this.musicList = new ArrayList<>(); + this.isPlaying = false; + this.delay = delay; + this.numTicks = numTicks; + this.soundClock = new SoundClock(delay, numTicks); + } + + /** + * add a sound to player + * + * @param soundName soundName + */ + public void addSound(String soundName) { + if (manager.containSound(soundName)) { + musicList.add(soundName); + } + } + + /** + * remove a sound from player + * + * @param soundName soundName + */ + public void removeSound(String soundName) { + if (manager.containSound(soundName)) { + musicList.remove(soundName); + } + } + + public void play() { + if (!musicList.isEmpty() && + !isPlaying) { + if (soundClock.getTickNumber() == 0) { +// soundClock.restart(); + soundClock = null; + soundClock = new SoundClock(delay, numTicks); + soundClock.start(); + } else + soundClock.start(); + isPlaying = true; + } + } + + private class SoundClock extends TimerTask { + // timer + private Timer timer; + // Starts the clock running for a specific number of ticks. + private int tickLeft; + + private int tickLeftSave; + + // The number of milliseconds to delay between ticks. + private long delay; + + public SoundClock(long delay, int numTicks) { + init(); + this.tickLeft = numTicks; + this.tickLeftSave = numTicks; + this.delay = delay; + } + + private void init() { + this.timer = new Timer(); + } + + // tick 心跳 + private void tick() { + if (tickLeft == 0) + this.stop(); + else { + for (int i = 0; i < musicList.size(); i++) { + manager.play(musicList.get(i)); + } + if (tickLeft > 0) + tickLeft--; + } + } + + @Override + public void run() { + tick(); + } + + public void start(int numTicks) { + this.tickLeft = numTicks; + this.timer.schedule(this, 0, delay); + } + + public void start() { + if (tickLeft != 0) + start(tickLeft); + } + + public void stop() { + this.cancel(); + this.timer.cancel(); + isPlaying = false; + } + + public int getTickNumber() { + return tickLeft; + } + + public void restart() { + this.tickLeft = tickLeftSave; + this.timer.purge(); + this.timer = null; + init(); + start(); + } + } + +}