PCM 数据播放在开发中也经常使用,例如自己编写播放器,解码之后的音频PCM数据,就可以通过OpenSL 播放,比用Java层的AudioTrack更快,延迟更低。
下面我们编写OpenSL PCM播放,播放的主要逻辑是从文件读取PCM数据然后播放,代码编写环境Eclipse。
一、 Eclipse 创建Android工程
二、布局XML 创建文件 /res/layout/activity_audio_track.xml
布局文件就一个播放按钮
三、Activity类 创建/src/com/example/testopensl/AudioTrackActivity.java
package com.example.testopensl;
import com.example.audio.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class AudioTrackActivity extends Activity implements OnClickListener {
private static String URI_PCM = "/mnt/sdcard/pm.pcm";
static {
System.loadLibrary("TestAudio");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio_track);
findViewById(R.id.btn_play).setOnClickListener(this);
createEngine();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_play:
createAudioPlayer(URI_PCM);
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
shutdown();
}
/** Native methods, implemented in jni folder */
public static native void createEngine();
public static native boolean createAudioPlayer(String uri);
public static native void setPlayingAudioPlayer(boolean isPlaying);
public static native void setVolumeAudioPlayer(int millibel);
public static native void setMutAudioPlayer(boolean mute);
public static native void shutdown();
}
四、编写日志头文件,用于日志输出, 创建/jni/log.h 文件
#ifndef LOG_H_
#define LOG_H_
#include
#ifndef DGB
#define DGB 0
#endif
#ifndef LOG_TAG
#define LOG_TAG __FILE__
#endif
#ifndef ALOGD
#if DGB
#define ALOGD(...) \
__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#else
#define ALOGD(...) ((void)0)
#endif
#endif
#endif /* LOG_H_ */
五、用javah命令 生成jni 头文件. 文件目录/jni/com_example_testopensl_AudioTrackActivity.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class com_example_testopensl_AudioTrackActivity */
#ifndef _Included_com_example_testopensl_AudioTrackActivity
#define _Included_com_example_testopensl_AudioTrackActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_testopensl_AudioTrackActivity
* Method: createEngine
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_example_testopensl_AudioTrackActivity_createEngine
(JNIEnv *, jclass);
/*
* Class: com_example_testopensl_AudioTrackActivity
* Method: createAudioPlayer
* Signature: (Ljava/lang/String;)Z
*/
JNIEXPORT jboolean JNICALL Java_com_example_testopensl_AudioTrackActivity_createAudioPlayer
(JNIEnv *, jclass, jstring);
/*
* Class: com_example_testopensl_AudioTrackActivity
* Method: setPlayingAudioPlayer
* Signature: (Z)V
*/
JNIEXPORT void JNICALL Java_com_example_testopensl_AudioTrackActivity_setPlayingAudioPlayer
(JNIEnv *, jclass, jboolean);
/*
* Class: com_example_testopensl_AudioTrackActivity
* Method: setVolumeAudioPlayer
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_com_example_testopensl_AudioTrackActivity_setVolumeAudioPlayer
(JNIEnv *, jclass, jint);
/*
* Class: com_example_testopensl_AudioTrackActivity
* Method: setMutAudioPlayer
* Signature: (Z)V
*/
JNIEXPORT void JNICALL Java_com_example_testopensl_AudioTrackActivity_setMutAudioPlayer
(JNIEnv *, jclass, jboolean);
/*
* Class: com_example_testopensl_AudioTrackActivity
* Method: shutdown
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_example_testopensl_AudioTrackActivity_shutdown
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
六、JNI的实现 /jni/com_example_testopensl_AudioTrackActivity.cpp
/** log */
#define LOG_TAG "AudioTrackActivity"
#define DGB 1
#include "log.h"
#include
#include
#include
#include
#include
#include "com_example_testopensl_AudioTrackActivity.h"
class PlaybackThread;
// engine interfaces
static SLObjectItf engineObject = NULL;
static SLEngineItf engineEngine;
// output mix interfaces
static SLObjectItf outputMixObject = NULL;
// buffer queue player interfaces
static SLObjectItf bqPlayerObject = NULL;
static SLPlayItf bqPlayerPlay;
static SLAndroidSimpleBufferQueueItf bqPlayerBufferQueue;
static SLEffectSendItf bqPlayerEffectSend;
static SLVolumeItf bqPlayerVolume;
static PlaybackThread* mThread;
class PlaybackThread {
private:
FILE *mFile;
void* mBuffer;
size_t mSize;
bool read;
public:
PlaybackThread(const char* uri) :
mFile(NULL), mBuffer(NULL), mSize(0), read(true) {
mFile = fopen((char*) uri, "r");
if (mFile == NULL) {
ALOGD("open file error %s", uri);
return;
}
mBuffer = malloc(8192);
}
void start() {
if (mFile == NULL) {
return;
}
enqueueBuffer();
}
// release file buffer
void release() {
if (mFile != NULL) {
fclose(mFile);
mFile == NULL;
}
if (mBuffer != NULL) {
free(mBuffer);
mBuffer == NULL;
}
}
~PlaybackThread() {
release();
ALOGD("~PlaybackThread");
}
void enqueueBuffer() {
if (bqPlayerBufferQueue == NULL) {
return;
}
// for streaming playback, replace this test by logic to find and fill the next buffer
while (true) {
if (read) {
mSize = fread(mBuffer, 1, 8192, mFile);
}
if (mSize > 0) {
SLresult result;
// enqueue another buffer
result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, mBuffer, mSize);
if (result == SL_RESULT_BUFFER_INSUFFICIENT) {
read = false;
return;
}
read = true;
} else {
return;
}
}
}
// this callback handler is called every time a buffer finishes playing
static void playerCallback(SLAndroidSimpleBufferQueueItf bq, void *context) {
assert(NULL != context);
PlaybackThread* thread = (PlaybackThread *) context;
if (thread != NULL) {
thread->enqueueBuffer();
}
}
};
/*
* Class: com_example_testopensl_AudioTrackActivity
* Method: createEngine
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_example_testopensl_AudioTrackActivity_createEngine(JNIEnv *, jclass) {
SLresult result;
// create engine
result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
assert(SL_RESULT_SUCCESS == result);
(void) result;
// realize the engine
result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
assert(SL_RESULT_SUCCESS == result);
(void) result;
// get the engine interface, which is needed in order to create other objects
result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
assert(SL_RESULT_SUCCESS == result);
(void) result;
// create output mix,
result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 0, 0, 0);
assert(SL_RESULT_SUCCESS == result);
(void) result;
// realize the output mix
result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE);
assert(SL_RESULT_SUCCESS == result);
(void) result;
}
/*
* Class: com_example_testopensl_AudioTrackActivity
* Method: createAudioPlayer
* Signature: (Ljava/lang/String;)Z
*/
JNIEXPORT jboolean JNICALL Java_com_example_testopensl_AudioTrackActivity_createAudioPlayer(JNIEnv *env, jclass clazz, jstring uri) {
const char* utf8Uri = env->GetStringUTFChars(uri, NULL);
SLresult result;
// configure audio source
SLDataLocator_AndroidSimpleBufferQueue loc_bufq = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 3 };
SLDataFormat_PCM format_pcm = { SL_DATAFORMAT_PCM, 2, SL_SAMPLINGRATE_44_1,
SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16,
SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT, SL_BYTEORDER_LITTLEENDIAN };
SLDataSource audioSrc = { &loc_bufq, &format_pcm };
// configure audio sink
SLDataLocator_OutputMix loc_outmix = { SL_DATALOCATOR_OUTPUTMIX, outputMixObject };
SLDataSink audioSnk = { &loc_outmix, NULL };
// create audio player
const SLInterfaceID ids[3] = { SL_IID_BUFFERQUEUE, SL_IID_EFFECTSEND, SL_IID_VOLUME };
const SLboolean req[3] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
result = (*engineEngine)->CreateAudioPlayer(engineEngine, &bqPlayerObject, &audioSrc, &audioSnk, 3, ids, req);
assert(SL_RESULT_SUCCESS == result);
(void) result;
// realize the player
result = (*bqPlayerObject)->Realize(bqPlayerObject, SL_BOOLEAN_FALSE);
assert(SL_RESULT_SUCCESS == result);
(void) result;
// get the play interface
result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_PLAY, &bqPlayerPlay);
assert(SL_RESULT_SUCCESS == result);
(void) result;
// get the buffer queue interface
result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_BUFFERQUEUE, &bqPlayerBufferQueue);
assert(SL_RESULT_SUCCESS == result);
(void) result;
mThread = new PlaybackThread(utf8Uri);
// register callback on the buffer queue
result = (*bqPlayerBufferQueue)->RegisterCallback(bqPlayerBufferQueue, PlaybackThread::playerCallback, mThread);
assert(SL_RESULT_SUCCESS == result);
(void) result;
// get the effect send interface
result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_EFFECTSEND, &bqPlayerEffectSend);
assert(SL_RESULT_SUCCESS == result);
(void) result;
// get the volume interface
result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_VOLUME, &bqPlayerVolume);
assert(SL_RESULT_SUCCESS == result);
(void) result;
// set the player's state to playing
result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_PLAYING);
assert(SL_RESULT_SUCCESS == result);
(void) result;
//pthread_t id;
mThread->start();
env->ReleaseStringUTFChars(uri, utf8Uri);
ALOGD("createAudioPlayer finish");
return 0;
}
/*
* Class: com_example_testopensl_AudioTrackActivity
* Method: setPlayingAudioPlayer
* Signature: (Z)V
*/
JNIEXPORT void JNICALL Java_com_example_testopensl_AudioTrackActivity_setPlayingAudioPlayer(JNIEnv *, jclass, jboolean) {
}
/*
* Class: com_example_testopensl_AudioTrackActivity
* Method: setVolumeAudioPlayer
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_com_example_testopensl_AudioTrackActivity_setVolumeAudioPlayer(JNIEnv *, jclass, jint) {
}
/*
* Class: com_example_testopensl_AudioTrackActivity
* Method: setMutAudioPlayer
* Signature: (Z)V
*/
JNIEXPORT void JNICALL Java_com_example_testopensl_AudioTrackActivity_setMutAudioPlayer(JNIEnv *, jclass, jboolean) {
}
/*
* Class: com_example_testopensl_AudioTrackActivity
* Method: shutdown
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_example_testopensl_AudioTrackActivity_shutdown(JNIEnv *, jclass) {
//destory player object
if (bqPlayerObject != NULL) {
(*bqPlayerObject)->Destroy(bqPlayerObject);
bqPlayerPlay = NULL;
bqPlayerBufferQueue = NULL;
bqPlayerEffectSend = NULL;
bqPlayerVolume = NULL;
}
// destroy output mix object, and invalidate all associated interfaces
if (outputMixObject != NULL) {
(*outputMixObject)->Destroy(outputMixObject);
outputMixObject = NULL;
}
// destroy engine object, and invalidate all associated interfaces
if (engineObject != NULL) {
(*engineObject)->Destroy(engineObject);
engineObject = NULL;
engineEngine = NULL;
}
if (mThread != NULL) {
delete mThread;
mThread = NULL;
}
}
方法说明:
Java_com_example_testopensl_AudioTrackActivity_createEngine 创建引擎
Java_com_example_testopensl_AudioTrackActivity_createAudioPlayer 创建播放器并开始播放
Java_com_example_testopensl_AudioTrackActivity_shutdown 释放资源
Java_com_example_testopensl_AudioTrackActivity_setPlayingAudioPlayer 未实现
Java_com_example_testopensl_AudioTrackActivity_setVolumeAudioPlayer 未实现
Java_com_example_testopensl_AudioTrackActivity_setMutAudioPlayer 未实现
未实现方法可以参考 URI播放实现
七、创建/jni/Android.mk 编译文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := TestAudio
LOCAL_SRC_FILES := com_example_testopensl_AudioTrackActivity.cpp
LOCAL_LDLIBS += -llog -lOpenSLES -landroid
include $(BUILD_SHARED_LIBRARY)
LOCAL_LDLIBS 需要加llog、lOpenSLES、landroid 编译链接库
八、AndroidManifest 配置
编写完Eclipse 结构图如下:
运行之后界面图如下:
代码中使用的PCM数据信息:
采样率:44100
声道:2
编码:16BIT
关键代码是在 com_example_testopensl_AudioTrackActivity.cpp 中创建播放器AudioSrc 参数
// configure audio source
SLDataLocator_AndroidSimpleBufferQueue loc_bufq = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 3 };
SLDataFormat_PCM format_pcm = { SL_DATAFORMAT_PCM, 2, SL_SAMPLINGRATE_44_1,
SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16,
SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT, SL_BYTEORDER_LITTLEENDIAN };
SLDataSource audioSrc = { &loc_bufq, &format_pcm };
上面指定了播放PCM数据的信息