Support playing background music for desktop game

This commit is contained in:
drdev
2014-06-29 15:56:40 +00:00
parent b6a2b283cb
commit b1f2a7e05f
4 changed files with 115 additions and 43 deletions

View File

@@ -242,6 +242,11 @@
<artifactId>freemarker</artifactId> <artifactId>freemarker</artifactId>
<version>2.3.20</version> <version>2.3.20</version>
</dependency> </dependency>
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>jlayer</artifactId>
<version>1.0.1-1</version>
</dependency>
</dependencies> </dependencies>
<profiles> <profiles>

View File

@@ -1,67 +1,114 @@
package forge.sound; package forge.sound;
import java.io.File; import java.io.BufferedInputStream;
/*import java.io.IOException; import java.io.FileInputStream;
import javax.sound.sampled.AudioFormat; import javazoom.jl.player.advanced.AdvancedPlayer;
import javax.sound.sampled.AudioInputStream; import javazoom.jl.player.advanced.PlaybackEvent;
import javax.sound.sampled.AudioSystem; import javazoom.jl.player.advanced.PlaybackListener;
import javax.sound.sampled.DataLine.Info;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;*/
public class AudioMusic implements IAudioMusic { public class AudioMusic implements IAudioMusic {
private final File file; private AdvancedPlayer musicPlayer;
private FileInputStream fileStream;
private BufferedInputStream bufferedStream;
private boolean canResume;
private String filename;
private int total;
private int stopped;
private boolean valid;
private Runnable onComplete;
public AudioMusic(String filename) { public AudioMusic(String filename0) {
file = new File(filename); filename = filename0;
} }
public void play(final Runnable onComplete) { public void play(final Runnable onComplete0) {
/*try (final AudioInputStream in = AudioSystem.getAudioInputStream(file)) { onComplete = onComplete0;
final AudioFormat outFormat = getOutFormat(in.getFormat()); play(-1);
final Info info = new Info(SourceDataLine.class, outFormat); }
try (final SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info)) { private boolean play(int pos) {
if (line != null) { valid = true;
line.open(outFormat); canResume = false;
line.start(); try {
stream(AudioSystem.getAudioInputStream(outFormat, in), line); fileStream = new FileInputStream(filename);
line.drain(); total = fileStream.available();
line.stop(); if (pos > -1) {
fileStream.skip(pos);
}
bufferedStream = new BufferedInputStream(fileStream);
musicPlayer = new AdvancedPlayer(bufferedStream);
musicPlayer.setPlayBackListener(new PlaybackListener() {
@Override
public void playbackFinished(PlaybackEvent evt) {
if (onComplete != null) {
onComplete.run();
} }
} }
} catch (UnsupportedAudioFileException });
| LineUnavailableException new Thread(
| IOException e) { new Runnable(){
throw new IllegalStateException(e); public void run(){
}*/ try {
musicPlayer.play();
}
catch (Exception e){
e.printStackTrace();
valid = false;
}
}
}
).start();
}
catch (Exception e) {
e.printStackTrace();
valid = false;
}
return valid;
} }
public void pause() { public void pause() {
if (musicPlayer == null) { return; }
try {
stopped = fileStream.available();
close();
if (valid) {
canResume = true;
}
}
catch (Exception e) {
e.printStackTrace();
}
}
private void close() {
musicPlayer.setPlayBackListener(null); //prevent firing if closed
musicPlayer.close();
fileStream = null;
bufferedStream = null;
musicPlayer = null;
} }
public void resume() { public void resume() {
if (!canResume) { return; }
if (play(total - stopped)) {
canResume = false;
}
} }
public void stop() { public void stop() {
if (musicPlayer == null) { return; }
musicPlayer.setPlayBackListener(null); //prevent firing if stopped manually
musicPlayer.stop();
} }
public void dispose() { public void dispose() {
} if (musicPlayer == null) { return; }
/*private AudioFormat getOutFormat(AudioFormat inFormat) { close();
final int ch = inFormat.getChannels(); canResume = false;
final float rate = inFormat.getSampleRate();
return new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, rate, 16, ch, ch * 2, rate, false);
} }
private void stream(AudioInputStream in, SourceDataLine line)
throws IOException {
final byte[] buffer = new byte[4096];
for (int n = 0; n != -1; n = in.read(buffer, 0, buffer.length)) {
line.write(buffer, 0, n);
}
}*/
} }

View File

@@ -1,5 +1,6 @@
package forge.view; package forge.view;
import forge.Singletons;
import forge.gui.framework.SDisplayUtil; import forge.gui.framework.SDisplayUtil;
import forge.gui.framework.SResizingUtil; import forge.gui.framework.SResizingUtil;
import forge.model.FModel; import forge.model.FModel;
@@ -46,8 +47,19 @@ public class FFrame extends SkinnedFrame implements ITitleBarOwner {
this.lockTitleBar = this.isMainFrame && FModel.getPreferences().getPrefBoolean(FPref.UI_LOCK_TITLE_BAR); this.lockTitleBar = this.isMainFrame && FModel.getPreferences().getPrefBoolean(FPref.UI_LOCK_TITLE_BAR);
addResizeSupport(); addResizeSupport();
this.addWindowListener(new WindowAdapter() { this.addWindowListener(new WindowAdapter() {
@Override
public void windowActivated(WindowEvent e) {
if (isMainFrame) { //resume music when main frame regains focus
Singletons.getControl().getSoundSystem().resume();
}
}
@Override @Override
public void windowDeactivated(WindowEvent arg0) { public void windowDeactivated(WindowEvent arg0) {
if (isMainFrame) { //pause music when main frame loses focus
Singletons.getControl().getSoundSystem().pause();
}
if (fullScreen && arg0.getOppositeWindow() == null) { if (fullScreen && arg0.getOppositeWindow() == null) {
setMinimized(true); //minimize if switching from Full Screen Forge to outside application window setMinimized(true); //minimize if switching from Full Screen Forge to outside application window
} }

View File

@@ -8,6 +8,14 @@ Forge Beta: 0#-##-2014 ver 1.5.21
Release Notes Release Notes
------------- -------------
- Introducing Background Music -
Forge now supports playing background music!!!
There are 4 tracks for the menu screens and 4 tracks just for the match screen.
Tracks shuffle randomly as they finish, as well as when starting a new game or leaving the match screen.
A new "Enable Music" setting allows turning the music off, and toggling this setting can also be used to force a shuffle.
The music is paused automatically if you minimize or switch away from Forge, and resumed when it's activated again.
- New Magic 2015 cards - - New Magic 2015 cards -
We have added a branch to our SVN for the new cards that are currently being scripted. These cards are not yet available in this build of forge. Please be patient and they will soon become available. We have added a branch to our SVN for the new cards that are currently being scripted. These cards are not yet available in this build of forge. Please be patient and they will soon become available.