使用PlayFile苹果示例与OpenGL冻结OpenGL,直到播放声音结束 [英] Using PlayFile apple example with OpenGL freezes OpenGL until playing sound is over

查看:220
本文介绍了使用PlayFile苹果示例与OpenGL冻结OpenGL,直到播放声音结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C ++编写一个小应用程序,使用OpenGL的Xcode。
我试图找到一种方法来添加简单的声音到我的应用程序。我不是太成功的OpenAL(Alut不可用在Mac),不能使sfml和SDL在我的系统上工作。所以我决定尝试Apple的PlayFile示例:

I am working on a little app in C++ with Xcode which uses OpenGL. I tried to find a way to add simple sounds to my app. I was not too succesful with OpenAL (Alut is not available in Mac) and couldn't make sfml and SDL work on my system. So I decided to try Apple's PlayFile example:

http://developer.apple.com/library/mac/#samplecode/PlayFile/Introduction/Intro.html#//apple_ref/doc / uid / DTS40008651

我可以使用此示例播放声音。但是,当声音开始播放时,OpenGL冻结,即所有的动画停止,直到声音播放完毕。

I can play sounds using this example. However, when a sound starts playing, OpenGL freezes, namely all the animation stops until sound is done playing.

我检查了文件PlayFile.cpp并找到了这一行:

I checked the file PlayFile.cpp and found this line:

    usleep ((int)(fileDuration * 1000. * 1000.));

我认为是负责冻结OpenGL。在这条线之前,我们开始播放声音。在这行之后,我们清理缓冲区。有什么办法,我可以更改代码,所以,声音开始播放,OpenGL继续其任务,一旦声音完成播放我们清理缓冲区?我尝试使用其他解决方案,如指针,在播放开始后清理,但现在我不能使任何声音的应用程序。

which I think is responsible for freezing OpenGL. Before this line, we start playing sound. After this line we clean up buffers. Is there any way that I can change the code, so that, sound starts playing, OpenGL continues its tasks, once sound is done playing we clean the buffers? I tried to use other solutions like pointers, to clean up after playing started, but now I can not make any sound out of the app.

任何建议? >

Any suggestions?

推荐答案

好的,经过多天的研究,我发现了解决这个问题的方法。我使用pthread来创建多个线程,每个声音一个。我刚添加了

OK, after days of research I found a way to fix this problem. I used pthread to create multiple threads, one for each sound. I just added

#include <pthread.h>

到定义线程的任何文件。我为我的声音和线程定义了一个列表:

to any file that defines a thread. I defined a list for my sounds and threads:

pthread_t threads[3];
string sounds[3] = {"back.wav", "Arrow1.wav", "Arrow2.wav"};

并使用了一个简单的函数:

And used a simple function:

void * playSound(void *threadid)
{
long tid;
tid = (long)threadid;
player(sounds[tid], 1);
pthread_exit(NULL);
}

在创建主题时播放声音:

that plays a sound when I create a thread:

pthread_create(&threads[2], NULL, playSound, (void *)2);

这样我的动画可以很容易地与声音结合。

This way my animation can be combined with sound easily.

这篇关于使用PlayFile苹果示例与OpenGL冻结OpenGL,直到播放声音结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆