package sounds;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.HashMap;
import javax.sound.sampled.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class Playlist
{
private Clip activeClip;
/**
* Creates an audio clip from a sound file.
*/
public Clip createClip (String soundFile)
{
String fileName = "/sounds/" + soundFile + ".wav";
try (BufferedInputStream sound = new BufferedInputStream(getClass().getResourceAsStream(fileName)))
{
// Create and return a Clip that will play a sound file. There are
// various reasons that the creation attempt could fail. If it
// fails, return null.
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(sound));
return clip;
}
catch (LineUnavailableException e)
{
return null;
}
catch (IOException e)
{
return null;
}
catch (UnsupportedAudioFileException e)
{
return null;
}
}
public void play (String sound)
{
Clip clip = createClip(sound);
if (clip != null)
{
if (clip.isRunning())
{
clip.stop();
}
clip.setFramePosition(0);
clip.start();
}
}
public void startLoop (String sound)
{
Clip clip = createClip(sound);
if (clip != null)
{
if (clip.isRunning())
{
clip.stop();
}
clip.setFramePosition(0);
clip.loop(Clip.LOOP_CONTINUOUSLY);
activeClip = clip;
}
}
public void stopLoop ()
{
if (activeClip != null) {
if (activeClip.isRunning()) {
activeClip.stop();
}
}
}
}
所以我的主要问题是循环。当我有一个对象以一个音频文件开始循环时,它开始并停止正常,但是当我有两个对象同时调用startLoop时,只有最近的一个会停止而另一个将继续运行。我已经考虑过将每个带有名称的剪辑放入HashMap中,这样我就可以按名称区分每个剪辑但不断得到NullPointerException错误,而不管我如何将其“放入”Map中
这是我尝试将其放入HashMap的版本。 基本相似的代码,但每次调用play或startloop时,字符串都充当键并存储剪辑,以便稍后可以检索它以停止循环。但是每次我这样做,我都会在线上得到NullPointerException:我试图把它放到Map中:
package sounds;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.HashMap;
import javax.sound.sampled.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class Playlist
{
//private Clip activeClip;
//Here
private HashMap<String, Clip> list;
/**
* Creates an audio clip from a sound file.
*/
public Clip createClip (String soundFile)
{
String fileName = "/sounds/" + soundFile + ".wav";
// Opening the sound file this way will work no matter how the
// project is exported. The only restriction is that the
// sound files must be stored in a package.
try (BufferedInputStream sound = new BufferedInputStream(getClass().getResourceAsStream(fileName)))
{
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(sound));
return clip;
}
catch (LineUnavailableException e)
{
return null;
}
catch (IOException e)
{
return null;
}
catch (UnsupportedAudioFileException e)
{
return null;
}
}
public void play (String sound)
{
Clip clip = createClip(sound);
//Here
list.put(sound, clip);
if (clip != null)
{
if (clip.isRunning())
{
clip.stop();
}
clip.setFramePosition(0);
clip.start();
}
}
public void startLoop (String sound)
{
Clip clip = createClip(sound);
//Here
list.put(soundFile, clip);
if (clip != null)
{
if (clip.isRunning())
{
clip.stop();
}
clip.setFramePosition(0);
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
}
public void stopLoop (String sound)
{
//Here
Clip clip = (Clip) list.get(sound);
if (clip != null) {
if (clip.isRunning()) {
clip.stop();
}
}
}
}
答案 0 :(得分:0)
您没有实例化名为“list”的HashMap。
private HashMap<String, Clip> list = new HashMap<>();