android 从服务器获取音频,android 怎么样获得mediaplayer播放的音频数据

切包 copy到mData

OSStatus playCallback(void *inRefCon,

AudioUnitRenderActionFlags *ioActionFlags,

const AudioTimeStamp *inTimeStamp,

UInt32 inBusNumber,

UInt32 inNumberFrames,

AudioBufferList *ioData)

{

AudioPlayer *THIS = (__bridge AudioPlayer *)inRefCon;

//How do I use the AudioUnit to play the audio stream from server?

return noErr;

}

这个是我的回调函数,里面具体改些么写, 写完有大神能指点下。 还有就是,使用AudioUnit进行播放, 什么情况下,会调用这个播放回调, 希望大神也指点指点。

下面是我从服务器获取数据的代码:

- (void)openPlayThreadWithRtmpURL:(NSString *)rtmpURL {

spx_int16_t *input_buffer;

do {

if (self.rtmpDelegate) {

[self.rtmpDelegate evenCallbackWithEvent:2000];

}

//init speex decoder and config;

speex_bits_init(&dbits);

dec_state = speex_decoder_init(&speex_wb_mode);

speex_decoder_ctl(dec_state, SPEEX_GET_FRAME_SIZE, &dec_frame_size);

input_buffer = malloc(dec_frame_size * sizeof(short));

NSLog(@"Init Speex decoder success frame_size = %d",dec_frame_size);

//init rtmp

pPlayRtmp = RTMP_Alloc();

RTMP_Init(pPlayRtmp);

NSLog(@"Play RTMP_Init %@\n", rtmpURL);

if (!RTMP_SetupURL(pPlayRtmp, (char*)[rtmpURL UTF8String])) {

NSLog(@"Play RTMP_SetupURL error\n");

if(self.rtmpDelegate) {

[self.rtmpDelegate evenCallbackWithEvent:2002];

}

break;

}

if (!RTMP_Connect(pPlayRtmp, NULL) || !RTMP_ConnectStream(pPlayRtmp, 0)) {

NSLog(@"Play RTMP_Connect or RTMP_ConnectStream error\n");

if(self.rtmpDelegate) {

[self.rtmpDelegate evenCallbackWithEvent:2002];

}

break;

}

if(self.rtmpDelegate) {

[self.rtmpDelegate evenCallbackWithEvent:2001];

}

NSLog(@"Player RTMP_Connected \n");

RTMPPacket rtmp_pakt = {0};

isStartPlay = YES;

while (isStartPlay && RTMP_ReadPacket(pPlayRtmp, &rtmp_pakt)) {

if (RTMPPacket_IsReady(&rtmp_pakt)) {

if (!rtmp_pakt.m_nBodySize) {

continue;

}

if (rtmp_pakt.m_packetType == RTMP_PACKET_TYPE_AUDIO) {

NSLog(@"Audio size = %d head = %d time = %d", rtmp_pakt.m_nBodySize, rtmp_pakt.m_body[0], rtmp_pakt.m_nTimeStamp);

speex_bits_read_from(&dbits, rtmp_pakt.m_body + 1, rtmp_pakt.m_nBodySize - 1);

speex_decode_int(dec_state, &dbits, input_buffer); //获取到的,并且解码成功的数据就存放在这个 input_buffer里面

//怎么用AudioUnit将input_buffer里的数据进行播放?

free(sourceBuffer.mData);

NSLog(@"解码成功!");

} else if (rtmp_pakt.m_packetType == RTMP_PACKET_TYPE_VIDEO) {

// 处理视频数据包

} else if (rtmp_pakt.m_packetType == RTMP_PACKET_TYPE_INVOKE) {

// 处理invoke包

NSLog(@"RTMP_PACKET_TYPE_INVOKE");

RTMP_ClientPacket(pPlayRtmp,&rtmp_pakt);

} else if (rtmp_pakt.m_packetType == RTMP_PACKET_TYPE_INFO) {

// 处理信息包

//NSLog(@"RTMP_PACKET_TYPE_INFO");

} else if (rtmp_pakt.m_packetType == RTMP_PACKET_TYPE_FLASH_VIDEO) {

// 其他数据

int index = 0;

while (1) {

int StreamType; //1-byte

int MediaSize; //3-byte

int TiMMER; //3-byte

int Reserve; //4-byte

char* MediaData; //MediaSize-byte

int TagLen; //4-byte

StreamType = rtmp_pakt.m_body[index];

index += 1;

MediaSize = bigThreeByteToInt(rtmp_pakt.m_body + index);

index += 3;

TiMMER = bigThreeByteToInt(rtmp_pakt.m_body + index);

index += 3;

Reserve = bigFourByteToInt(rtmp_pakt.m_body + index);

index += 4;

MediaData = rtmp_pakt.m_body + index;

index += MediaSize;

TagLen = bigFourByteToInt(rtmp_pakt.m_body + index);

index += 4;

//NSLog(@"bodySize:%d index:%d",rtmp_pakt.m_nBodySize,index);

//LOGI("StreamType:%d MediaSize:%d TiMMER:%d TagLen:%d\n", StreamType, MediaSize, TiMMER, TagLen);

if (StreamType == 0x08) {

//音频包

//int MediaSize = bigThreeByteToInt(rtmp_pakt.m_body+1);

// LOGI("FLASH audio size:%d head:%d time:%d\n", MediaSize, MediaData[0], TiMMER);

speex_bits_read_from(&dbits, MediaData + 1, MediaSize - 1);

speex_decode_int(dec_state, &dbits, input_buffer);

//[mAudioPlayer putAudioData:input_buffer];

// putAudioQueue(output_buffer,dec_frame_size);

} else if (StreamType == 0x09) {

//视频包

// LOGI( "video size:%d head:%d\n", MediaSize, MediaData[0]);

}

if (rtmp_pakt.m_nBodySize == index) {

break;

}

}

}

RTMPPacket_Free(&rtmp_pakt);

}

}

if (isStartPlay) {

if(self.rtmpDelegate) {

[self.rtmpDelegate evenCallbackWithEvent:2005];

}

isStartPlay = NO;

}

} while (0);

[mAudioPlayer stopPlay];

if (self.rtmpDelegate) {

[self.rtmpDelegate evenCallbackWithEvent:2004];

}

if (RTMP_IsConnected(pPlayRtmp)) {

RTMP_Close(pPlayRtmp);

}

RTMP_Free(pPlayRtmp);

free(input_buffer);

speex_bits_destroy(&dbits);

speex_decoder_destroy(dec_state);

}

取消

评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值