Skip to content

Instantly share code, notes, and snippets.

@thecodeduchess
Last active January 11, 2016 02:43
Show Gist options
  • Select an option

  • Save thecodeduchess/87a5bb7e9e1e3d6e7a6e to your computer and use it in GitHub Desktop.

Select an option

Save thecodeduchess/87a5bb7e9e1e3d6e7a6e to your computer and use it in GitHub Desktop.
Code Provided to use using java zoom. It just works. Should be second class in app.
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.AudioDevice;
import javazoom.jl.player.FactoryRegistry;
import javazoom.jl.player.advanced.AdvancedPlayer;
/**
* Provide basic playing of MP3 files via the javazoom library.
* See http://www.javazoom.net/
*
* @author David J. Barnes and Michael Kölling
* @version 2011.07.31
*/
public class MusicPlayer
{
// The current player. It might be null.
private AdvancedPlayer player;
/**
* Constructor for objects of class MusicFilePlayer
*/
public MusicPlayer()
{
player = null;
}
/**
* Play a part of the given file.
* The method returns once it has finished playing.
* @param filename The file to be played.
*/
public void playSample(String filename)
{
try {
setupPlayer(filename);
player.play(500);
}
catch(JavaLayerException e) {
reportProblem(filename);
}
finally {
killPlayer();
}
}
/**
* Start playing the given audio file.
* The method returns once the playing has been started.
* @param filename The file to be played.
*/
public void startPlaying(final String filename)
{
try {
setupPlayer(filename);
Thread playerThread = new Thread() {
public void run()
{
try {
player.play(5000);
}
catch(JavaLayerException e) {
reportProblem(filename);
}
finally {
killPlayer();
}
}
};
playerThread.start();
}
catch (Exception ex) {
reportProblem(filename);
}
}
public void stop()
{
killPlayer();
}
/**
* Set up the player ready to play the given file.
* @param filename The name of the file to play.
*/
private void setupPlayer(String filename)
{
try {
InputStream is = getInputStream(filename);
player = new AdvancedPlayer(is, createAudioDevice());
}
catch (IOException e) {
reportProblem(filename);
killPlayer();
}
catch(JavaLayerException e) {
reportProblem(filename);
killPlayer();
}
}
/**
* Return an InputStream for the given file.
* @param filename The file to be opened.
* @throws IOException If the file cannot be opened.
* @return An input stream for the file.
*/
private InputStream getInputStream(String filename)
throws IOException
{
return new BufferedInputStream(
new FileInputStream(filename));
}
/**
* Create an audio device.
* @throws JavaLayerException if the device cannot be created.
* @return An audio device.
*/
private AudioDevice createAudioDevice()
throws JavaLayerException
{
return FactoryRegistry.systemRegistry().createAudioDevice();
}
/**
* Terminate the player, if there is one.
*/
private void killPlayer()
{
synchronized(this) {
if(player != null) {
player.stop();
player = null;
}
}
}
/**
* Report a problem playing the given file.
* @param filename The file being played.
*/
private void reportProblem(String filename)
{
System.out.println("There was a problem playing: " + filename);
}
}
@thecodeduchess
Copy link
Author

Answers to some challenges for Music Organizer class

import java.util.ArrayList;

/**
 * A class to hold details of audio files.
 * This version can play the files.
 * 
 * @author David J. Barnes and Michael Kölling
 * @version 2011.07.31
 */
public class MusicOrganizer
{
    // An ArrayList for storing the file names of music files.
    private ArrayList<String> files;
    // A player for the music files.
    private MusicPlayer player;

    /**
     * Create a MusicOrganizer
     */
    public MusicOrganizer()
    {
        files = new ArrayList<String>();
        player = new MusicPlayer();
    }

    /**
     * Add a file to the collection.
     * @param filename The file to be added.
     */
    public void addFile(String filename)
    {
        files.add(filename);
    }

    /**
     * Return the number of files in the collection.
     * @return The number of files in the collection.
     */
    public int getNumberOfFiles()
    {
        return files.size();
    }

    /**
     * List a file from the collection.
     * @param index The index of the file to be listed.
     */
    public void listFile(int index)
    {
        if(index >= 0 && index < files.size()) {
            String filename = files.get(index);
            System.out.println(filename);
        }
    }

    /**
     * Exercise 4.20 and 4.21, and challenge 4.24
     */
    public void listAllFiles()
    {   int position = 0;
        for(String musicFile : files) {
            position += 1;
            System.out.println(position + ": " + musicFile);
        } 
    }

    /**
     * Remove a file from the collection.
     * @param index The index of the file to be removed.
     */
    public void removeFile(int index)
    {
        if(index >= 0 && index < files.size()) {
            files.remove(index);
        }
    }

    /**
     * Start playing a file in the collection.
     * Use stopPlaying() to stop it playing.
     * @param index The index of the file to be played.
     */
    public void startPlaying(int index)
    {
        String filename = files.get(index);
        player.startPlaying(filename);
    }

    /**
     * Stop the player.
     */
    public void stopPlaying()
    {
        player.stop();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment