与Webstream的JAAD一起玩AAC

时间:2018-05-25 20:39:15

标签: java aac

我需要使用Java应用程序播放AAC编码音频。 我阅读了Java play AAC encoded audio ( JAAD decoder ),其中展示了如何播放文件数组。 但是当我的源是网络流时,我如何玩AAC?

2 个答案:

答案 0 :(得分:0)

从您提供的示例中,它使用MP4Container

来自JAAD' scode:

public MP4Container(InputStream in) throws IOException {
    this.in = new MP4InputStream(in);
    boxes = new ArrayList<Box>();

    readContent();
}

public MP4Container(RandomAccessFile in) throws IOException {
    this.in = new MP4InputStream(in);
    boxes = new ArrayList<Box>();

    readContent();
}

示例使用MP4Container(RandomAccessFile in)构造函数,而您必须使用此MP4Container(InputStream in),其中in将从套接字输入流。

答案 1 :(得分:0)

以下是使用JAAD播放原始AAC的解决方案:

public class AACPlayer extends AbstractPlayer implements Runnable {
private Thread runnerThread;
@Override
public void stop() {
    stop = true;
    GUIHandler.getInstance().resetComponents();
}

@Override
public void play() {
    stop = false;
    if(!runnerThread.isAlive()) {
        runnerThread = new Thread(this);
        runnerThread.start();
    }
}

@Override
public void setUrl(URL url) {
    this.url = url;
}

@Override
public void run() {
    decodeAndPlayAAC();
}

private void decodeAndPlayAAC() {
    SourceDataLine line = null;
    byte[] b;
    try {
        isPlaying = true;
        final ADTSDemultiplexer adts = new ADTSDemultiplexer(url.openStream());
        final Decoder dec = new Decoder(adts.getDecoderSpecificInfo());
        final SampleBuffer buf = new SampleBuffer();
        while(!stop) {
            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);
        }
    } catch (LineUnavailableException e) {
        e.printStackTrace();
    } catch (AACException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(line!=null) {
            line.stop();
            line.close();
            isPlaying = false;
        }
    }
}

}