Qt Creator (32 ビット QT 4.8.0 ベース) を使用して C++ GUI アプリケーションを作成しています。私の目標は、自然音をランダムに再生するアプリケーションを作成し、各音にさまざまな属性を付けることです。これらのサウンドを再生するために Phonon ライブラリを使用しようとしています。
public QThread を継承する ZooKeeper というクラスがあります。このクラスには、ループするメイン実行関数があります。
while(true)
{
ManageCritters();
QThread::msleep(10);
}
関数内ManageCritters();
で、特定の時間に特定の動物に固有のファイル名に基づいてサウンド ファイルを再生します。これが私がそれを実行する方法です:
// create our media objects and an audio-output
Phonon::MediaObject *mediaObject = new Phonon::MediaObject(this);
Phonon::AudioOutput *autioOut = new Phonon::AudioOutput(Phonon::MusicCategory, this);
// link the two together
Phonon::createPath(mediaObject, audioOut);
// set our audio source to the filename we want to play
mediaObject->setCurrentSource(filename);
// play the audio file
mediaObject->play();
これはすべて正常にコンパイルされますが、実行時エラーが発生します。
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QThread(0x82c7e48), parent's thread is QThread(0x8166ee8), current thread is QThread(0x82c7e48)
WARNING: Phonon needs QCoreApplication::applicationName to be set to export audio output names through the DBUS interface
KGlobal::locale() must be called from the main thread before using i18n() in threads. KApplication takes care of this. If not using KApplication, call KGlobal::locale() during initialization.
The program has unexpectedly finished.
QThreads 内でオーディオ再生をセットアップする方法を理解できていないように見えますが、エラーが発生している場所や修正方法がわかりません。
オーディオの再生を処理するために別の設定を行う必要がありますか? これはすべて OOP です。Critter()
個々の生き物 (虫、鳥など) を表す別のクラスがあります。理想的には、各「クリッター」が独自のオーディオ再生を処理するようにしたいと思います(オーディオ再生をCritter()
クラスの関数にします)。しかし、このCritter()
クラスを取得してフォノン ライブラリにリンクし、オーディオ ファイルを再生する方法がわかりません。
提案やサンプルコードはありますか?