00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.sound;
00023
00024 import com.realtime.crossfire.jxclient.util.DebugWriter;
00025 import org.jetbrains.annotations.NotNull;
00026 import org.jetbrains.annotations.Nullable;
00027
00033 public class MusicManager {
00034
00038 @NotNull
00039 private final AudioFileLoader audioFileLoader;
00040
00045 @Nullable
00046 private final DebugWriter debugSound;
00047
00052 @Nullable
00053 private Processor processor = null;
00054
00059 @Nullable
00060 private Thread thread = null;
00061
00065 private boolean enabled = false;
00066
00070 private boolean muted = true;
00071
00076 @Nullable
00077 private String name = null;
00078
00085 public MusicManager(@NotNull final AudioFileLoader audioFileLoader, @Nullable final DebugWriter debugSound) {
00086 this.audioFileLoader = audioFileLoader;
00087 this.debugSound = debugSound;
00088 }
00089
00095 public void play(@Nullable final String name) {
00096 if (this.name == null ? name == null : this.name.equals(name)) {
00097 return;
00098 }
00099
00100 if (debugSound != null) {
00101 debugSound.debugProtocolWrite("play: "+name);
00102 }
00103 this.name = name;
00104 restart();
00105 }
00106
00111 public void setEnabled(final boolean enabled) {
00112 if (this.enabled == enabled) {
00113 return;
00114 }
00115
00116 if (debugSound != null) {
00117 debugSound.debugProtocolWrite("setEnabled: "+enabled);
00118 }
00119 this.enabled = enabled;
00120 restart();
00121 }
00122
00127 public void setMuted(final boolean muted) {
00128 if (this.muted == muted) {
00129 return;
00130 }
00131
00132 if (debugSound != null) {
00133 debugSound.debugProtocolWrite("setMuted: "+muted);
00134 }
00135 this.muted = muted;
00136 restart();
00137 }
00138
00143 private void restart() {
00144 if (processor != null) {
00145 processor.terminate(enabled);
00146 processor = null;
00147 }
00148
00149 if (enabled && !muted && name != null) {
00150 processor = new Processor(name, audioFileLoader);
00151 thread = new Thread(processor, "JXClient:MusicManager");
00152 thread.start();
00153 }
00154 }
00155
00159 public void shutdown() {
00160 if (processor != null) {
00161 processor.terminate(false);
00162 processor = null;
00163 }
00164 if (thread != null) {
00165 try {
00166 thread.join();
00167 } catch (final InterruptedException ex) {
00168 throw new AssertionError(ex);
00169 }
00170 thread = null;
00171 }
00172 }
00173
00174 }