Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JAAD ADTS Header Exception

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JAAD ADTS Header Exception

    First off let me say hello! I'm new to the forums and was happy to see there is a community built around what has slowly become one of my favorite programming languages! I'm sure I will be back here time and time again .

    Anyway I've been programming for probably close to 10 years now and have learned my fair share of languages (crappy and good alike). I didn't start with Java until about 2 years ago, as it was the main topic of my first two years of college Comp Sci classes. Anyway I've grown to love it, and recently started programming OpenGL with Java via the LWJGL (which I find quite nice). Anyway all of this is beside the point of the post, just a little get to know me rather... now back on track...

    I don't know a whole lot about audio encoding and decoding, but I know that Java doesn't have much native support for different encoded files. So this morning I turned to 3rd party libraries to help me play all my audio files (I'm pretty sure all of them are either mp3 or m4a). A little bit of research showed me that m4a is essentially the same thing as a mp4 file. Anyway I downloaded and used the jl1.0.jar file to write a class that will play mp3 files, no problems there and it was pretty easy. Then I found JAAD (Java Advanced Audio Decoder?) for decoding AAC files, again more research will tell me that the *.m4a files fall into the AAC category (if I'm not missing something here). Keep in mind that my *.m4a files are just Audio and no Video mp4 files. So looking through the example code for JAAD I notice it has two methods of playing AAC files, the first (decodeMP4) seems to me to play a video/audio mp4 file, and the second (decodeAAC) seems to be just for audio (below is the code in question).

    private static void decodeMP4(String in) throws Exception {
    		SourceDataLine line = null;
    		byte[] b;
    		try {
    			//create container
    			final MP4Container cont = new MP4Container(new RandomAccessFile(in, "r"));
    			final Movie movie = cont.getMovie();
    			//find AAC track
    			final List<Track> tracks = movie.getTracks(AudioTrack.AudioCodec.AAC);
    			if(tracks.isEmpty()) throw new Exception("movie does not contain any AAC track");
    			final AudioTrack track = (AudioTrack) tracks.get(0);
     
    			//create audio format
    			final AudioFormat aufmt = new AudioFormat(track.getSampleRate(), track.getSampleSize(), track.getChannelCount(), true, true);
    			line = AudioSystem.getSourceDataLine(aufmt);
    			line.open();
    			line.start();
     
    			//create AAC decoder
    			final Decoder dec = new Decoder(track.getDecoderSpecificInfo());
     
    			//decode
    			Frame frame;
    			final SampleBuffer buf = new SampleBuffer();
    			while(track.hasMoreFrames()) {
    				frame = track.readNextFrame();
    				try {
    					dec.decodeFrame(frame.getData(), buf);
    					b = buf.getData();
    					line.write(b, 0, b.length);
    				}
    				catch(AACException e) {
    					e.printStackTrace();
    					//since the frames are separate, decoding can continue if one fails
    				}
    			}
    		}
    		finally {
    			if(line!=null) {
    				line.stop();
    				line.close();
    			}
    		}
    	}
     
    	private static void decodeAAC(String in) throws Exception {
    		SourceDataLine line = null;
    		byte[] b;
    		try {
    			final ADTSDemultiplexer adts = new ADTSDemultiplexer(new FileInputStream(in));
    			final Decoder dec = new Decoder(adts.getDecoderSpecificInfo());
    			final SampleBuffer buf = new SampleBuffer();
    			while(true) {
    				b = adts.readNextFrame();
    				dec.decodeFrame(b, buf);
     
    				if(line==null) {
    					final AudioFormat aufmt = new AudioFormat(buf.getSampleRate(), buf.getBitsPerSample(), buf.getChannels(), true, true);
    					line = AudioSystem.getSourceDataLine(aufmt);
    					line.open();
    					line.start();
    				}
    				b = buf.getData();
    				line.write(b, 0, b.length);
    			}
    		}
    		finally {
    			if(line!=null) {
    				line.stop();
    				line.close();
    			}
    		}
    	}


    So what I did was essentially take the code from decodeAAC and modify it ever so slightly and plug it into my own class. Then I gave it a whirl with a few different audio .m4a files, but they all return the same IOException: "java.io.IOException: no ADTS header found". Again I have very little knowledge of music encoding / decoding so I googled a bit and am still confused as to why it can't find the header info. I assume this means it's not there... but why would it not be there? It is after all an AAC encoded file? Just to make sure the file wasn't improperly labeled I took a look at the properties and sure enough the section labeled "Codec: " reads "MPEG-4 AAC audio". So now I'm stumped so I hope you guys can help out a bit and at least clear up some of the uncertainties. Below is my code, it's pretty simple, right now it's just supposed to play a file which is in "./Music/". Thanks for any help.

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
     
    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.SourceDataLine;
     
    import net.sourceforge.jaad.aac.Decoder;
    import net.sourceforge.jaad.aac.SampleBuffer;
    import net.sourceforge.jaad.adts.ADTSDemultiplexer;
     
    /**
     * @author howard
     * 
     */
    public class Mp4 {
     
    	private static String fileName = "Music/Krytonite.m4a";
     
    	public Mp4(String file) {
     
    		// If we have specified a file, set the fileName variable
    		if (!file.equals("")) {
    			fileName = file;
    		}
     
    		SourceDataLine line = null;
    		byte[] b;
     
    		try {
    			ADTSDemultiplexer adts = new ADTSDemultiplexer(new FileInputStream(
    					fileName));
    			Decoder dec = new Decoder(adts.getDecoderSpecificInfo());
    			SampleBuffer buf = new SampleBuffer();
     
    			while ((b = adts.readNextFrame()) != null) {
    				dec.decodeFrame(b, buf);
     
    				if (line == null) {
    					AudioFormat aufmt = new AudioFormat(buf.getSampleRate(),
    							buf.getBitsPerSample(), buf.getChannels(), true,
    							true);
    					line = AudioSystem.getSourceDataLine(aufmt);
    					line.open();
    					line.start();
    				}
     
    				b = buf.getData();
    				line.write(b, 0, b.length);
    			}
     
    		} catch (FileNotFoundException e) {
    			System.out.println("Failed to load file: " + fileName);
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} catch (LineUnavailableException e) {
    			System.out.println("Failed to grab audio line!");
    			e.printStackTrace();
    		} finally {
    			if (line != null) {
    				line.stop();
    				line.close();
    			}
    		}
    	}
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		new Mp4(fileName);
    	}
     
    }


  2. #2
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAAD ADTS Header Exception

    Is this perhaps the wrong place to put this thread? I could really use any thoughts that you guys may have on this one, like I said I'm completely stumped but would like to move forward with this project before classes start again and I won't be able to finish it.

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: JAAD ADTS Header Exception

    Hey Howie.
    Your lack of responses aren't due to it being in the wrong section, it's just the field your problem is in.
    When you're using third party API's and having audio related problems, only a certain percentage of this forum would know enough that could benefit you.

    Myself, I've never dealt with audio in Java myself so I can't really help you either.
    A shot in the dark for me would be to ask you if there are any methods which relate to adding in an ADTS header manually? As what I've quickly read on google about it, is that you need to place headers into the files
    in order to achieve playback.

    Good luck.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  4. The Following User Says Thank You to newbie For This Useful Post:

    JavaPF (August 16th, 2011)

  5. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JAAD ADTS Header Exception

    Well thanks for the reply. I read through the JavaDoc and didn't find anything similar to adding ADTS data to a file. But it did give me an opportunity to load the file other ways and try to different methods of playing the file, but again to no avail for one reason or another. I do appreciate the answer though.

Similar Threads

  1. JavaMail - How do I get a specific Header of an Email?
    By gsanchezbiz in forum Java SE APIs
    Replies: 4
    Last Post: December 13th, 2011, 02:27 PM
  2. JPCap TCP header encryption
    By proskopos in forum Java SE APIs
    Replies: 0
    Last Post: May 21st, 2011, 05:02 AM
  3. Urgent: invalid stream header: 5B47656E
    By DanBrown in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 28th, 2011, 07:30 AM
  4. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM
  5. How to bind soap header using jax-rpc
    By jadeite100 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 5th, 2010, 02:54 PM