当前位置: 首页 > 面试题库 >

Java播放AAC编码的音频(JAAD解码器)

司空俊雄
2023-03-14
问题内容

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

我们在第一学期末有一个小组项目,希望有一个背景音乐,并且那里几乎没有音效。最后,我们使用了WAV文件,因为我们无法播放AAC。


问题答案:

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

为了在以后的项目中更舒适地使用,我为我准备了一个小库,用于AAC播放。

由于很难找到可行的解决方案,因此我想与您分享,希望以后遇到同样问题的人可以更轻松一些。
以下代码是我编写的lib的代码段。您可以在AACPlayer的
GitHub上查看整个库 。
该库/解决方案使用aac解码器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();
                }
}


 类似资料:
  • 我正在尝试解码ADTS容器中的AAC音频流,该音频流来自外部硬件H264编码器。 我分析了ADT,它告诉我我有一个2通道,44100 AAC主配置文件框架。我为ffmpeg解码器设置了额外的数据字节,并成功解码了帧?详情如下: (伪c代码) 设置解码器: 设置额外的数据字节: 然后解码帧: 解码帧: 现在,据我所知,32位原始格式的每个帧每个采样将有4个字节,每个通道将被交错(因此每4个字节是交替

  • 我有以下功能,它获取一个WAV(PCM)文件并使用Android的MediaCode和MediaMuxer类将其编码为AAC编码的MP4文件。这只是音频。该函数成功运行并输出一个合理的. mp4,该函数被识别为AAC编码。但它不能在Android、Web或iOS播放器上播放,并使Audacy崩溃。我错过了什么吗?代码如下所示。

  • 我正在尝试使用使用编解码器对一些音频流进行编码。为此,我使用了google cts ExtractEncodeMust的这个实现。 对于某些aac文件,它会在编码某些帧后抛出。更准确地说,它会在第1030行抛出异常,。 我正在配置如下: 我完全不知道如何解决这个问题。任何形式的帮助都将不胜感激。 带有一些日志的堆栈跟踪: 设备:小米POCO x3 操作系统:Android10 导致溢出的示例文件信

  • 我正在尝试使用android AudioRecord和MediaCodec对aac音频进行编码。我创建了一个非常类似于(使用Android MediaCodec从相机编码H.264)的编码器类。使用此类,我创建了一个AudioRecord实例,并告诉它将其byte[]数据读出到AudioEncoder(audioEncoder.offerEncoder(Data))。 这是我的音频记录设置 我成功

  • 16 音频编码器 介绍当前可用的音频编码器 aac AAC(Advanced Audio Coding )编码器 当前原生(内置)编码器还处于实验阶段,而且只能支持AAC-LC(低复杂度AAC)。要使用这个编码器,必须选择 ‘experimental’或者’lower’ 因为当前还处于实验期,所以很多意外可能发生。如果需要一个更稳定的AAC编码器,参考libvo-aacenc,然而它也有一些负面报

  • 在我的网络应用程序中,用户应该能够上传自己的视频(任意一种格式)。我正在使用ffmpeg对视频进行编码。mp4和。flv使用命令: ffmpeg-iuservid.whateveroutput.mp4output.flv 当ffmpeg对视频进行编码时,它会加载到用户页面上的flowplayer中。但flowplayer总是说“找不到文件”,因为ffmpeg编码尚未完成。 是否有可能在某个播放器中