我正在尝试在静态模式下使用Android AudioTrack。
我给建设者打电话了,一切顺利。
当我调用play()时,出现以下异常...
java.lang.IllegalStateException:未初始化时调用play() 音轨。
根据调试器,在调用播放之前,AudioTrack的状态为AudioTrack.STATE_NO_STATIC_DATA。正如我刚才所说的write方法那样,提供静态数据很奇怪。
我提供的缓冲区大小为8192,是2的幂。如果我将音频样本的大小作为缓冲区大小,则会抛出异常,表明它是无效的缓冲区大小。
我尝试在play()之前调用write(),这与我预期的没有任何区别。我什至删除了那个写调用,在play()调用上抛出的异常与write()调用无关。
我提供的音频样本根据sox具有以下格式。
Channels : 1
Sample Rate : 44100
Precision : 16-bit
Duration : 00:00:00.25 = 11153 samples = 18.9677 CDDA sectors
File Size : 22.4k
Bit Rate : 707k
Sample Encoding: 16-bit Signed Integer PCM
根据我能找到的所有示例,看来我做得正确。我尝试在模拟器和真实设备上运行此代码。
try {
WavFile wave = new WavFile(assets.open("sine.wav"));
if (wave.getChannel() != 1)
throw new RuntimeException("Incorrect Channels");
float[] data = wave.getData();
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, wave.getSmpRate(),
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT,
8192,
AudioTrack.MODE_STATIC);
audioTrack.write(data , 0 , data.length , AudioTrack.WRITE_BLOCKING);
audioTrack.play();
}
catch (IOException e) {
Log.e(e.toString());
}
在播放功能之前,这是调试器中AudioTrack的状态。...
mAppOps = {IAppOpsService$Stub$Proxy@4205}
mAttributes = {AudioAttributes@4206} "AudioAttributes: usage=1 content=2 flags=0x0 tags="
mAudioFormat = 2
mAvSyncBytesRemaining = 0
mAvSyncHeader = null
mChannelConfiguration = 12
mChannelCount = 2
mChannelIndexMask = 0
mChannelMask = 12
mDataLoadMode = 0
mEventHandlerDelegate = null
mInitializationLooper = {Looper@4207} "Looper (main, tid 1) {db61b45}"
mJniData = -1292582400
mNativeBufferSizeInBytes = 8192
mNativeBufferSizeInFrames = 2048
mNativeTrackInJavaObj = -1274526976
mPlayState = 1
mPlayStateLock = {Object@4208}
mPreferredDevice = null
mRoutingChangeListeners = {ArrayMap@4209} ArrayMap@4209, size = 0
mSampleRate = 48000
mSessionId = 21
mState = 2
mStreamType = 3
shadow$_klass_ = {Class@2915} "class android.media.AudioTrack"
shadow$_monitor_ = -2144204448
mState变量对应于AudioTrack.STATE_NO_STATIC_DATA。就像上一行我刚刚写静态数据一样,这很奇怪!