Package eas.miscellaneous.useful

Source Code of eas.miscellaneous.useful.MP3Player

/*
* File name:        MP3Test.java (package eas.users.lukas.demos.clock)
* Author(s):        lko
* Java version:     7.0
* Generation date:  21.11.2012 (10:16:02)
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
*   author or licensor (but not in any way that suggests that they endorse
*   you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
*   distribute the resulting work only under the same or a similar license to
*   this one.
*
* + Detailed license conditions (Germany):
*   http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
*   http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/

package eas.miscellaneous.useful;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import eas.miscellaneous.system.FileNamePostfixFilter;

/**
* @author lko
*/
public class MP3Player implements Runnable, Serializable {
   
    /**
     *
     */
    private static final long serialVersionUID = 7315528366023566240L;
    private transient Player p;
    private ArrayList<File> playList;
    private boolean repeat = false;
    private boolean stopRequested = false;
    public static LinkedList<MP3Player> activePlayers = new LinkedList<MP3Player>();
   
    public void requestStop() {
        this.stopRequested = true;
        if (p != null) {
            p.close();
        }
    }
   
    public void setRepeat(boolean repeat) {
        this.repeat = repeat;
    }
   
    public MP3Player(File f) {
        try {
            String[] files = f.list(new FileNamePostfixFilter("mp3"));
            ArrayList<String> a = new ArrayList<String>(files.length);
            for (String s : files) {
                a.add(s);
            }
            Collections.sort(a);
            ArrayList<File> filesList = new ArrayList<File>(files.length);
           
            for (String s : a) {
                filesList.add(new File(f.getAbsolutePath() + "/" + s));
            }
           
            this.playList = filesList;
            activePlayers.add(this);
        } catch (Exception e) {
            throw new RuntimeException("MP3-Player down [" + e.getMessage() + "].");
        }
    }
   
    @Override
    public void run() {
        boolean playFirst = true;
       
        while (!this.stopRequested && (this.repeat || playFirst)) {
            playFirst = false;
            for (File f : playList) {
                if (this.stopRequested || MP3Player.turnOffAllSounds) {
                    return;
                }
                try {
                    createPlayer(f);
                    p.play();
                } catch (Exception e) {
                    throw new RuntimeException("MP3-Player down [" + e.getMessage() + "].");
                }
            }
        }
    }

    private void createPlayer(File f) throws FileNotFoundException,
            JavaLayerException {
        InputStream stream = new FileInputStream(f);
        p = new Player(stream);
    }

//    public static void main(String[] args) {
//        MP3Player mp3 = new MP3Player(new File("sharedDirectory/Polytris"));
//        Thread t = new Thread(mp3);
//        t.start();
//    }
   
    public static boolean turnOffAllSounds = false;
}
TOP

Related Classes of eas.miscellaneous.useful.MP3Player

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.