I'm reading codec file and converting to mp3/wav file using Java and Java Sound but getting the following Error.
Error
javax.sound.sampled.UnsupportedAudioFileException: file is not a supported file type
at javax.sound.sampled.AudioSystem.getAudioFileFormat(AudioSystem.java:1076)
at test.postingstackOverflow.main(postingstackOverflow.java:29)
Code
package test;
import java.io.File;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
/**
*
* #author shankar
*/
public class postingstackOverflow {
public static void main(String args[]){
AudioFileFormat inputFileFormat=null;
javax.sound.sampled.AudioFormat audioFormat =null;
AudioInputStream encodedASI=null;
AudioInputStream ais=null;
try{
inputFileFormat = AudioSystem.getAudioFileFormat(new File("/media/shankar/voip/Temp/JavaSound/1000.g711u"));
ais = AudioSystem.getAudioInputStream(new File("/media/shankar/voip/Temp/JavaSound/1000.g711u"));
audioFormat = ais.getFormat();
encodedASI = AudioSystem.getAudioInputStream(javax.sound.sampled.AudioFormat.Encoding.ULAW, ais);
int i = AudioSystem.write(encodedASI, AudioFileFormat.Type.WAVE, new File("/media/shankar/voip/Temp/JavaSound/converted.mp3"));
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(encodedASI!=null)
encodedASI.close();
if(ais!=null)
ais.close();
if(encodedASI!=null)
encodedASI.close();
}catch(Exception expClose){
expClose.printStackTrace();
}
}//end finally
}
}
Can anyone tell me how solve reading a μ-law file?
Related
I have been experimenting with Java Swing using a GUI and have hit a wall. I am trying to play a sound using Java Sound. Ultimately, I want to push a button and the sound plays. I have tried a lot of combinations but none seem to work. Here is the latest code I tried and I code and it reports:
Error: could not find or load main class.
I am not seeing why:
package net.codejava.sound;
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.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
/**
* This is an example program that demonstrates how to play back an audio file
* using the SourceDataLine in Java Sound API.
* #author www.codejava.net
*
*/
public class AudioPlayerExample2 {
// size of the byte buffer used to read/write the audio stream
private static final int BUFFER_SIZE = 4096;
/**
* Play a given audio file.
* #param audioFilePath Path of the audio file.
*/
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(SourceDataLine.class, format);
SourceDataLine audioLine = (SourceDataLine) AudioSystem.getLine(info);
audioLine.open(format);
audioLine.start();
System.out.println("Playback started.");
byte[] bytesBuffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
while ((bytesRead = audioStream.read(bytesBuffer)) != -1) {
audioLine.write(bytesBuffer, 0, bytesRead);
}
audioLine.drain();
audioLine.close();
audioStream.close();
System.out.println("Playback completed.");
} 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 static void main(String[] args) {
String audioFilePath = "https://codehs.com/uploads/1981fc4b1d2e4123e9cbe7ab8cc1962a";
AudioPlayerExample2 player = new AudioPlayerExample2();
player.play(audioFilePath);
}
}
I made a couple small changes to the tutorial code example you posted, and the program worked perfectly well.
Here are my changes:
(1) Replaced "File audioFile = new File(audioFilePath);" with the following:
URL audioFile = null;
try {
audioFile = new URL(audioFilePath);
} catch (MalformedURLException e) {
e.printStackTrace();
}
(2) Added the following line to the module-info file (required if you are using Java 9 or higher):
requires java.desktop;
My package setting is slightly different, but I assume you know how to properly set up packages. Your class is in the file folder specified by the package statement, yes?
The error being cited: "could not find or load main class" indicates that something is going wrong with how the code is being invoked rather than a problem with the audio part of the code. What version of Java are you using? What IDE? What is the command you are issuing to execute the program? FWIW, my setup that successfully executed this code has an up-to-date Eclipse IDE running Java 11.
Nam Ha Minh's tutorials at codejava.net usually are quite good. I think he is one of the more reliable tutorial writers out there.
".getAudioInputStream(file)" give the error "cannot resolve symbol" (IDE Intellij, java 8)
I try the solution in File > Invalidate Chaces / Restart ... but it doesn't work
package com.Main;
import javax.sound.sampled.Clip;
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
public class SoundEffect {
private String filepath;
private Clip clip;
public SoundEffect(String filepath) {
this.filepath = filepath;
try {
File file = new File(filepath);
AudioInputStream sound = new AudioSystem.getAudioInputStream(file);
clip = AudioSystem.getClip();
clip.open(sound);
}
catch (Exception e) { e.printStackTrace(); }
}
public void play() {
clip.start();
}
}
AudioInputStream sound = new AudioSystem.getAudioInputStream(file);
You don't want to create an instance of an object. You want to invoke a static method of a class.
You don't need the "new".
The code should be:
//AudioInputStream sound = new AudioSystem.getAudioInputStream(file);
AudioInputStream sound = AudioSystem.getAudioInputStream(file);
I got a bunch of audio files from a client which at some point in the program need to play. Only a few of these .wav files play when I call them, but most don't and give me an IllegalArgumentException. I tried many different code examples of how to play .wav files in Java but none worked for me and I don't know why. Currently I'm using this, which came from another stackOverflow question/answer:
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
/**
* Handles playing, stoping, and looping of sounds for the game.
* #author Tyler Thomas
*
*/
public class Sound {
private final int BUFFER_SIZE = 128000;
private AudioInputStream audioStream;
private SourceDataLine sourceLine;
/**
* #param filename the name of the file that is going to be played
*/
public void playSound(String filename){
try {
audioStream = AudioSystem.getAudioInputStream(new File(filename));
} catch (Exception e){
e.printStackTrace();
}
try {
sourceLine = (SourceDataLine) AudioSystem.getLine(new DataLine.Info(SourceDataLine.class, audioStream.getFormat()));
sourceLine.open(audioStream.getFormat());
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
sourceLine.start();
int nBytesRead = 0;
byte[] abData = new byte[BUFFER_SIZE];
while (nBytesRead != -1) {
try {
nBytesRead = audioStream.read(abData, 0, abData.length);
} catch (IOException e) {
e.printStackTrace();
}
if (nBytesRead >= 0) {
#SuppressWarnings("unused")
int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
}
}
sourceLine.drain();
sourceLine.close();
}
}
If I use this to play my file I get the following Error:
java.lang.IllegalArgumentException: No line matching interface SourceDataLine supporting format PCM_FLOAT 44100.0 Hz, 32 bit, stereo, 8 bytes/frame, is supported.
at javax.sound.sampled.AudioSystem.getLine(Unknown Source)
at Sound.playSound(Sound.java:29)
Extra, when I open the file with a regular media player it has no problems. So the files are not corrupted or anything.
The audio file that I am using is found here: http://www.orangefreesounds.com/loud-alarm-clock-sound/
This is what my file structure looks like in my Eclipse IDE:
The audio file plays perfectly fine when I run it in my IDE, but not when I export it as a JAR file. I have already checked and found that the audio file is inside the JAR file.
I am using the terminal command java -jar Sandbox.jar & to run the JAR file. The program seems to be able to find the file (since it is not throwing an IOException), but does not seem to be able to perform playback.
Why is this problem happening and how can I fix it?
Weird Update
Okay, so actually, the JAR file is able to play the audio file when run in cmd or PowerShell on Windows 8.1, but not in the terminal of Ubuntu 14.04 for some reason. This whole time, I have been trying to run the JAR file in Ubuntu 14.04.
Weird Update #2
I have confirmed the issue of the JAR files only working on a Windows 8.1 system. Both of the code snippets in this question DO NOT WORK, while both of MadProgrammer's solutions work.
Minimal, Complete, and Verifiable example (does NOT work on Windows or Ubuntu)
import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.*;
public class Sandbox
{
public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException
{
URL url = Sandbox.class.getResource("/sound-effects/alarmSoundClip.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
AudioFormat af = ais.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, af);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(ais);
clip.start();
}
}
Attempted Solution #1 (does NOT work on Windows or Ubuntu)
One attempted solution (as suggested by Andrew Thompson) was to write this.getClass().getResource( ... ) instead of Sandbox.class.getResource( ... ):
import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.*;
public class Sandbox
{
public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException
{
new Sandbox();
}
public Sandbox() throws UnsupportedAudioFileException, IOException, LineUnavailableException
{
URL url = this.getClass().getResource("/sound-effects/alarmSoundClip.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
AudioFormat af = ais.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, af);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(ais);
clip.start();
}
}
Adding clip.drain() after clip.start() seems to have worked okay for me (IDE and command line both with and without &)
import java.io.IOException;
import java.net.URL;
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.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class Sandbox {
public static void main(String[] args) {
try {
URL url = Sandbox.class.getResource("/sound-effects/Loud-alarm-clock-sound.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
AudioFormat af = ais.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, af);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(ais);
clip.start();
System.out.println("Drain...");
clip.drain();
System.out.println("...Drained");
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException exp) {
exp.printStackTrace();
}
}
}
Now, having said that, I have found drain a little unreliable in the past, especially when there are multiple sounds playing in which case I tend to use a LineListener
For example...
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
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 Sandbox {
protected static final Object LOCK = new Object();
public static void main(String[] args) {
try {
URL url = Sandbox.class.getResource("/sound-effects/Loud-alarm-clock-sound.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
AudioFormat af = ais.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, af);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(ais);
clip.addLineListener(new LineListener() {
#Override
public void update(LineEvent event) {
System.out.println(event.getType());
if (event.getType() == LineEvent.Type.STOP) {
synchronized (LOCK) {
LOCK.notify();
}
}
}
});
clip.start();
synchronized (LOCK) {
LOCK.wait();
}
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException | InterruptedException exp) {
exp.printStackTrace();
}
}
}
I am trying to change the output audio levels of the program, preferably in decibels. I need to change the audio levels of the entire program and record the change in the level. The language is Java. Is there any easy way to do this? The sounds I am using to play the sounds is below:
import java.io.InputStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class Sound
{
String sounds;
public Sound(String file)
{
sounds = file;
playSound(sounds);
}//end contructor
public void playSound(String soundLoc)
{
try
{
InputStream inputStream = getClass().getResourceAsStream(soundLoc);
AudioStream audioStream = new AudioStream(inputStream);
AudioPlayer.player.start(audioStream);
}//end try
catch (Exception e)
{
}//end catch
}//end playSound method
}//end class Sound
You can use MASTER_GAIN_CONTROL using the Java sound API
you need to import this.... import javax.sound.sampled.*;
get your clip using a Clip object and then,
public void playSound(String soundLoc)
{
try
{
InputStream inputStream = getClass().getResourceAsStream(soundLoc);
AudioStream audioStream = new AudioStream(inputStream);
AudioPlayer.player.start(audioStream);
Clip myclip = AudioSystem.getClip();
myclip.open(audioStream);
FloatControl audioControl = (FloatControl) myclip.getControl(FloatControl.Type.MASTER_GAIN);
audioControl.setValue(-5.0f); //decrease volume 5 decibels
clip.start();
}//end try
catch (Exception e)
{
}//end catch
}//end playSound method