first commit
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,32 @@
|
||||
#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=
|
||||
comment4.target=void\ listAllFiles()
|
||||
comment4.text=\n\ Show\ a\ list\ of\ all\ the\ files\ in\ the\ collection.\n
|
||||
comment5.params=index
|
||||
comment5.target=void\ removeFile(int)
|
||||
comment5.text=\n\ Remove\ a\ file\ from\ the\ collection.\n\ @param\ index\ The\ index\ of\ the\ file\ to\ be\ removed.\n
|
||||
comment6.params=index
|
||||
comment6.target=void\ startPlaying(int)
|
||||
comment6.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
|
||||
comment7.params=
|
||||
comment7.target=void\ stopPlaying()
|
||||
comment7.text=\n\ Stop\ the\ player.\n
|
||||
comment8.params=index
|
||||
comment8.target=void\ playAndWait(int)
|
||||
comment8.text=\n\ Play\ a\ file\ in\ the\ collection.\ Only\ return\ once\ playing\ has\ finished.\n\ @param\ index\ The\ index\ of\ the\ file\ to\ be\ played.\n
|
||||
comment9.params=index
|
||||
comment9.target=boolean\ validIndex(int)
|
||||
comment9.text=\n\ Determine\ whether\ the\ given\ index\ is\ valid\ for\ the\ collection.\n\ Print\ an\ error\ message\ if\ it\ is\ not.\n\ @param\ index\ The\ index\ to\ be\ checked.\n\ @return\ true\ if\ the\ index\ is\ valid,\ false\ otherwise.\n
|
||||
numComments=10
|
@@ -0,0 +1,134 @@
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* A class to hold details of audio 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(validIndex(index)) {
|
||||
String filename = files.get(index);
|
||||
System.out.println(filename);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a list of all the files in the collection.
|
||||
*/
|
||||
public void listAllFiles()
|
||||
{
|
||||
for(String filename : files) {
|
||||
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(validIndex(index)) {
|
||||
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)
|
||||
{
|
||||
if(validIndex(index)) {
|
||||
String filename = files.get(index);
|
||||
player.startPlaying(filename);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the player.
|
||||
*/
|
||||
public void stopPlaying()
|
||||
{
|
||||
player.stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Play a file in the collection. Only return once playing has finished.
|
||||
* @param index The index of the file to be played.
|
||||
*/
|
||||
public void playAndWait(int index)
|
||||
{
|
||||
if(validIndex(index)) {
|
||||
String filename = files.get(index);
|
||||
player.playSample(filename);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the given index is valid for the collection.
|
||||
* Print an error message if it is not.
|
||||
* @param index The index to be checked.
|
||||
* @return true if the index is valid, false otherwise.
|
||||
*/
|
||||
private boolean validIndex(int index)
|
||||
{
|
||||
// The return value.
|
||||
// Set according to whether the index is valid or not.
|
||||
boolean valid;
|
||||
|
||||
if(index < 0) {
|
||||
System.out.println("Index cannot be negative: " + index);
|
||||
valid = false;
|
||||
}
|
||||
else if(index >= files.size()) {
|
||||
System.out.println("Index is too large: " + index);
|
||||
valid = false;
|
||||
}
|
||||
else {
|
||||
valid = true;
|
||||
}
|
||||
return valid;
|
||||
}
|
||||
}
|
@@ -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,18 @@
|
||||
Project: music-organizer-v3. 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. The listAllFiles
|
||||
method prints a list of the collection.
|
||||
|
@@ -0,0 +1,45 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=MusicOrganizer
|
||||
dependency1.to=MusicPlayer
|
||||
dependency1.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=771
|
||||
package.editor.height=390
|
||||
package.editor.width=663
|
||||
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=654
|
||||
readme.editor.width=869
|
||||
readme.editor.x=39
|
||||
readme.editor.y=23
|
||||
target1.editor.height=700
|
||||
target1.editor.width=900
|
||||
target1.editor.x=181
|
||||
target1.editor.y=51
|
||||
target1.height=60
|
||||
target1.name=MusicPlayer
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=130
|
||||
target1.x=300
|
||||
target1.y=220
|
||||
target2.editor.height=766
|
||||
target2.editor.width=888
|
||||
target2.editor.x=23
|
||||
target2.editor.y=27
|
||||
target2.height=60
|
||||
target2.name=MusicOrganizer
|
||||
target2.naviview.expanded=true
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=130
|
||||
target2.x=150
|
||||
target2.y=120
|
Reference in New Issue
Block a user