Android Audio代码分析9 - AudioTrack::write函数
在最初的代码testWriteByte中,创建完AudioTrack对象后,调用了AudioTrack对象的write函数实现播放。
今天就来看看write函数的实现。
*****************************************源码*************************************************
public int write(byte[] audioData,int offsetInBytes, int sizeInBytes) {
if ((mDataLoadMode == MODE_STATIC)
&& (mState == STATE_NO_STATIC_DATA)
&& (sizeInBytes > 0)) {
mState = STATE_INITIALIZED;
}
if (mState != STATE_INITIALIZED) {
return ERROR_INVALID_OPERATION;
}
if ( (audioData == null) || (offsetInBytes < 0 ) || (sizeInBytes < 0)
|| (offsetInBytes + sizeInBytes > audioData.length)) {
return ERROR_BAD_VALUE;
}
return native_write_byte(audioData, offsetInBytes, sizeInBytes, mAudioFormat);
}
**********************************************************************************************
源码路径:
frameworks\base\media\java\android\media\AudioTrack.java
#################说明################################################
/**
* Writes the audio data to the audio hardware for playback.
* @param audioData the array that holds the data to play.
* @param offsetInBytes the offset expressed in bytes in audioData where the data to play
* starts.
* @param sizeInBytes the number of bytes to read in audioData after the offset.
* @return the number of bytes that were written or {@link #ERROR_INVALID_OPERATION}
* if the object wasn't properly initialized, or {@link #ERROR_BAD_VALUE} if
* the parameters don't resolve to valid data and indexes.
*/
// 先看看注释,有一点需要注意,offsetInBytes是指要播放的数据是从参数audioData的哪个地方开始
public int write(byte[] audioData,int offsetInBytes, int sizeInBytes) {
if ((mDataLoadMode == MODE_STATIC)
&& (mState == STATE_NO_STATIC_DATA)
&& (sizeInBytes > 0)) {
mState = STATE_INITIALIZED;
}
if (mState != STATE_INITIALIZED) {
return ERROR_INVALID_OPERATION;
}
if ( (audioData == null) || (offsetInBytes < 0 ) || (sizeInBytes < 0)
|| (offsetInBytes + sizeInBytes > audioData.length)) {
return ERROR_BAD_VALUE;
}
// 前面主要检查了状态及参数,真正干活的在native中。
return native_write_byte(audioData, offsetInBytes, sizeInBytes, mAudioFormat);
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
路径:frameworks\base\core\jni\android_media_AudioTrack.cpp
对应的native侧的函数为android_media_AudioTrack_native_write,其实现如下:
static jint android_media_AudioTrack_native_write(JNIEnv *env, jobject thiz,
jbyteArray javaAudioData,
jint offsetInBytes, jint sizeInBytes,
jint javaAudioFormat) {
jbyte* cAudioData = NULL;
AudioTrack *lpTrack = NULL;
//LOGV("android_media_AudioTrack_native_write(offset=%d, sizeInBytes=%d) called",
// offsetInBytes, sizeInBytes);
// get the audio track to load with samples
// 我们创建AudioTrack对象的时间将其保存到了java侧,
// 现在要使用它了,所以把它取出来
lpTrack = (AudioTrack *)env->GetIntField(thiz, javaAudioTrackFields.nativeTrackInJavaObj);
if (lpTrack == NULL) {
jniThrowException(env, "java/lang/IllegalStateException",
"Unable to retrieve AudioTrack pointer for write()");
return 0;
}
// get the pointer for the audio data from the java array
if (javaAudioData) {
cAudioData = (jbyte *)env->GetPrimitiveArrayCritical(javaAudioData, NULL);
if (cAudioData == NULL) {
LOGE("Error retrieving source of audio data to play, can't play");
return 0; // out of memory or no data to load
}
} else {
LOGE("NULL java array of audio data to play, can't play");
return 0;
}
jint written = writeToTrack(lpTrack, javaAudioFormat, cAudioData, offsetInBytes, sizeInBytes);
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
jint writeToTrack(AudioTrack* pTrack, jint audioFormat, jb
补充:移动开发 , Android ,