If you think the Android project DolphinOES 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
/*******************************************************************************
* Copyright 2014 See AUTHORS file.//fromwww.java2s.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/package com.sidereal.dolphinoes.behaviors.audio;
import java.util.ArrayList;
import java.util.Hashtable;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.sidereal.dolphinoes.architecture.DolphinOES;
import com.sidereal.dolphinoes.architecture.GameBehavior;
import com.sidereal.dolphinoes.architecture.GameObject;
import com.sidereal.dolphinoes.architecture.core.GameData.Settings;
import com.sidereal.dolphinoes.util.FloatWrapper;
/** Plays audio that a {@link GameObject} instance with the {@link AudioListener}
* can listen to.
*
* @author Claudiu Bele */publicclass AudioPlayer extends GameBehavior
{
// region static
publicstatic Hashtable<String, Music> musicClips;
publicstatic Hashtable<String, Sound> soundClips;
publicstatic Sound getSoundClip(String location)
{
if (soundClips == null)
soundClips = new Hashtable<String, Sound>();
if (!soundClips.containsKey(location))
{
soundClips.put(location,
Gdx.audio.newSound(Gdx.files.internal(location)));
}
return soundClips.get(location);
}
publicstatic Music getMusicClip(String location)
{
if (musicClips == null)
musicClips = new Hashtable<String, Music>();
if (!musicClips.containsKey(location))
{
musicClips.put(location,
Gdx.audio.newMusic(Gdx.files.internal(location)));
}
return musicClips.get(location);
}
// endregion
// region fields
/** A playlist of music, can be made to loop */public ArrayList<Music> playlist;
public Music currMusicClip;
publicboolean repeat;
publicboolean mustPlay;
privateboolean paused;
privatelong currSoundId;
public Sound sound;
// endregion fields
// region constructors
public AudioPlayer(GameObject obj)
{
super(obj);
mustPlay = false;
repeat = false;
currSoundId = -2;
}
// endregion constructors
// region methods
publicvoid setClip(String filepath)
{
sound = getSoundClip(filepath);
}
publicvoid setMusicPlaylist(String[] filePaths, boolean repeat)
{
playlist = new ArrayList<Music>();
for (int i = 0; i < filePaths.length; i++)
{
playlist.add(getMusicClip(filePaths[i]));
}
this.repeat = repeat;
}
@Override
publicvoid update()
{
if (mustPlay)
{
if (sound != null)
{
currSoundId = sound.play(((FloatWrapper) DolphinOES.data.getSettings(Settings.SOUND_VOLUME)).get());
mustPlay = false;
}
if (playlist != null && playlist.size() > 0)
{
if (currMusicClip == null)
{
currMusicClip = playlist.get(0);
currMusicClip.play();
}
if (!currMusicClip.isPlaying() && !DolphinOES.isFocused())
{
if (playlist.indexOf(currMusicClip) == playlist.size() - 1)
{
currMusicClip = (repeat) ? playlist.get(0) : null;
} else
{
currMusicClip = playlist.get(playlist
.indexOf(currMusicClip) + 1);
}
currMusicClip.setVolume(((FloatWrapper) DolphinOES.data
.getSettings(Settings.SOUND_VOLUME)).get());
currMusicClip.play();
}
}
}
}
publicvoid Play()
{
if (!mustPlay)
{
mustPlay = true;
if (sound != null)
{
currSoundId = sound.play(((FloatWrapper) DolphinOES.data.getSettings(Settings.SOUND_VOLUME)).get());
}
if (currMusicClip != null && paused)
{
currMusicClip.play();
}
paused = false;
}
}
publicvoid Pause()
{
if (mustPlay && !paused)
{
if (sound != null)
{
sound.resume(currSoundId);
}
if (currMusicClip != null)
{
currMusicClip.pause();
}
paused = true;
mustPlay = false;
}
}
// endregion methods
}