Why is this program not playing my music files? - java

I was trying to create a basic music player in Java using JLayer library, but it doesn't seem to work. Nothing is played when I open a file (.mp3) through JFileChooser. Below is the code for my application. Please tell me what's wrong in it.
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javax.swing.JFileChooser;
import javazoom.jl.player.Player;
import javazoom.jl.decoder.JavaLayerException;
class MusicPlayer
{
public void Player()
{
JFileChooser chooser = new JFileChooser();
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
try
{
File track = chooser.getSelectedFile();
FileInputStream trackstream = new FileInputStream(track);
BufferedInputStream bufferedtrack = new BufferedInputStream(trackstream);
try
{
Player player = new Player(bufferedtrack);
}
catch(JavaLayerException e)
{
System.out.println("Can't open file!");
}
}
catch(FileNotFoundException e)
{
}
}
else {
}
}
}
public class PlayerApp {
public static void main(String[] args) {
new MusicPlayer().Player();
}
}

You did miss out player.play() of the Player to start playing the requested file.
try
{
Player player = new Player(bufferedtrack);
player.play(); // add this line
}
catch(JavaLayerException e)
{
System.out.println("Can't open file!");
}
Also, method names are commonly written in camel-case (first letter lowercase). I recommend to rename your method void Player() to something like void loadDialogAndPlayFile().

You forgot to call the player.play(); method after Player player = new Player(bufferedtrack);

Related

Getting java.io.IOException:mark/reset not supported after uploading a file to play, pause, resume, loop audio

This is the code I have as of now:
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.util.Scanner;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.sound.sampled.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class changeyouvoice extends Frame implements WindowListener, ActionListener {
JButton upload;
JFileChooser jfc;
int i;
Long currentFrame;
Clip clip;
String status;
public static void main(String[] args) {
// 2nd step
changeyouvoice myWindow = new changeyouvoice();
myWindow.setSize(350, 100);
myWindow.setVisible(true);
}
public changeyouvoice() {
jfc = new JFileChooser();
setLayout(new FlowLayout());
addWindowListener(this);
upload = new JButton("Upload");
add(upload);
upload.addActionListener(this);
// create AudioInputStream object
}
public void actionPerformed(ActionEvent e) {
jfc.setDialogTitle("Select the video of your voice");
jfc.setAcceptAllFileFilterUsed(false);// you are not searching for anything.
// figure out why
FileNameExtensionFilter filter = new FileNameExtensionFilter("Select Audio", "m4a", "mp3", "flac", "wav", "wma",
"aac");
jfc.setFileFilter(filter);
int returnValue = jfc.showOpenDialog(this);
// create AudioInputStream object
if (returnValue == JFileChooser.APPROVE_OPTION) {
try {
if (e.getSource() == upload) {
int x = jfc.showOpenDialog(this);
if (x == JFileChooser.APPROVE_OPTION) {
File fileToBeSent = jfc.getSelectedFile();
File initialFile = new File(fileToBeSent.getAbsolutePath());
try {
// InputStream targetStream;
InputStream targetStream = new FileInputStream(initialFile);
AudioInputStream myvoice = AudioSystem.getAudioInputStream(targetStream); // figure
// out
// how
// its
// created. read the file
// and get audio stream
// create AudioInputStream object
// create clip reference
clip = AudioSystem.getClip();
try {
clip.start();
clip.open(myvoice);
clip.loop(Clip.LOOP_CONTINUOUSLY);
status = "play";
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("1. pause");
System.out.println("2. resume");
System.out.println("3. restart");
System.out.println("4. stop");
System.out.println("5. Jump to specific time");
int c = sc.nextInt();
switch (c) {
case 1:
pause();
break;
case 2:
resumeAudio();
break;
case 3:
restart();
break;
case 4:
stop();
break;
case 5:
System.out.println("Enter time (" + 0 +
", " + clip.getMicrosecondLength() + ")");
long c1 = sc.nextLong();
jump(c1);
break;
}
if (c == 4)
break;
}
sc.close();
} catch (Exception ex) {
System.out.println("Error with playing sound.");
ex.printStackTrace();
}
// create clip reference
// open audioInputStream to the clip
} catch (Exception j) {
// Handle the error...
System.out.println(j.toString());
}
}
}
} catch (Exception png) {
// Handle the error...
System.out.println(png.toString());
} finally {
// ... cleanup that will execute whether or not an error occurred ...
}
}
}
// Method to pause the audio
public void pause() {
if (status.equals("paused")) {
System.out.println("audio is already paused");
return;
}
this.currentFrame = this.clip.getMicrosecondPosition();
clip.stop();
status = "paused";
}
// Method to resume the audio
public void resumeAudio() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
if (status.equals("play")) {
System.out.println("Audio is already " + "being played");
return;
}
clip.close();
resetAudioStream();
clip.setMicrosecondPosition(currentFrame);
clip.start();
status = "play";
}
// Method to restart the audio
public void restart() throws IOException, LineUnavailableException, UnsupportedAudioFileException {
clip.stop();
clip.close();
resetAudioStream();
currentFrame = 0L;
clip.setMicrosecondPosition(0);
clip.start();
status = "play";
}
// Method to stop the audio
public void stop() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
currentFrame = 0L;
clip.stop();
clip.close();
}
// Method to jump over a specific part
public void jump(long c) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
if (c > 0 && c < clip.getMicrosecondLength()) {
clip.stop();
clip.close();
resetAudioStream();
currentFrame = c;
clip.setMicrosecondPosition(c);
clip.start();
status = "play";
}
}
// Method to reset audio stream
public void resetAudioStream() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
AudioInputStream audioInputStream = AudioSystem
.getAudioInputStream(this.getClass().getResource(jfc.getSelectedFile().getAbsolutePath()));
clip.open(audioInputStream);
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowOpened(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
}
The code works fine when uploading a file, but for some reason I am not able to play, pause, or resume the audio. I used several tutorials to get the functions to help play the audio, but when I uploaded the file I had to upload it twice and an exception was displayed which was
/Users/thevladimirgeorge/Downloads/Vlad's story.mp3
java.io.IOException: mark/reset not supported
I wonder what was the issue going on with the code. The app I used to code is Visual Studio Code.
If you provide a URL instead of an InputStream as an argument to AudioSystem.getAudioStream, the requirement that the media resource supports marking and resetting is circumvented. This can be observed by comparing the APIs for the two methods.
AudioSystem.getAudioInputStream(InputStream)
AudioSystem.getAudioInputStream(URL)
Only the method that takes InputStream includes these checks:
The implementation of this method may require multiple parsers to
examine the stream to determine whether they support it. These parsers
must be able to mark the stream, read enough data to determine whether
they support the stream, and reset the stream's read pointer to its
original position. If the input stream does not support these
operations, this method may fail with an IOException.
So, rewriting the code to obtain a URL for your resource instead of an InputStream should solve your problem, unless you explicitly need to have the AudioInputStream support marking and resetting. I've not come across such a scenario myself. Would be interesting in learning about situations where this capability is used.
EDIT: I overlooked that you are trying to load an mp3. Java doesn't directly support this or many of the other files on your list of audio. You will need to include additional libraries and code to handle these. The following (executed in jshell) can show what formats are supported:
jshell> import javax.sound.sampled.*;
jshell> AudioFileFormat.Type[] types = AudioSystem.getAudioFileTypes();
types ==> AudioFileFormat$Type[3] { WAVE, AU, AIFF }
More documentation can be found in the JavaSound section of the TroubleShooting Guide.

Adjust the JLayer playback volume

How to set the volume in this simple example?
I saw different approaches before but I don't know how to adapt them for my simple snippet.
Related Questions:
Changing Volume with JLayer
Changing volume in Java when using JLayer
Code listing:
package view.sound;
import java.io.FileInputStream;
import javazoom.jl.player.Player;
public class Audio extends Thread {
String fileLocation = "src/EgyptianTavernFullofGuitarists_1.mp3";
boolean loop = true;
Player player;
#Override
public void run() {
try {
do {
FileInputStream buff = new FileInputStream(fileLocation);
player = new Player(buff);
player.play();
} while (loop);
} catch (Exception ioe) {
}
}
public void close(){
loop = false;
player.close();
this.interrupt();
}
}

Adding sound to game. Help needed

So I have this code, and I would like to know how could I join it with the rest of my game, 'cause the only way it plays sound now is by selecting this class as a launcher. Also, could I get some info, what exactly is changed and why. I feel like I should get a better understanding of this code, since I just found the code, and pasted it :D
Code:
package main;
import main.Handler;
import java.io.File;
import java.io.IOException;
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;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class AudioPlayer implements LineListener {
boolean playCompleted;
void play(String audioFilePath) {
File audioFile = new File(audioFilePath);
try {
AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
AudioFormat format = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip audioClip = (Clip) AudioSystem.getLine(info);
audioClip.addLineListener(this);
audioClip.open(audioStream);
audioClip.start();
while (!playCompleted) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
audioClip.close();
} catch (UnsupportedAudioFileException ex) {
System.out.println("The specified audio file is not supported.");
ex.printStackTrace();
} catch (LineUnavailableException ex) {
System.out.println("Audio line for playing back is unavailable.");
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("Error playing the audio file.");
ex.printStackTrace();
}
}
public void update(LineEvent event) {
LineEvent.Type type = event.getType();
if (type == LineEvent.Type.START) {
System.out.println("Playback started.");
} else if (type == LineEvent.Type.STOP) {
playCompleted = true;
System.out.println("Playback completed.");
}
}
public static void main(String[] args) {
String audioFilePath = "res/music/dark_theme.wav";
AudioPlayer player = new AudioPlayer();
player.play(audioFilePath);
}
}'
This class is a fairly normal class to use: Construct an instance, call some methods, and it does what it is expected to do. When you are using it as a launcher, all you're doing is calling the main method, which serves as an example of how to use this class:
public static void main(String[] args) {
String audioFilePath = "res/music/dark_theme.wav";
AudioPlayer player = new AudioPlayer();
player.play(audioFilePath);
}
Just construct an instance, and call play() on it with the name of the audio file intended.
However, be warned that this class isn't really a good example of how to go about doing this, for a few reasons:
It blocks while playing, meaning that you can't start playing sound and go about doing something different at the same time.
It can't play a sound more than once without incurring issues.
So, let's modify this class to not have these issues. This class will let you load a clip into memory, and start it asynchronously (meaning that you start it and then your program keeps running). The start() method starts it to play once, and the loop() method loops it forever. stop() is self-explanatory, and cleanup() should be called to release resources once you no longer need this audio clip. (Of course, if you intend to start playing the clip again soon, you should not cleanup() at that point).
import main.Handler;
import java.io.File;
import java.io.IOException;
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;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class AudioPlayer{
Clip audioClip;
boolean playCompleted;
String path;
public AudioPlayer(String path){
this.path = path;
File audioFile = new File(path);
try {
AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
AudioFormat format = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
audioClip = (Clip) AudioSystem.getLine(info);
audioClip.open(audioStream);
} catch (UnsupportedAudioFileException ex) {
System.out.println("The specified audio file is not supported.");
ex.printStackTrace();
} catch (LineUnavailableException ex) {
System.out.println("Audio line for playing back is unavailable.");
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("Error playing the audio file.");
ex.printStackTrace();
}
}
void play() {
audioClip.start();
}
void loop(){
audioClip.loop(Clip.LOOP_CONTINUOUSLY);
}
void stop(){
audioClip.stop();
}
void cleanup(){
audioClip.close();
}
public static void main(String[] args) throws InterruptedException {
String audioFilePath = "res/music/dark_theme.wav";
AudioPlayer player = new AudioPlayer(audioFilePath);
player.play();
// give the sound time to play
while(true){
Thread.sleep(1000);
}
}
}

Audio not Working Correctly

The last time I asked how to use background audio correctly, I was told I had to use a thread. Now, I have done that, yet I still have the same problem: It does not run the rest of the program until the song completes, yet I need it to play in the background! help anyone? (Sorry for no comments in the code. If you all really feel there needs to be some to help you out I will update it with comments).
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import java.io.*;
import javax.sound.sampled.*;
public class PokemonDemo3 implements LineListener
{
private boolean done = false;
public void update(LineEvent event)
{
if(event.getType() == LineEvent.Type.STOP || event.getType() == LineEvent.Type.CLOSE)
{
done = true;
}
}
public void waitonfinish() throws InterruptedException
{
while(!done)
{
Thread.sleep(1000);
}
}
public static void playSound(final String url)
{
try
{
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(url));
PokemonDemo3 control = new PokemonDemo3();
clip.addLineListener(control);
clip.open(inputStream);
clip.start();
control.waitonfinish();
}
catch (Exception e)
{
System.err.println(e.getMessage());
}
}
public static void main(String [] args)
{
String holder;
int response;
String[] options = {"Boy", "Girl"};
Pokemon intro = new Pokemon();
playSound("/Users/2018658/Desktop/pokemon.wav");
}

Clear data from Clip/AudioStream to free up memory

I have some code that serves as a super basic music player:
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
public class MusicPlayer{
private Clip c;
private boolean playing;
public MusicPlayer(){
playing = false;
}
public void play(String fileName) {
if(playing == false){
try {
c.drain();
c.flush();
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(fileName));
c = AudioSystem.getClip();
c.open(audioInputStream);
c.start();
playing = true;
} catch(Exception ex) {
System.out.println("Error with playing sound:" + ex);
ex.printStackTrace();
}
}
}
public void stop(){
if(playing == true){
c.stop();
playing = false;
}
}
}
The problem is that if I try to open too many wav files, the program crashes with an OutOfMemoryError. Giving the program more memory to work with (less than ideal to begin with) still means that the program runs slower each time a new file is opened. You can see I tried to solve this with:
c.drain();
c.flush();
but plainly this is not enough. Any help is appreciated!

Categories