栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

为了在以后的项目中更舒适地使用,我为我准备了一个小库,用于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     Deprer         dec; // deprer to get the audio bytes     frameframe;          //     SampleBuffer    buf; //     int  currentTrack;   // index of current track from playlist     MP4Container    cont;// container to open the current track with     Moviemovie;          // 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 Deprer(track.getDeprerSpecificInfo());  buf = new SampleBuffer();  while(track.hasMoreframes())     // while we have frames left  {      frame = track.readNextframe();          // read next frame,      dec.depreframe(frame.getData(), buf);  // depre 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();     }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/455004.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号