Advertisement
Guest User

Untitled

a guest
May 26th, 2015
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. public final class MyAudioController implements AudioController {
  2. private final Object mMutex = new Object();
  3. private final AudioRingBuffer mAudioBuffer = new AudioRingBuffer(81920);
  4. private volatile boolean mActive = true;
  5. private volatile boolean mSuspended = true;
  6. private AudioTrack mAudioTrack;
  7.  
  8. @Override
  9. public void start() {
  10. final Thread nThread = new Thread() {
  11. public void run() {
  12. setName("MyAudioController");
  13. pfxThread();
  14. }
  15. };
  16. nThread.start();
  17. }
  18.  
  19. @Override
  20. public void stop() {
  21. if (mSuspended) {
  22. notifyThreadResume();
  23. }
  24. mActive = false;
  25. }
  26.  
  27. @Override
  28. public int onAudioDataDelivered(short[] frames, int numberOfFrames, int rate, int channel) {
  29. if (mAudioTrack != null &&
  30. (rate != mAudioTrack.getSampleRate() || channel != mAudioTrack.getChannelCount())) {
  31. synchronized (mMutex) {
  32. mAudioTrack.release();
  33. mAudioTrack = null;
  34. }
  35. }
  36. if (mAudioTrack == null) {
  37. pfCreateAudioTrack(STREAM_MUSIC, rate, channel);
  38. }
  39. return mAudioBuffer.write(frames, (int) frameToBuffer);
  40.  
  41. }
  42.  
  43. @Override
  44. public void onAudioFlush() {
  45. if (mAudioTrack != null) {
  46. synchronized (mMutex) {
  47. mAudioTrack.pause();
  48. mAudioTrack.flush();
  49. mAudioTrack.release();
  50. mAudioTrack = null;
  51. }
  52. }
  53. mAudioBuffer.clear();
  54. notifyThreadResume();
  55. }
  56.  
  57. @Override
  58. public void onAudioPaused() {
  59. if (mAudioTrack != null) {
  60. mAudioTrack.pause();
  61. }
  62. notifyThreadPause();
  63. }
  64.  
  65. @Override
  66. public void onAudioResumed() {
  67. if (mAudioTrack != null) {
  68. mAudioTrack.play();
  69. }
  70. notifyThreadResume();
  71. }
  72.  
  73.  
  74. private synchronized void notifyThreadResume() {
  75. mSuspended = false;
  76. notifyAll();
  77. }
  78.  
  79. private synchronized void notifyThreadPause() {
  80. mSuspended = true;
  81. notifyAll();
  82. }
  83.  
  84. private void pfCreateAudioTrack(int type, int rate, int channel) {
  85. final int outChannel = (channel == 1
  86. ? AudioFormat.CHANNEL_OUT_MONO
  87. : AudioFormat.CHANNEL_OUT_STEREO);
  88. final int size = AudioFormat.ENCODING_PCM_16BIT;
  89. final int length =
  90. AudioTrack.getMinBufferSize(rate, outChannel, size) * 2;
  91.  
  92. synchronized (mMutex) {
  93. mAudioTrack = new AudioTrack(type, rate, outChannel, size, length, AudioTrack.MODE_STREAM);
  94. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  95. mAudioTrack.setVolume(AudioTrack.getMaxVolume());
  96. } else {
  97. mAudioTrack.setStereoVolume(AudioTrack.getMaxVolume(), AudioTrack.getMaxVolume());
  98. }
  99. mAudioTrack.play();
  100. }
  101. }
  102.  
  103. private int pfWriteToTrack(short[] frames, int numberOfFrames) {
  104. if (mAudioTrack != null && (mAudioTrack.getPlayState() == AudioTrack.PLAYSTATE_PLAYING)) {
  105. mAudioTrack.write(frames, 0, numberOfFrames)
  106. }
  107. return 0;
  108. }
  109.  
  110. private void pfxThread() {
  111. final short[] pendingFrames = new short[4096];
  112. while (mActive) {
  113. synchronized (this) {
  114. while (mSuspended) {
  115. try {
  116. wait();
  117. } catch (InterruptedException e) {
  118. mSuspended = mActive = false;
  119. }
  120. }
  121. }
  122. final int itemsRead = mAudioBuffer.peek(pendingFrames);
  123. if (itemsRead > 0 && !mDirty) {
  124. synchronized (mMutex) {
  125. pfWriteToTrack(pendingFrames, itemsRead);
  126. }
  127. mAudioBuffer.remove(itemsRead);
  128. }
  129. }
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement