AudioTrack執行個體代碼 C++代碼

來源:互聯網
上載者:User

char path[] = "/sdcard/testmusic.pcm";

AudioTrack *lpTrack = NULL;

static void audioCallback(int event, void* user, void *info)
{
     FILE *fp = (FILE*)user;
    if (event == AudioTrack::EVENT_MORE_DATA)
    {
        // set size to 0 to signal we're not using the callback to write more data
        AudioTrack::Buffer* pBuff = (AudioTrack::Buffer*)info;
        pBuff->size = 0;
        if (feof(fp) == 0)
        {
            pBuff->size = fread(pBuff->raw, 1, 2048, fp);
        }
        lpTrack->flush();
    }
    else if ( event ==AudioTrack::EVENT_LOOP_END )
    {
        //fclose( fp );
        //free(lpTrack);

    }
    else if (event == AudioTrack::EVENT_MARKER)
    {
//        audiotrack_callback_cookie *callbackInfo = (audiotrack_callback_cookie *)user;
//        JNIEnv *env = AndroidRuntime::getJNIEnv();
//        if (user && env) {
//            env->CallStaticVoidMethod(
//                callbackInfo->audioTrack_class,
//                javaAudioTrackFields.postNativeEventInJava,
//                callbackInfo->audioTrack_ref, event, 0,0, NULL);
//            if (env->ExceptionCheck()) {
//                env->ExceptionDescribe();
//                env->ExceptionClear();
//            }
//        }

    }
    else if (event == AudioTrack::EVENT_NEW_POS)
    {
//        audiotrack_callback_cookie *callbackInfo = (audiotrack_callback_cookie *)user;
//        JNIEnv *env = AndroidRuntime::getJNIEnv();
//        if (user && env) {
//            env->CallStaticVoidMethod(
//                callbackInfo->audioTrack_class,
//                javaAudioTrackFields.postNativeEventInJava,
//                callbackInfo->audioTrack_ref, event, 0,0, NULL);
//            if (env->ExceptionCheck()) {
//                env->ExceptionDescribe();
//                env->ExceptionClear();
//            }
//        }
    }
}

void Java_com_jqbar_SkiaView_AudioTest(JNIEnv *env, jobject thizz)
{

    char buf[4096] = "";
    int offset = 0;
    int num_read = 0;

    FILE *fp = NULL;

    fp = fopen(path, "r");;
//    fseek(fp, 0, SEEK_END);
//    int fileSize = ftell(fp);
//    rewind(fp);

    lpTrack = new AudioTrack(
            AudioSystem::MUSIC,// stream type
            44100,
            AudioSystem::PCM_16_BIT,// word length, PCM
            AudioSystem::CHANNEL_OUT_STEREO,
            0, 0, audioCallback, fp, 0 );
    lpTrack->initCheck();
    lpTrack->setVolume(1.0, 1.0);
    lpTrack->start();

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.