Crossfire JXClient, Trunk  R20561
MusicManager.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.sound;
23 
25 import org.jetbrains.annotations.NotNull;
26 import org.jetbrains.annotations.Nullable;
27 
33 public class MusicManager {
34 
38  @NotNull
40 
45  @Nullable
46  private final DebugWriter debugSound;
47 
52  @Nullable
54 
59  @Nullable
60  private Thread thread;
61 
65  private boolean enabled;
66 
70  private boolean muted = true;
71 
76  @Nullable
77  private String name;
78 
85  public MusicManager(@NotNull final AudioFileLoader audioFileLoader, @Nullable final DebugWriter debugSound) {
86  this.audioFileLoader = audioFileLoader;
87  this.debugSound = debugSound;
88  }
89 
95  public void play(@Nullable final String name) {
96  if (this.name == null ? name == null : this.name.equals(name)) {
97  return;
98  }
99 
100  if (debugSound != null) {
101  debugSound.debugProtocolWrite("play: "+name);
102  }
103  this.name = name;
104  restart();
105  }
106 
111  public void setEnabled(final boolean enabled) {
112  if (this.enabled == enabled) {
113  return;
114  }
115 
116  if (debugSound != null) {
117  debugSound.debugProtocolWrite("setEnabled: "+enabled);
118  }
119  this.enabled = enabled;
120  restart();
121  }
122 
127  public void setMuted(final boolean muted) {
128  if (this.muted == muted) {
129  return;
130  }
131 
132  if (debugSound != null) {
133  debugSound.debugProtocolWrite("setMuted: "+muted);
134  }
135  this.muted = muted;
136  restart();
137  }
138 
143  private void restart() {
144  if (processor != null) {
145  processor.terminate(enabled);
146  processor = null;
147  }
148 
149  if (enabled && !muted && name != null) {
150  processor = new Processor(name, audioFileLoader);
151  thread = new Thread(processor, "JXClient:MusicManager");
152  thread.start();
153  }
154  }
155 
159  public void shutdown() {
160  if (processor != null) {
161  processor.terminate(false);
162  processor = null;
163  }
164  if (thread != null) {
165  try {
166  thread.join();
167  } catch (final InterruptedException ex) {
168  throw new AssertionError(ex);
169  }
170  thread = null;
171  }
172  }
173 
174 }
boolean muted
Whether background music is muted.
void setMuted(final boolean muted)
Sets whether background music is muted.
final AudioFileLoader audioFileLoader
The AudioFileLoader for loading audio files.
void debugProtocolWrite(@NotNull final CharSequence str)
Writes a message to the debug protocol.
A thread that plays a music file over and over until terminated.
Definition: Processor.java:38
Writer debug information to a log file.
void shutdown()
Terminates a playing background music and free resources.
void play(@Nullable final String name)
Plays the given music.
Thread thread
The Thread executing processor.
void terminate(final boolean fadeOut)
Stops playing music.
Definition: Processor.java:90
final DebugWriter debugSound
The writer for logging sound related information or.
MusicManager(@NotNull final AudioFileLoader audioFileLoader, @Nullable final DebugWriter debugSound)
Creates a new instance.
void setEnabled(final boolean enabled)
Sets whether background music is enabled.
boolean enabled
Whether background music is enabled.
String name
The currently playing music name.
Processor processor
The currently running processor, or.
void restart()
Restarts the current music.