如何使用 AudioQueue 从特定位置启动音频文件?

发布于 2024-09-11 19:45:32 字数 250 浏览 1 评论 0原文

我分析了iPhone开发论坛的“SpeakHere”示例代码。

有一个启动 AudioQueue 的代码如下。

AudioTimeStamp ats = {0};
AudioQueueStart(mQueue, &ats);

但我不知道如何启动文件中间。

我用包括负值在内的各种值更改了 AudioTimeStamp。但它不起作用。

请让我知道您的好意见。谢谢。

I have analyzed "SpeakHere" sample code of iPhone dev forum.

There is a code for starting AudioQueue as following..

AudioTimeStamp ats = {0};
AudioQueueStart(mQueue, &ats);

But I have no idea that how to start middle of file.

I changed AudioTimeStamp with various values include negative. But it does not works.

Please let me know your great opinion. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

め可乐爱微笑 2024-09-18 19:45:32

AudioQueueStart 不是可以帮助您执行此操作的函数。时间就像一个延迟,如果你传递NULL那么这意味着队列将尽快启动。

您必须传递要播放的帧并将其排入队列,以计算您必须知道文件具有的帧数以及要播放的(相对)位置。

这些是如何在 SpeakHere 中进行制作的说明

在新的(基于 objc++ 的)SpeakHere

中在 AQPlayer.h 中添加一个私有实例变量:

UInt64 mPacketCount;

和一个公共方法:

void SetQueuePosition(float position) { mCurrentPacket = mPacketCount*position; };

在 AQPlayer.mm 中的 AQPlayer::SetupNewQueue()mIsInitialized = true; 之前添加:

// get the total number of packets
UInt32 sizeOfPacketsCount = sizeof(mPacketCount);
XThrowIfError (AudioFileGetProperty (mAudioFile, kAudioFilePropertyAudioDataPacketCount, &sizeOfPacketsCount, &mPacketCount), "get packet count");

现在您必须使用它(例如,在 SpeakHereControler.mm 中添加它并将其链接到 UISlider):

- (IBAction) sliderValueChanged:(UISlider *) sender
{
    float value = [sender value];
    player->SetQueuePosition(position);
}

为什么它有效:

为队列提供数据的播放回调函数 AudioQueueOutputCallback带有新数据包,在新的 SpeakHere 中是: void AQPlayer::AQBufferCallback( , , ) 调用 AudioFileReadPackets 来读取文件的特定部分并将其排入队列。对于该任务,使用了 mCurrentPacket,这就是我们刚刚在上述方法中调整的内容,因此您想要播放的部分被读取、排队并最终播放:)


Just for historical reasons :)

在旧的(基于objc)SpeakHere

中的AudioPlayer.h中添加一个实例变量:

UInt64      totalFrames;

AudioPlayer.m里面
- (void) openPlaybackFile: (CFURLRef) soundFile
添加:

UInt32 sizeOfTotalFrames = sizeof(UInt64);
AudioFileGetProperty (                        
          [self audioFileID], 
          kAudioFilePropertyAudioDataPacketCount,
          &sizeOfTotalFrames,
          &totalFrames
          );

然后向 AudioPlayer.h 和 .m 添加一个方法

- (void) setRelativePlaybackPosition: (float) position 
{
    startingPacketNumber = totalFrames * position;
}

现在您必须使用它(例如在 AudioViewController 中添加此方法并将其链接到 UISlider):

- (IBAction) setPlaybackPosition: (UISlider *) sender
{
    float value = [sender value];
    [audioPlayer setRelativePlaybackPosition: value];
}

当值为 0 时,您将从头开始播放,0.5 从中间开始播放,等等

希望这有帮助。

AudioQueueStart is not the function that will help you to do that. The time there is like a delay, if you pass NULL then it means that the queue will start ASAP.

You have to pass the frame you want to play and enqueue it, to calculate that you have to know the number of frames your file has and the (relative) position you want to play.

These are instructions how to make it in SpeakHere

In the new (objc++ based) SpeakHere

In AQPlayer.h add a private instance variable:

UInt64 mPacketCount;

and a public method:

void SetQueuePosition(float position) { mCurrentPacket = mPacketCount*position; };

In AQPlayer.mm inside AQPlayer::SetupNewQueue() before mIsInitialized = true; add:

// get the total number of packets
UInt32 sizeOfPacketsCount = sizeof(mPacketCount);
XThrowIfError (AudioFileGetProperty (mAudioFile, kAudioFilePropertyAudioDataPacketCount, &sizeOfPacketsCount, &mPacketCount), "get packet count");

Now you have to use it (In SpeakHereControler.mm add this and link it to a UISlider for example):

- (IBAction) sliderValueChanged:(UISlider *) sender
{
    float value = [sender value];
    player->SetQueuePosition(position);
}

Why this works:

The playback callback function AudioQueueOutputCallback that feeds the queue with new packets and which in the new SpeakHere is: void AQPlayer::AQBufferCallback( , , ) calls AudioFileReadPackets to read and enqueue a certain part of a file. For that task mCurrentPacket is used and that is what we just adjusted in above methods, hence the part you wanted to play is read, enqueued and finally played :)


Just for historical reasons :)

In the old (objc based) SpeakHere

In AudioPlayer.h add an instance variable:

UInt64      totalFrames;

AudioPlayer.m inside
- (void) openPlaybackFile: (CFURLRef) soundFile
add:

UInt32 sizeOfTotalFrames = sizeof(UInt64);
AudioFileGetProperty (                        
          [self audioFileID], 
          kAudioFilePropertyAudioDataPacketCount,
          &sizeOfTotalFrames,
          &totalFrames
          );

Then add a method to AudioPlayer.h and .m

- (void) setRelativePlaybackPosition: (float) position 
{
    startingPacketNumber = totalFrames * position;
}

Now you have to use it (In AudioViewController add this and link it to a UISlider for example):

- (IBAction) setPlaybackPosition: (UISlider *) sender
{
    float value = [sender value];
    [audioPlayer setRelativePlaybackPosition: value];
}

When value is 0 you will play from the beggining, 0.5 from the middle, etc.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文