first commit
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,23 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=MusicOrganizer()
|
||||
comment0.text=\n\ Create\ a\ MusicOrganizer\n
|
||||
comment1.params=filename
|
||||
comment1.target=void\ addFile(java.lang.String)
|
||||
comment1.text=\n\ Add\ a\ file\ to\ the\ collection.\n\ @param\ filename\ The\ file\ to\ be\ added.\n
|
||||
comment2.params=
|
||||
comment2.target=int\ getNumberOfFiles()
|
||||
comment2.text=\n\ Return\ the\ number\ of\ files\ in\ the\ collection.\n\ @return\ The\ number\ of\ files\ in\ the\ collection.\n
|
||||
comment3.params=index
|
||||
comment3.target=void\ listFile(int)
|
||||
comment3.text=\n\ List\ a\ file\ from\ the\ collection.\n\ @param\ index\ The\ index\ of\ the\ file\ to\ be\ listed.\n
|
||||
comment4.params=index
|
||||
comment4.target=void\ removeFile(int)
|
||||
comment4.text=\n\ Remove\ a\ file\ from\ the\ collection.\n\ @param\ index\ The\ index\ of\ the\ file\ to\ be\ removed.\n
|
||||
comment5.params=index
|
||||
comment5.target=void\ startPlaying(int)
|
||||
comment5.text=\n\ Start\ playing\ a\ file\ in\ the\ collection.\n\ Use\ stopPlaying()\ to\ stop\ it\ playing.\n\ @param\ index\ The\ index\ of\ the\ file\ to\ be\ played.\n
|
||||
comment6.params=
|
||||
comment6.target=void\ stopPlaying()
|
||||
comment6.text=\n\ Stop\ the\ player.\n
|
||||
numComments=7
|
@@ -0,0 +1,85 @@
|
||||
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 2016.02.29
|
||||
*/
|
||||
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<>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
}
|
@@ -0,0 +1,30 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=MusicPlayer()
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ MusicFilePlayer\n
|
||||
comment1.params=filename
|
||||
comment1.target=void\ playSample(java.lang.String)
|
||||
comment1.text=\n\ Play\ a\ part\ of\ the\ given\ file.\n\ The\ method\ returns\ once\ it\ has\ finished\ playing.\n\ @param\ filename\ The\ file\ to\ be\ played.\n
|
||||
comment2.params=filename
|
||||
comment2.target=void\ startPlaying(java.lang.String)
|
||||
comment2.text=\n\ Start\ playing\ the\ given\ audio\ file.\n\ The\ method\ returns\ once\ the\ playing\ has\ been\ started.\n\ @param\ filename\ The\ file\ to\ be\ played.\n
|
||||
comment3.params=
|
||||
comment3.target=void\ run()
|
||||
comment4.params=
|
||||
comment4.target=void\ stop()
|
||||
comment5.params=filename
|
||||
comment5.target=void\ setupPlayer(java.lang.String)
|
||||
comment5.text=\n\ Set\ up\ the\ player\ ready\ to\ play\ the\ given\ file.\n\ @param\ filename\ The\ name\ of\ the\ file\ to\ play.\n
|
||||
comment6.params=filename
|
||||
comment6.target=java.io.InputStream\ getInputStream(java.lang.String)
|
||||
comment6.text=\n\ Return\ an\ InputStream\ for\ the\ given\ file.\n\ @param\ filename\ The\ file\ to\ be\ opened.\n\ @throws\ IOException\ If\ the\ file\ cannot\ be\ opened.\n\ @return\ An\ input\ stream\ for\ the\ file.\n
|
||||
comment7.params=
|
||||
comment7.target=javazoom.jl.player.AudioDevice\ createAudioDevice()
|
||||
comment7.text=\n\ Create\ an\ audio\ device.\n\ @throws\ JavaLayerException\ if\ the\ device\ cannot\ be\ created.\n\ @return\ An\ audio\ device.\n
|
||||
comment8.params=
|
||||
comment8.target=void\ killPlayer()
|
||||
comment8.text=\n\ Terminate\ the\ player,\ if\ there\ is\ one.\n
|
||||
comment9.params=filename
|
||||
comment9.target=void\ reportProblem(java.lang.String)
|
||||
comment9.text=\n\ Report\ a\ problem\ playing\ the\ given\ file.\n\ @param\ filename\ The\ file\ being\ played.\n
|
||||
numComments=10
|
@@ -0,0 +1,150 @@
|
||||
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 2016.02.29
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
Project: music-organizer-v2. A project to collect audio files.
|
||||
Authors: David J. Barnes and Michael Kölling
|
||||
|
||||
This project is part of the material for the book
|
||||
|
||||
Objects First with Java - A Practical Introduction using BlueJ
|
||||
Sixth edition
|
||||
David J. Barnes and Michael Kölling
|
||||
Pearson Education, 2016
|
||||
|
||||
It is discussed in chapter 4.
|
||||
|
||||
To start:
|
||||
|
||||
Create a MusicOrganizer object on the object bench.
|
||||
Use its methods to add, remove, and play files.
|
||||
|
@@ -0,0 +1,45 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=MusicOrganizer
|
||||
dependency1.to=MusicPlayer
|
||||
dependency1.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=755
|
||||
package.editor.height=385
|
||||
package.editor.width=647
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=1
|
||||
package.numTargets=2
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=652
|
||||
readme.editor.width=874
|
||||
readme.editor.x=41
|
||||
readme.editor.y=23
|
||||
target1.editor.height=700
|
||||
target1.editor.width=900
|
||||
target1.editor.x=76
|
||||
target1.editor.y=35
|
||||
target1.height=60
|
||||
target1.name=MusicPlayer
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=130
|
||||
target1.x=310
|
||||
target1.y=210
|
||||
target2.editor.height=699
|
||||
target2.editor.width=758
|
||||
target2.editor.x=529
|
||||
target2.editor.y=23
|
||||
target2.height=60
|
||||
target2.name=MusicOrganizer
|
||||
target2.naviview.expanded=false
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=130
|
||||
target2.x=150
|
||||
target2.y=110
|
Reference in New Issue
Block a user