-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
439 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
engine/src/main/java/com/lfk/justweengine/Utils/Music/Music.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
|
||
} |
121 changes: 121 additions & 0 deletions
121
engine/src/main/java/com/lfk/justweengine/Utils/Music/MusicPlayer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
140 changes: 140 additions & 0 deletions
140
engine/src/main/java/com/lfk/justweengine/Utils/Music/SoundManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Integer> 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); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.