Playing music continuously in the loop in java - java

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);
}
}

Related

Java: Console stops working when using javazoom

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.

Javafx won't play mp3

I want to run mp3 without any videos with java.
import javafx.embed.swing.JFXPanel;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
public class Guess {
public static void main(String[] args) throws Exception {
new JFXPanel();
Media m = new Media("file:///D://Guess_the_Song//resources//new.m4a");
MediaPlayer p = new MediaPlayer(m);
p.play();
}
}
This code doesn't lead me to any errors, but I can't hear any sound...
Try this:
path = "D:/Guess_the_Song/resources/new.m4a";
Media m = new Media(new File(path).toURI().toString());

In Java playing back audio using a clip isn't working with no error messages

I've been trying to use the "Trail: Sound" from Oracle and I've gotten to this part. https://docs.oracle.com/javase/tutorial/sound/playing.html#113609
"Using a Clip"
Well I tried to follow the directions which aren't exactly specific and I made some code that seems like it should work. It basically matches examples I found online that work for other people. On my machine nothing happens the program ends without playing any sound right after I start it. It doesn't say any errors.
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Clip;
import javax.sound.sampled.AudioFormat;
import java.io.File;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
class Demo{
public static void main(String[] args) throws LineUnavailableException, UnsupportedAudioFileException, IOException{
File file = new File("song1.wav");
AudioFileFormat audioFileFormat = AudioSystem.getAudioFileFormat(file);
AudioFormat audioFormat = audioFileFormat.getFormat();
javax.sound.sampled.DataLine.Info dataLineInfo = new javax.sound.sampled.DataLine.Info(Clip.class,audioFormat);
Line theLine = AudioSystem.getLine(dataLineInfo);
Clip clip = (Clip)theLine;
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
clip.open(audioInputStream);
clip.start();
}
}
EDIT: I figured out after clip.start() I needed to keep the program open.
I use that class to play MP# sounds and works for me
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javazoom.jl.player.Player;
import monitorbixao.Main;
/**
*
* #author C007329
*/
public class MusicPlayer extends Thread {
private File music;
private Player player;
public MusicPlayer(File music) {
this.music = music;
}
#Override
public void run() {
play();
}
public void play() {
try {
FileInputStream stream = new FileInputStream(music);
BufferedInputStream buffer = new BufferedInputStream(stream);
this.player = new Player(buffer);
//System.out.println("Executando...");
this.player.play();
//System.out.println("Terminado");
} catch (Exception e) {
//System.out.println("Erro!");
Main.logApp.addMsgLog(MusicPlayer.class.getCanonicalName(), e.getMessage());
e.printStackTrace();
}
}
public void close() {
this.player.close();
//System.out.println("Interrompido...");
}
}
I figured it out. I needed to keep the program open so after "clip.start();" I added this code
java.io.Console c = System.console();
if (c == null) {
System.err.println("No console.");
System.exit(1);
}
String nothing = c.readLine("Wait... ");
:D

playing music continuously in the loop

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();
}
}
}

AudioStream cannot find file Java

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.

Categories