Java播放AAC编码音频(JAAD解码器) [英] Java play AAC encoded audio ( JAAD decoder )

查看:934
本文介绍了Java播放AAC编码音频(JAAD解码器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力用Java播放aac编码的音频文件。

I have struggled with playing aac encoded audio files with Java a while now.

我们在第一学期结束时有一个团队项目,想要有一个那里有背景音乐和很​​少的声音效果。
最后我们使用了WAV文件,因为我们无法播放AAC。

We had a group project at the end of our first semester and wanted to have a background music and few soundeffects in there. At the end we used WAV files, as we couldn't get the AAC's to play.

推荐答案

本周末我又试了一次,再次搜索,并且从不同的站点一起搜索了一个工作代码,但没有一个完整的工作解决方案。

This weekend I gave it another try and searched along again, and have got a working code searched together from different sites, but nowhere was a complete working solution.

为了更舒适的使用在未来的项目中,我为我做了一个小型的aac播放库。

For a more comfortable usage in future projects I made me a small library for aac playback.

由于很难找到合适的解决方案,我想与你分享,希望有些一个在将来遇到同样问题的人将会更容易。

以下代码是我写的lib的片段。您可以在我的GitHub上查看整个lib,价格为
AACPlayer 。$
这个lib /解决方案使用aac解码器 JAAD

As it was hard to find th working solution, I wanted to share it with you, in the hope some one having the same problem in the future will have it a bit easier.
The following code is a snippet from the lib I wrote. You can have a look onto the whole lib on my GitHub at AACPlayer.
This lib/solution uses the aac decoder JAAD.

public static void play(File[] files)
{
                // local vars
                byte[]          b;              // array for the actual audio Data during the playback
                AudioTrack      track;          // track we are playing atm
                AudioFormat     af;             // the track's format
                SourceDataLine  line;           // the line we'll use the get our audio to the speaker's
                Decoder         dec;            // decoder to get the audio bytes
                Frame           frame;          //
                SampleBuffer    buf;            //
                int             currentTrack;   // index of current track from playlist
                MP4Container    cont;           // container to open the current track with
                Movie           movie;          // and get the content from the container

                try
                {
                    // for-next loop to play each titel from the playlist once
                    for (currentTrack = 0; currentTrack < files.length; currentTrack++)
                    {
                        cont    = new MP4Container(new RandomAccessFile(files[currentTrack], "r")); // open titel with random access
                        movie   = cont.getMovie();                          // get content from container,
                        List<Track> content = movie.getTracks();
                        if (content.isEmpty())                              // check if container HAS content
                            throw new Exception ("insert error message here");  // if so,
                        track   = (AudioTrack) movie.getTracks().get(0);    // grab first track and set the audioformat
                        af      = new AudioFormat(track.getSampleRate(), track.getSampleSize(), track.getChannelCount(), true, true);
                        line    = AudioSystem.getSourceDataLine(af);        // get a DataLine from the AudioSystem
                        line.open();                                        // open and
                        line.start();                                       // start it

                        dec     = new Decoder(track.getDecoderSpecificInfo());

                        buf = new SampleBuffer();
                        while(track.hasMoreFrames())                // while we have frames left
                        {
                            frame = track.readNextFrame();          // read next frame,
                            dec.decodeFrame(frame.getData(), buf);  // decode it and put into the buffer
                            b = buf.getData();                      // write the frame data from the buffer to our byte-array
                            line.write(b, 0, b.length);             // and from there write the byte array into our open AudioSystem DataLine

                            while (paused)                  // check if we should pause
                            {
                                Thread.sleep(500);          // if yes, stay half a second

                                if (Thread.interrupted())   // check if we should stop possibly
                                {
                                    line.close();           // if yes, close line and
                                    return;                 // exit thread
                                }
                            }

                            if (Thread.interrupted())       // if not in pause, still check on each frame if we should
                            {                               // stop. If so
                                line.close();               // close line and
                                return;                     // exit thread
                            }
                        }

                        line.close();           // after titel is over, close line

                        if (loop)               // if we should loop current titel, set currentTrack -1,
                            currentTrack--;     // as on bottom of for-next it get's +1 and so the same titel get's played again
                        else if (repeat && (currentTrack == files.length -1)) // else check if we are at the end of the playlist
                            currentTrack = -1;  // and should repeat the whole list. If so, set currentTrack -1, so it get's 0 on for-next bottom
                    }
                }
                catch (LineUnavailableException | IOException | InterruptedException e)
                {
                    e.printStackTrace();
                }
}

这篇关于Java播放AAC编码音频(JAAD解码器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆