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=searchString
|
||||
comment5.target=void\ listMatching(java.lang.String)
|
||||
comment5.text=\n\ List\ the\ names\ of\ files\ matching\ the\ given\ search\ string.\n\ @param\ searchString\ The\ string\ to\ match.\n
|
||||
comment6.params=searchString
|
||||
comment6.target=int\ findFirst(java.lang.String)
|
||||
comment6.text=\n\ Find\ the\ index\ of\ the\ first\ file\ matching\ the\ given\n\ search\ string.\n\ @param\ searchString\ The\ string\ to\ match.\n\ @return\ The\ index\ of\ the\ first\ occurrence,\ or\ -1\ if\n\ \ \ \ \ \ \ \ \ no\ match\ is\ found.\n
|
||||
comment7.params=index
|
||||
comment7.target=void\ removeFile(int)
|
||||
comment7.text=\n\ Remove\ a\ file\ from\ the\ collection.\n\ @param\ index\ The\ index\ of\ the\ file\ to\ be\ removed.\n
|
||||
comment8.params=index
|
||||
comment8.target=void\ startPlaying(int)
|
||||
comment8.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
|
||||
comment9.params=
|
||||
comment9.target=void\ stopPlaying()
|
||||
comment9.text=\n\ Stop\ the\ player.\n
|
||||
numComments=10
|
@@ -0,0 +1,144 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.OptionalInt;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* 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(index >= 0 && index < files.size()) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* List the names of files matching the given search string.
|
||||
* @param searchString The string to match.
|
||||
*/
|
||||
public void listMatching(String searchString)
|
||||
{
|
||||
for(String filename : files) {
|
||||
if(filename.contains(searchString)) {
|
||||
// A match.
|
||||
System.out.println(filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the index of the first file matching the given
|
||||
* search string.
|
||||
* @param searchString The string to match.
|
||||
* @return The index of the first occurrence, or -1 if
|
||||
* no match is found.
|
||||
*/
|
||||
public int findFirst(String searchString)
|
||||
{
|
||||
int index = 0;
|
||||
// Record that we will be searching until a match is found.
|
||||
boolean searching = true;
|
||||
|
||||
while(searching && index < files.size()) {
|
||||
String filename = files.get(index);
|
||||
if(filename.contains(searchString)) {
|
||||
// A match. We can stop searching.
|
||||
searching = false;
|
||||
}
|
||||
else {
|
||||
// Move on.
|
||||
index++;
|
||||
}
|
||||
}
|
||||
if(searching) {
|
||||
// We didn't find it.
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
// Return where it was found.
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,24 @@
|
||||
Project: music-organizer-v4. 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 listMatching method prints all files matching a
|
||||
given string the collection.
|
||||
|
||||
The findFirst method finds the index of a file that
|
||||
matches a search string.
|
||||
|
||||
|
@@ -0,0 +1,45 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=MusicOrganizer
|
||||
dependency1.to=MusicPlayer
|
||||
dependency1.type=UsesDependency
|
||||
objectbench.height=100
|
||||
objectbench.width=774
|
||||
package.editor.height=390
|
||||
package.editor.width=971
|
||||
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=664
|
||||
readme.editor.width=812
|
||||
readme.editor.x=37
|
||||
readme.editor.y=23
|
||||
target1.editor.height=700
|
||||
target1.editor.width=900
|
||||
target1.editor.x=90
|
||||
target1.editor.y=53
|
||||
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=200
|
||||
target2.editor.height=657
|
||||
target2.editor.width=819
|
||||
target2.editor.x=95
|
||||
target2.editor.y=143
|
||||
target2.height=60
|
||||
target2.name=MusicOrganizer
|
||||
target2.naviview.expanded=true
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=140
|
||||
target2.x=140
|
||||
target2.y=120
|
Reference in New Issue
Block a user