File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Unplayable file using java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Unplayable file using java" Watch "Unplayable file using java" New topic

Unplayable file using java

Armando Festerelli Cambi

Joined: Mar 30, 2010
Posts: 2
can anyone help me ?
I wanto to play an audio file and whenever I press the stop button , the already played part of the file should be saved.
Unfortunately , what I get is an audio file (.wav) which actually is unplayable.


void play_cut() {

try {

// First, we get the format of the input file
final AudioFileFormat.Type fileType = AudioSystem.getAudioFileFormat(inputAudio).getType();
// Then, we get a clip for playing the audio.
c = AudioSystem.getClip();
// We get a stream for playing the input file.
AudioInputStream ais = AudioSystem.getAudioInputStream(inputAudio);
// We use the clip to open (but not start) the input stream;
// We get the format of the audio codec (not the file format we got above)
final AudioFormat audioFormat = ais.getFormat();

AudioInputStream startStream = new AudioInputStream(new FileInputStream(inputAudio), audioFormat, c.getLongFramePosition());
AudioSystem.write(startStream, fileType, outputAudio);

} catch (UnsupportedAudioFileException e) {
} catch (IOException e) {
} catch (LineUnavailableException e) {
}// end play_cut
Jonas Isberg
Ranch Hand

Joined: Mar 18, 2003
Posts: 118
Hi and welcome Tom!
tom qwehnch wrote:can anyone help me ?

I am pretty sure that someone can. You can however help them help
you by considering to UseCodeTags and PostRealCode.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11778

Please check your private messages regarding an important administrative matter.

Addressing Jonas' concerns, making this real code and using code tags:
(minor changes made for readability)

Hint: Take a look at the debug message I added at line 28 - does that look correct to you? Note that this matches up neatly with the debug message at line 32 - the expected amount to write + audio format header = output size.

Also - line 23 does not block. By the time you get to line 25 you do not know what state your playableClip is in - has it just started, has it completed, has the end user chosen how large they would like the resultant clip?

For that matter: is there even a need for line 23? I assume that this is where you are going to have your hook to allow the user to choose how much of the input they want to output, but for now it has no value.

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Armando Festerelli Cambi

Joined: Mar 30, 2010
Posts: 2
Thank you.
So how should I address the problem . A knotty problem for me .
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63866

"tom moose", please check your private messages for an important administrative matter. Again.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Unplayable file using java
It's not a secret anymore!