AudioTrack停止在Android 5.0上运行

时间:2015-11-03 23:06:05

标签: android android-5.0-lollipop audiotrack android-5.1.1-lollipop

在掌握了Lollipop的Android设备之后,我注意到我们为Kitkat(以及之前的版本)工作的AudioTrack代码只是停止工作,完全相同的代码适用于Lollipop之前的设备,所以我想知道是什么我对新版本做错了。 以下是AudioTrack(由mTrack属性引用)初始化的方式:

protected void onCreate(Bundle savedInstanceState) {
    int bufferSize = AudioTrack.getMinBufferSize(AUDIO_SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT);

    mTrack = new AudioTrack(AudioManager.STREAM_MUSIC, AUDIO_SAMPLE_RATE,
                    AudioFormat.CHANNEL_CONFIGURATION_STEREO,
                    AudioFormat.ENCODING_PCM_16BIT, bufferSize,
                    AudioTrack.MODE_STREAM);
}

以下是填充缓冲区的方法:

public void onPlayAudio(byte[] audioBuffer) {
    if (mTrack.getPlayState() != AudioTrack.PLAYSTATE_PLAYING) {
        mTrack.play();
    }

    if (audioBuffer != null) {
        mTrack.write(audioBuffer, 0, audioBuffer.length);
    }
}

这再次适用于Lollipop之前的设备。 Logcat上没有错误,一切似乎都很正常,但声音没有出来(是的,我检查了音量,它一直都是)。所以我想知道是否有更新我缺少或者说我做错了。

我注意到AudioFormat.CHANNEL_CONFIGURATION_STEREO已被弃用,但尝试使用建议的AudioFormat.CHANNEL_OUT_STEREO似乎无法解决问题。

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

Google Changing API。 试试这个:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/lightslider/1.1.3/css/lightslider.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lightslider/1.1.3/js/lightslider.min.js"></script>

<div class="demo tooltip">
  <span class="tooltiptext">Tooltip text</span>
  <ul id="lightSlider">
    <li data-thumb="http://sachinchoolur.github.io/lightslider/img/thumb/cS-1.jpg">
      <img src="http://sachinchoolur.github.io/lightslider/img/cS-1.jpg" alt="">
    </li>
    <li data-thumb="http://sachinchoolur.github.io/lightslider/img/thumb/cS-2.jpg">
      <img src="http://sachinchoolur.github.io/lightslider/img/cS-2.jpg" alt="">
    </li>
    <li data-thumb="http://sachinchoolur.github.io/lightslider/img/thumb/cS-3.jpg">
      <img src="http://sachinchoolur.github.io/lightslider/img/cS-3.jpg" alt="">
    </li>
    <li data-thumb="http://sachinchoolur.github.io/lightslider/img/thumb/cS-4.jpg">
      <img src="http://sachinchoolur.github.io/lightslider/img/cS-4.jpg" alt="">
    </li>
    <li data-thumb="http://sachinchoolur.github.io/lightslider/img/thumb/cS-5.jpg">
      <img src="http://sachinchoolur.github.io/lightslider/img/cS-5.jpg" alt="">
    </li>
    <li data-thumb="http://sachinchoolur.github.io/lightslider/img/thumb/cS-6.jpg">
      <img src="http://sachinchoolur.github.io/lightslider/img/cS-6.jpg" alt="">
    </li>
    <li data-thumb="http://sachinchoolur.github.io/lightslider/img/thumb/cS-7.jpg">
      <img src="http://sachinchoolur.github.io/lightslider/img/cS-7.jpg" alt="">
    </li>
    <li data-thumb="http://sachinchoolur.github.io/lightslider/img/thumb/cS-8.jpg">
      <img src="http://sachinchoolur.github.io/lightslider/img/cS-8.jpg" alt="">
    </li>
    <li data-thumb="http://sachinchoolur.github.io/lightslider/img/thumb/cS-9.jpg">
      <img src="http://sachinchoolur.github.io/lightslider/img/cS-9.jpg" alt="">
    </li>
    <li data-thumb="http://sachinchoolur.github.io/lightslider/img/thumb/cS-10.jpg">
      <img src="http://sachinchoolur.github.io/lightslider/img/cS-10.jpg" alt="">
    </li>
    <li data-thumb="http://sachinchoolur.github.io/lightslider/img/thumb/cS-11.jpg">
      <img src="http://sachinchoolur.github.io/lightslider/img/cS-12.jpg" alt="">
    </li>
    <li data-thumb="http://sachinchoolur.github.io/lightslider/img/thumb/cS-13.jpg">
      <img src="http://sachinchoolur.github.io/lightslider/img/cS-13.jpg" alt="">
    </li>
  </ul>
</div>