Package com.aqpproject.sound

Source Code of com.aqpproject.sound.SoundController

/*
* AQP Project
* http://http://code.google.com/p/aqp-project/
* Alexandre Gomez - Clément Troesch - Fabrice Latterner
*/
package com.aqpproject.sound;

import com.aqpproject.game.Singleton;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.files.FileHandle;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
*
* @author Fabrice Latterner
*/
public final class SoundController implements SoundInterface {

    public SoundController(String music_path, String sound_path, String playlist_path) throws ParserConfigurationException, SAXException, IOException {

        m_volumeFX = Singleton.getOptionsController().getFXVolume();
        m_volumeMusic = Singleton.getOptionsController().getMusicVolume();
        m_musicList = new HashMap<>();
        m_soundList = new HashMap<>();
        m_playlistList = new HashMap();
        m_pl = new PlaylistPlayer();
        m_currentMusic = null;

        if (music_path != null) {
            load(music_path, "music");
        }
        if (sound_path != null) {
            load(sound_path, "sound");
        }
        if (playlist_path != null) {
            load(playlist_path, "playlist");
        }
    }

    @Override
    public void playSound(String name, boolean loop) {
        Sound s = m_soundList.get(name);
        if (loop) {
            s.loop(m_volumeFX);
        } else {
            s.play(m_volumeFX);

        }
    }

    @Override
    public void stopSound(String name) {
        if (m_soundList.containsKey(name)) {
            m_soundList.get(name).stop();
        }
    }

    @Override
    public void playMusic(String name, boolean loop) {
        //On interrompt la playlist si elle tourne
        if (m_pl.isAlive()) {
            m_pl.interrupt();
        }
        //Si une musique est jouée, on la coupe également
        if (m_currentMusic != null) {
            m_currentMusic.stop();
        }
        m_currentMusic = m_musicList.get(name);
        m_currentMusic.setVolume(m_volumeMusic);
        m_currentMusic.play();
    }

    @Override
    public void stopMusic() {
        for (Music m : m_musicList.values()) {
            if (m.isPlaying()) {
                m.stop();
            }
        }
    }

    @Override
    public void playPlaylist(String name) {
        //On récupère la playlist
        ArrayList<String> playlist;
        try {
            if (!m_pl.isAlive()) {
                m_pl = new PlaylistPlayer();
                playlist = loadPlaylist(name);
                m_pl.setConf(m_musicList, playlist, m_volumeMusic);
                m_pl.start();
            }
        } catch (ParserConfigurationException | SAXException | IOException ex) {
            Logger.getLogger(SoundController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void setVolumeFX(float v) {
        m_volumeFX = Math.max(Math.min(v, 1), 0);
    }

    @Override
    public void setVolumeMusic(float v) {
        m_volumeMusic = Math.max(Math.min(v, 1), 0);
        if (m_currentMusic != null) {
            m_currentMusic.setVolume(m_volumeMusic);
        }
    }

    public ArrayList<String> loadPlaylist(String name) throws ParserConfigurationException, SAXException, IOException {
        ArrayList<String> playlist = new ArrayList<String>();
        String path = m_playlistList.get(name);
        if (path != null) {
            File file = new File(path);
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = builder.parse(file);
            doc.getDocumentElement().normalize();

            Element root = doc.getDocumentElement();
            NodeList children = root.getChildNodes();

            for (int i = 0; i < children.getLength(); i++) {
                Node n = children.item(i);
                if (n.getNodeType() == Node.ELEMENT_NODE) {
                    Element e = (Element) n;
                    String _name = e.getAttribute("name");
                    playlist.add(_name);
                }
            }
            return playlist;
        } else {
            return playlist;
        }
    }

    public void load(String path, String type) throws ParserConfigurationException, SAXException, IOException {

        File file = new File(path);
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(file);
        doc.getDocumentElement().normalize();

        Element root = doc.getDocumentElement();
        NodeList children = root.getChildNodes();

        for (int i = 0; i < children.getLength(); i++) {
            Node n = children.item(i);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                Element e = (Element) n;
                String name = e.getAttribute("name");
                String entityPath = e.getAttribute("path");
                switch (type) {
                    case "music": {
                        FileHandle f = new FileHandle(new File(entityPath));
                        Music m = Gdx.audio.newMusic(f);
                        m_musicList.put(name, m);
                        break;
                    }
                    case "sound": {
                        FileHandle f = new FileHandle(new File(entityPath));
                        Sound s = Gdx.audio.newSound(f);
                        m_soundList.put(name, s);
                        break;
                    }
                    case "playlist":
                        m_playlistList.put(name, entityPath);
                        break;
                }
            }
        }
    }

    @Override
    public void pauseMusic() {
        for (Music s : m_musicList.values()) {
            s.pause();
        }
    }

    @Override
    public void resumeMusic() {
        if (m_currentMusic != null) {
            m_currentMusic.play();
        }
    }

    @Override
    public void stopPlaylist() {
        if (m_pl.isAlive()) {
            m_pl.pl_stop();
        }
    }

    @Override
    public void pausePlaylist() {
        if (m_pl.isAlive()) {
            m_pl.pause();
        }
    }

    @Override
    public void resumePlaylist() {
        if (m_pl.isAlive()) {
            m_pl.pl_resume();
        }
    }
    float m_volumeFX;
    float m_volumeMusic;
    HashMap<String, Music> m_musicList;
    HashMap<String, Sound> m_soundList;
    HashMap<String, String> m_playlistList;
    PlaylistPlayer m_pl;
    Music m_currentMusic;

    @Override
    public void destroy() {
        stopPlaylist();
        stopMusic();
    }
}
TOP

Related Classes of com.aqpproject.sound.SoundController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.