Skip to content

Commit b9f4f63

Browse files
committedMar 18, 2018
18.03.2018
Signed-off-by: Ralph Niemitz <ralph.niemitz@gmx.de>
1 parent 6d00e09 commit b9f4f63

File tree

23 files changed

+1071
-282
lines changed

23 files changed

+1071
-282
lines changed
 

‎README.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[![Build Result](https://travis-ci.org/RalleYTN/SimpleAudio.svg?branch=master)](https://travis-ci.org/RalleYTN/SimpleAudio)
2+
[![Coverage Status](https://coveralls.io/repos/github/RalleYTN/SimpleAudio/badge.svg?branch=master)](https://coveralls.io/github/RalleYTN/SimpleAudio?branch=master)
23
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/87a04f7e6823474a83b49daf6acc6e23)](https://www.codacy.com/app/ralph.niemitz/SimpleAudio?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=RalleYTN/SimpleAudio&amp;utm_campaign=Badge_Grade)
34

45
# Description
@@ -19,6 +20,8 @@ Code written with it, is highly readable and maintainable.
1920
- Moved everything static in the `Audio` interface to `AbstractAudio`
2021
- Fixed a bug that caused `BufferedAudio.isPlaying()` to return a wrong value
2122
- Fixed a bug that would re-open an instance of `StreamedAudio` if it was closed in the `REACHED_END` event
23+
- Added a close method in the `Playlist` class
24+
- Fixed most threading issues in `StreamedAudio`
2225

2326
### Version 2.0.0 (incompatible with older versions of this library)
2427

File renamed without changes.
File renamed without changes.

‎src/main/java/de/ralleytn/simple/audio/AbstractAudio.java

+20-11
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
* SOFTWARE.
2323
*/
24-
2524
package de.ralleytn.simple.audio;
2625

2726
import java.io.File;
@@ -66,18 +65,22 @@
6665
*/
6766
public abstract class AbstractAudio implements Audio {
6867

68+
// ==== 18.03.2018 | Ralph Niemitz/RalleYTN(ralph.niemitz@gmx.de)
69+
// Made the trigger methods synchronized to prevent concurrent access on the listeners
70+
// ====
71+
6972
/**
7073
* @since 1.0.0
7174
*/
7275
public static final int LOOP_ENDLESS = -1;
7376

74-
protected volatile URL resource;
75-
protected volatile FileFormat fileFormat;
76-
protected volatile AudioInputStream audioInputStream;
77-
protected volatile HashMap<String, Control> controls;
78-
protected volatile boolean open;
79-
protected volatile boolean paused;
80-
protected volatile List<AudioListener> listeners = new ArrayList<>();
77+
protected URL resource;
78+
protected FileFormat fileFormat;
79+
protected AudioInputStream audioInputStream;
80+
protected HashMap<String, Control> controls;
81+
protected boolean open;
82+
protected boolean paused;
83+
protected List<AudioListener> listeners = new ArrayList<>();
8184

8285
/**
8386
* @param file name of the resource file
@@ -267,6 +270,11 @@ public AbstractAudio(URI uri) throws AudioException {
267270
*/
268271
public static Mixer.Info[] getPorts() {
269272

273+
// FIXME
274+
// ==== 17.03.2018 | Ralph Niemitz/RalleYTN(ralph.niemitz@gmx.de)
275+
// AudioSystem.getMixerInfo cuts some of the names for no good reason
276+
// ====
277+
270278
return AudioSystem.getMixerInfo();
271279
}
272280

@@ -562,7 +570,7 @@ public boolean isPaused() {
562570
* @param type the event type
563571
* @since 1.1.0
564572
*/
565-
protected void trigger(AudioEvent.Type type) {
573+
protected synchronized void trigger(AudioEvent.Type type) {
566574

567575
AudioEvent event = new AudioEvent(this, type);
568576
this.listeners.forEach(listener -> listener.update(event));
@@ -575,7 +583,7 @@ protected void trigger(AudioEvent.Type type) {
575583
* @param newVal the new value
576584
* @since 1.1.0
577585
*/
578-
protected void trigger(AudioEvent.Type type, Object oldVal, Object newVal) {
586+
protected synchronized void trigger(AudioEvent.Type type, Object oldVal, Object newVal) {
579587

580588
AudioEvent event = new AudioEvent(this, type, oldVal, newVal);
581589
this.listeners.forEach(listener -> listener.update(event));
@@ -628,7 +636,8 @@ private static final URL extractZipEntry(ZipFile zip, String entry) throws IOExc
628636
File tempDir = new File(System.getProperty("java.io.tmpdir"));
629637
File extracted = new File(tempDir, "simple-audio_buffered-" + System.nanoTime() + extension);
630638

631-
try(InputStream input = zip.getInputStream(zipEntry); FileOutputStream output = new FileOutputStream(extracted)) {
639+
try(InputStream input = zip.getInputStream(zipEntry);
640+
FileOutputStream output = new FileOutputStream(extracted)) {
632641

633642
int readBytes = 0;
634643

‎src/main/java/de/ralleytn/simple/audio/BufferedAudio.java

+20-12
Original file line numberDiff line numberDiff line change
@@ -244,22 +244,30 @@ public void open() throws AudioException {
244244
@Override
245245
public void close() {
246246

247-
if(this.isPlaying()) {
248-
249-
this.stop();
250-
}
251-
252-
this.clip.flush();
253-
this.clip.close();
254-
this.controls.clear();
247+
// ==== 18.03.2018 | Ralph Niemitz/RalleYTN(ralph.niemitz@gmx.de)
248+
// Fixed a NullPointerException that was thrown if this method is called without #open being called first.
249+
// ====
255250

256-
try {
251+
if(this.open) {
257252

258-
this.audioInputStream.close();
253+
if(this.isPlaying()) {
254+
255+
this.stop();
256+
}
257+
258+
this.clip.flush();
259+
this.clip.close();
260+
this.controls.clear();
261+
262+
try {
263+
264+
this.audioInputStream.close();
265+
266+
} catch(IOException exception) {}
259267

260-
} catch(IOException exception) {}
268+
this.open = false;
269+
}
261270

262-
this.open = false;
263271
this.trigger(AudioEvent.Type.CLOSED);
264272
}
265273

‎src/main/java/de/ralleytn/simple/audio/Playlist.java

+9
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,15 @@ public float getVolume() {
317317
return this.volume;
318318
}
319319

320+
/**
321+
* Calls the {@link Audio#close()} method for all tracks in this playlist.
322+
* @since 2.0.0
323+
*/
324+
public void close() {
325+
326+
this.tracks.forEach(Audio::close);
327+
}
328+
320329
/**
321330
* Starts the next track.
322331
* @since 1.1.0

‎src/main/java/de/ralleytn/simple/audio/PlaylistEvent.java

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
* SOFTWARE.
2323
*/
24-
2524
package de.ralleytn.simple.audio;
2625

2726
/**

0 commit comments

Comments
 (0)