Crossfire JXClient, Trunk  R20561
ClipManager.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 java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import javax.sound.sampled.DataLine;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
30 
36 public class ClipManager {
37 
41  @NotNull
42  private final ExecutorService executorService = Executors.newCachedThreadPool();
43 
47  @NotNull
48  private final ClipCache clipCache;
49 
56  public ClipManager(@NotNull final AudioFileLoader audioFileLoader, @Nullable final DebugWriter debugSound) {
57  clipCache = new ClipCache(audioFileLoader, debugSound);
58  }
59 
65  public void play(@Nullable final String name, @NotNull final String action) {
66  final DataLine clip = clipCache.allocateClip(name, action);
67  if (clip == null) {
68  return;
69  }
70 
71  executorService.execute(() -> {
72  try {
73  clip.start();
74  try {
75  clip.drain();
76  } finally {
77  clip.stop();
78  }
79  } finally {
80  clipCache.freeClip(clip);
81  }
82  });
83  }
84 
88  public void shutdown() {
89  executorService.shutdownNow();
90  }
91 
92 }
ClipManager(@NotNull final AudioFileLoader audioFileLoader, @Nullable final DebugWriter debugSound)
Creates a new instance.
DataLine allocateClip(@Nullable final String name, @NotNull final String action)
Allocates a new clip.
Definition: ClipCache.java:73
void shutdown()
Terminates all running clips and free resources.
Writer debug information to a log file.
void play(@Nullable final String name, @NotNull final String action)
Plays the given sound effect.
final ClipCache clipCache
The clip cache used to allocate new clips.
Manages a set of sound clips (short sound effects).
final ExecutorService executorService
The executor service used to play sound clips.
void freeClip(@NotNull final Line clip)
Deallocates a clip.
Definition: ClipCache.java:81