I have written this code for playing music, this code plays music once but I want to play it continuously in the loop please tell how I can do that?
import java.io.FileInputStream;
import sun.audio.*;
public class A {
public static void main(String arg[]) throws Exception {
AudioPlayer MGP = AudioPlayer.player;
AudioStream BGM = new AudioStream(new FileInputStream("sounds.wav"));
AudioPlayer.player.start(BGM);
}
}
According to the documentation, start will cause the player to:
Start playing a stream. The stream will continue to play until the stream runs out of data, or it is stopped.
Hence, you could just wrap it in an endless loop:
public static void main(String arg[]) throws Exception {
AudioPlayer MGP = AudioPlayer.player;
while(true) {
AudioStream BGM = new AudioStream(new FileInputStream("sounds.wav"));
AudioPlayer.player.start(BGM);
BGM.close();
}
}
}
Related
When I start playing music by javazoom library, console stops answer. I can write anything, but no response.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.advanced.AdvancedPlayer;
public class Main {
public static void men(int i) {
try {
FileInputStream fis = new FileInputStream("Filename.WAV");
AdvancedPlayer player = new AdvancedPlayer(fis);
player.play();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(JavaLayerException e) {
e.printStackTrace();
}
}
public static Scanner scn = new Scanner(System.in);
public static void main(String[] args) {
while(true) {
int i = scn.nextInt();
if(i == 1) {
men(i);
}
if(i == 5) {
System.out.println("i am here");
}
}
}
}
Is there solution? Or javazoom can't play audio and do other things?
I'm not clear what you mean by "console stops answer" and "Javazoom can't play audio and do other things."
You can execute the javazoom play command in its own thread. While that thread runs concurrently, your program will be free to execute other tasks.
Do you know how to create and launch a thread? Here is Oracle's tutorial Processes and Threads.
I have the next class where it stores all the sounds i'm gonna use for a game i'm doing, however i'm trying to test the Jar file to see if is working properly and I find the next error from the command prompt
Exception in thread "main" java.lang.NullPointerException
at com.sun.media.sound.StandardMidiFileReader.getSequence(Unknown Source)
at javax.sound.midi.MidiSystem.getSequence(Unknown Source)
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at Assets.AudioStreaming.loadMusicAssets(AudioStreaming.java:27)
at CrossyMain.MainMenu.<init>(MainMenu.java:64)
at CrossyMain.Principal.main(Principal.java:18)
This is the class where i'm storing all the audio assets
package Assets;
import java.io.IOException;
import javax.sound.sampled.*;
public class AudioStreaming {
private static Clip mainMenu,BGM,coinP,gameOver,trainAlert,birdGO;
private static AudioInputStream in_mainMenu,in_BGM,in_coinP,in_gameOver,in_trainAlert,in_birdGO;
public static void loadMusicAssets(){
try{
/*Background*/
BGM = AudioSystem.getClip();
in_BGM = AudioSystem.getAudioInputStream(AudioStreaming.class.getResource("/sound/background/NeonW.wav"));
BGM.open(in_BGM);
coinP = AudioSystem.getClip();
in_coinP = AudioSystem.getAudioInputStream(AudioStreaming.class.getResource("/sound/entityFx/coinPickup.wav"));
coinP.open(in_coinP);
gameOver = AudioSystem.getClip();
in_gameOver = AudioSystem.getAudioInputStream(AudioStreaming.class.getResource("/sound/entityFx/gameOver.wav"));
gameOver.open(in_gameOver);
trainAlert = AudioSystem.getClip();
in_trainAlert = AudioSystem.getAudioInputStream(AudioStreaming.class.getResource("/sound/entityFx/trainWarning.wav"));
trainAlert.open(in_trainAlert);
birdGO = AudioSystem.getClip();
in_birdGO = AudioSystem.getAudioInputStream(AudioStreaming.class.getResource("/sound/entityFx/eaglePickup.wav"));
birdGO.open(in_birdGO);
mainMenu = AudioSystem.getClip();
in_mainMenu = AudioSystem.getAudioInputStream(AudioStreaming.class.getResource("/sound/background/NeonValley.wav"));
mainMenu.open(in_mainMenu);
}catch(LineUnavailableException | UnsupportedAudioFileException | IOException e){
e.printStackTrace();
}
}
public static void playBGM(){
BGM.setFramePosition(0);
BGM.start();
}
public static void stopBGM(){
BGM.stop();
BGM.close();
}
public static void playMainMenu(){
mainMenu.setFramePosition(0);
mainMenu.start();
}
public static void stopMainMenu(){
mainMenu.stop();
mainMenu.close();
}
public static void playTrainWarning(){
trainAlert.setFramePosition(0);
trainAlert.start();
}
public static void stopTrainWarning(){
trainAlert.stop();
trainAlert.close();
}
public static void playBirdGO(){
birdGO.setFramePosition(0);
birdGO.start();
}
public static void playCoinP(){
coinP.setFramePosition(0);
coinP.start();
}
public static void playGameOver(){
gameOver.setFramePosition(0);
gameOver.start();
}
}
It works perfectly within NetBeans with no errors whatsoever, however it sucessfully compiles the JAR file but i noticed this error with the command prompt cause it didn't want to open by just double clicking it? has anyone experienced this error? if so, how did you manage to load your sound assets to a Jar file? any tips/advices would be highly appreciated thanks for your time!
import javax.swing.*;
import sun.audio.*;
import java.awt.event.*;
import java.io.*;
public class Sound {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(200,200);
JButton button = new JButton("Clcik me");
frame.add(button);
button.addActionListener(new AL());
frame.show(true);
}
public static class AL implements ActionListener{
public final void actionPerformed(ActionEvent e){
music();
}
}
public static void music(){
AudioPlayer MGP = AudioPlayer.player;
AudioStream BGM;
AudioData MD;
ContinuousAudioDataStream loop = null;
try{
BGM = new AudioStream(new FileInputStream("backgroundmusic.wav"));
MD = BGM.getData();
loop = new ContinuousAudioDataStream(MD);
}catch(IOException error){
error.printStackTrace();
}
MGP.start(loop);
}
}
When I run this code, I see following warnings. Why and how do I avoid that?
Sound.java:22: warning: AudioPlayer is internal proprietary API and may be removed in a future release
Sound.java:23: warning: AudioStream is internal proprietary API and may be removed in a future release
Sound.java:24: warning: AudioData is internal proprietary API and may be removedin a future release
Sound.java:25: warning: ContinuousAudioDataStream is internal proprietary API and may be removed in a future release
Sound.java:27: warning: AudioStream is internal proprietary API and may be removed in a future release
Sound.java:29: warning: ContinuousAudioDataStream is internal proprietary API and may be removed in a future release
Note: Sound.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
7 warnings
First, you should be using the classes in the javax.sound.sampled package to avoid the compiler warnings. Second, when using these classes, you'll also need to drive them from a thread in the background.
Here's one a wrote awhile ago. There are better ways to do it now than to sleep in a loop, but it works for quick and easy wav files, and you can adapt the code if you need to. A clever implementation might even be able to drive several audio files from the same thread.
Plays an audio file:
import java.io.InputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
public class SoundThread extends Thread {
private final String resource;
public static void play(String resource) {
Thread t = new SoundThread(resource);
t.setDaemon(true);
t.start();
}
public SoundThread(String resource) {
this.resource = resource;
}
#Override
public void run() {
Clip clip = null;
try {
InputStream in = SoundThread.class.getClassLoader().getResourceAsStream(resource);
if(in != null) {
AudioInputStream stream = AudioSystem.getAudioInputStream(in);
AudioFormat format = stream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
clip = (Clip) AudioSystem.getLine(info);
clip.open(stream);
clip.loop(0);
do {
try {
Thread.sleep(100);
} catch(InterruptedException iex) {
// bad form on my part here, should do something
}
} while(clip.isRunning());
}
} catch (Exception e) {
x.printStackTrace(System.out);
} finally {
try {
if(clip != null) {
clip.close();
}
} catch(Exception x) {
x.printStackTrace(System.out);
}
}
}
}
Example of how to call it:
SoundThread.play("resources/cashregister6.wav");
I have written this code for playing music in my java program, but I want to play the music continuously in the loop, for that I have tried an infinite loop as well but it is not working. Please tell how it possible to play the music continuously?
import java.io.FileInputStream;
import sun.audio.*;
public class A {
public static void main(String arg[]) throws Exception {
AudioPlayer MGP = AudioPlayer.player;
AudioStream BGM = new AudioStream(new FileInputStream("sounds.wav"));
AudioPlayer.player.start(BGM);
}
}
From the JavaDoc:
To play a continuous sound you first have to create an AudioData instance and use it to construct a ContinuousAudioDataStream. For example:
AudioData data = new AudioStream(url.openStream()).getData();
ContinuousAudioDataStream audiostream = new ContinuousAudioDataStream(data);
AudioPlayer.player.start(audiostream);
I really didn't think it would be that difficult to adapt the docs.
import java.io.FileInputStream;
import sun.audio.*;
public class A {
public static void main(String arg[]) throws Exception {
AudioData data = new AudioStream(new FileInputStream("yourfile.wav")).getData();
ContinuousAudioDataStream BGM = new ContinuousAudioDataStream(data);
AudioPlayer.player.start(BGM);
}
}
I am trying to make background music for my game, but when I try to get the file, it brings up the error File Not Found. Here is my code:
package TBG;
import sun.audio.*;
import java.io.*;
public class Sound {
public static void main(String[] args) {
//File here = new File("Slow.wav");
//System.out.println(here.getAbsolutePath());
music();
}
public static void music(){
AudioPlayer MGP = AudioPlayer.player;
AudioData MD;
ContinuousAudioDataStream loop = null;
try{
AudioStream BGM = new AudioStream(new FileInputStream("C:/Users/GrffinStout/Documents/Eclipse Stuff/G Text RPG/Slow.wav"));
MD = BGM.getData();
loop = new ContinuousAudioDataStream(MD);
}catch(IOException error){}
MGP.start(loop);
}
}
Mac file paths don't use C: - that is only for Windows.
On Macs (or any Unix or Linux system) absolute file paths start with / so you just want /Users/GrffinStout/Documents/Eclipse Stuff/G Text RPG/Slow.wav.