我有一个应用程序将数据包排入AudioQueue,它的工作正常。问题是当我在网络中出现延迟时,我无法及时向AudioQueue提供数据包。
所有应用程序运行良好且enqueueBuffer不会返回任何错误,但AudioQueue正在丢弃数据包(因此我没有声音),因为它们太旧了。
我可以强制AudioQueue播放这些音频数据包吗?或者至少知道数据包被丢弃了吗?因为如果我知道它,我可以做Pause-Play重新启动队列...(不是很好的解决方案,但我没有更好的事情)
因为延迟可能很大,我不能使用大缓冲区,因为这样可以减少错误,但不能解决问题
非常感谢
答案 0 :(得分:0)
你走在正确的轨道上。当您到达网络缓冲区末尾时,可以通过检测回调过程来处理网络延迟,然后暂停AudioQueue。稍后,一旦缓冲了足够的数据包,就重新启动队列。您的代码看起来像这样
if playerState.packetsRead == playerState.packetsWritten {
playerState.isPlaying = false
AudioQueuePause(aq)
}
并在您的网络代码中
if (playerState.packetsWritten >= playerState.packetsRead + kNumPacketsToBuffer) {
if !playerState.isPlaying {
playerState.isPlaying = true
for buffer in playerState.buffers {
audioCallback(playerState, aq: playerState.queue, buffer: buffer)
}
AudioQueueStart(playerState.queue, nil)
}
}
您还需要更新代码,以便每次从网络收到数据包时,playerState.packetsWritten
会增加,而playerState.packetsRead
在向音频队列添加数据包时也会增加kNumPacketsToBuffer
。 class UShape {
public:
virtual void setWidth (int w) =0;
virtual void setHeight (int h) =0;
virtual int getArea() =0;
};
class Rectangle: public UShape {
private:
int width;
int height;
public:
void setWidth (int w) { this.width = w; }
void setHeight (int h) { this.height = h; }
int getArea (void) { return (width * height); }
};
class Triangle: public UShape {
private:
int width;
int height;
public:
void setWidth (int w) { this.width = w; }
void setHeight (int h) { this.height = h; }
int getArea (void) { return (width * height) / 2; }
};
class bucket{
private:
std::unique_ptr<UShape> myshape; //c++11
public:
//do something with myshape
void defineShape(int i){
if (i == 1){
myshape = std::make_unique<Rectangle>(); //C++14
myshape->setWidth(5);
myshape->setHeight(5);
}
}
void printArea(){
cout << myshape->getArea();
}
的最佳数量取决于编解码器和网络条件。我建议使用256作为AAC并根据性能调高/调低。