Jump to content

Qt for HarmonyOS/qtohosextras doc/Audio Stream Usage Hint Example: Difference between revisions

From Qt Wiki
Fix example page conversion (QTFOROH-2307)
Add code snippets to example pages (QTFOROH-2307)
Line 8: Line 8:


A combo box lists all [[Qt_for_HarmonyOS/qtohosextras_doc/Multimedia_Namespace#AudioStreamUsageHint-enum|QtOhosExtras::Multimedia::AudioStreamUsageHint]] values. Two buttons play a bundled sound either through QSoundEffect or through QSound; before playback the selected hint is applied with Multimedia::setAudioStreamUsageHintProperty(). The hint influences how the system routes the audio and which volume policy applies.
A combo box lists all [[Qt_for_HarmonyOS/qtohosextras_doc/Multimedia_Namespace#AudioStreamUsageHint-enum|QtOhosExtras::Multimedia::AudioStreamUsageHint]] values. Two buttons play a bundled sound either through QSoundEffect or through QSound; before playback the selected hint is applied with Multimedia::setAudioStreamUsageHintProperty(). The hint influences how the system routes the audio and which volume policy applies.
For QSoundEffect the hint is set before the source is assigned and playback starts:
<syntaxhighlight lang="cpp">
    auto *soundEffect = new QSoundEffect(this);
    QtOhosExtras::Multimedia::setAudioStreamUsageHintProperty(soundEffect, selectedAudioRole());
    soundEffect->setSource(QUrl(QString::fromLatin1("qrc:/test.wav")));
    soundEffect->setLoopCount(1);
</syntaxhighlight>
The same function works for QSound:
<syntaxhighlight lang="cpp">
    auto *sound = new QSound(QString::fromLatin1(":/test.wav"), this);
    QtOhosExtras::Multimedia::setAudioStreamUsageHintProperty(sound, selectedAudioRole());
</syntaxhighlight>


'''Note: '''The target object must be a QSoundEffect or QSound instance; other object types are rejected with an error log. The example sets the hint before starting playback.
'''Note: '''The target object must be a QSoundEffect or QSound instance; other object types are rejected with an error log. The example sets the hint before starting playback.

Revision as of 10:10, 16 September 2026

< Qt for HarmonyOS / qtohosextras doc

Audio Stream Usage Hint Example

Demonstrates tagging Qt sound playback with an OHOS audio stream usage hint.


A combo box lists all QtOhosExtras::Multimedia::AudioStreamUsageHint values. Two buttons play a bundled sound either through QSoundEffect or through QSound; before playback the selected hint is applied with Multimedia::setAudioStreamUsageHintProperty(). The hint influences how the system routes the audio and which volume policy applies.

For QSoundEffect the hint is set before the source is assigned and playback starts:

     auto *soundEffect = new QSoundEffect(this);
     QtOhosExtras::Multimedia::setAudioStreamUsageHintProperty(soundEffect, selectedAudioRole());
     soundEffect->setSource(QUrl(QString::fromLatin1("qrc:/test.wav")));
     soundEffect->setLoopCount(1);

The same function works for QSound:

     auto *sound = new QSound(QString::fromLatin1(":/test.wav"), this);
     QtOhosExtras::Multimedia::setAudioStreamUsageHintProperty(sound, selectedAudioRole());

Note: The target object must be a QSoundEffect or QSound instance; other object types are rejected with an error log. The example sets the hint before starting playback.