Qt5 +如何设置QMediaPlayer的默认音频设备 [英] Qt5+ How to set default audio device for QMediaPlayer

查看:4222
本文介绍了Qt5 +如何设置QMediaPlayer的默认音频设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序使用Qt,我需要使它使用输出设备播放声音,用户在首选项中选择。我可以通过调用以下代码列出Windows中所有可用的设备:

  QList< QAudioDeviceInfo> devices = QAudioDeviceInfo :: availableDevices(QAudio :: AudioOutput); 
foreach(QAudioDeviceInfo i,devices)
this-> ui-> comboBox-> addItem(i.deviceName());

但是我不知道如何更改作为我的应用程序的默认设备的设备 QMediaPlayer 会使用该设备播放所有声音,而不是默认声音。我该怎么办?



基本上,我想要实现类似的首选项对话框,像这样从Microsoft Lync:我只是在窗口只有Qt5 +特定的解决方案,虽然跨平台的解决方案可能是最好的。



>



根据microsoft: https://social.technet.microsoft.com/Forums/windows/en-US/b1d1acac-1f21-4d23-8d68-98964d67c2c7/assigning - 应用程序到不同的声音输出窗口7介绍了可以做到这一点的API。

解决方案

似乎你应该做这样的事情是QMediaPlayer *):

  QMediaService * svc = player-> service 
if(svc!= nullptr)
{
QAudioOutputSelectorControl * out = qobject_cast< QAudioOutputSelectorControl *>
(svc-> requestControl(QAudioOutputSelectorControl_iid));
if(out!= nullptr)
{
out-> setActiveOutput(this-> ui-> comboBox-> currentText());
svc-> releaseControl(out);
}
}

但由于 this 我无法在我的Win7安装上测试这个。



UPDATE:

好吧,这里是我做的修正(用Desktop_Qt_5_4_0_MSVC2013_32 / 64-Debug / Release测试;你可能需要更改不同工具链的vtable代码):





如何获取带有友好名称的设备列表(键 - 名称;值 - deviceID):

  MFAudioEndpointControl_Fixed :: availableOutputsFriendly(); 
for(auto it = outputs.cbegin(),e = outputs.cend(); it!= e; ++ it)
{
this-> ui-> comboBox - > addItem(it.key(),it.value());
this-> ui-> plainTextEdit-> appendPlainText(it.key()+(+ it.value()+));
}

您必须为您创建的每个QMediaPlayer应用以下修复: p>

  QMediaPlayer * player = new QMediaPlayer 

QMediaService * svc = player-> service();
if(svc!= nullptr)
{
QAudioOutputSelectorControl * out = reinterpret_cast< QAudioOutputSelectorControl *>
(svc-> requestControl(QAudioOutputSelectorControl_iid));
if(out!= nullptr)
{
new MFAudioEndpointControl_Fixed_Helper(out); //< - the fix;注意它是一个HELPER类别

out-> setActiveOutput(this-> ui-> comboBox-> itemData(this-> ui-> comboBox-> currentIndex .toString()); //我们必须传递deviceID,而不是名字
svc-> releaseControl(out);
}
}


I have a program using Qt and I need to make it play sounds using an output device that user selects in preferences. I can list all available devices that are in windows by calling this code:

QList<QAudioDeviceInfo> devices = QAudioDeviceInfo::availableDevices(QAudio::AudioOutput);
foreach (QAudioDeviceInfo i, devices)
    this->ui->comboBox->addItem(i.deviceName());

However I have no idea how can I change the device that would be a default device for my application so that QMediaPlayer would play all sounds using that device, instead of a default one. How can I do that? I am fine with windows only Qt5+ specific solution, although cross-platform solution would be probably best.

Basically I want to implement similar preferences dialog like this one from Microsoft Lync:

According to microsoft: https://social.technet.microsoft.com/Forums/windows/en-US/b1d1acac-1f21-4d23-8d68-98964d67c2c7/assigning-an-application-to-different-sound-outputs windows 7 introduced API's that can do that. However I have no idea how and where is that documented.

解决方案

It seems you are supposed to do something like this (where player is QMediaPlayer*):

QMediaService *svc = player->service();
if (svc != nullptr)
{
    QAudioOutputSelectorControl *out = qobject_cast<QAudioOutputSelectorControl *>
                                       (svc->requestControl(QAudioOutputSelectorControl_iid));
    if (out != nullptr)
    {
        out->setActiveOutput(this->ui->comboBox->currentText());
        svc->releaseControl(out);
    }
}

But due to this and this I am unable to test this on my Win7 installation.

UPDATE:

Well, here's the fix I made (tested with Desktop_Qt_5_4_0_MSVC2013_32/64-Debug/Release; you might need to change the vtable code for different toolchains):

How to get the list of devices with their 'friendly names' (key - name; value - deviceID):

auto outputs = MFAudioEndpointControl_Fixed::availableOutputsFriendly();
for (auto it = outputs.cbegin(), e = outputs.cend(); it != e; ++it)
{
    this->ui->comboBox->addItem(it.key(), it.value());
    this->ui->plainTextEdit->appendPlainText(it.key() + " (" + it.value() + ")");
}

You will have to apply the following fix for each QMediaPlayer you create:

QMediaPlayer *player = new QMediaPlayer(this);

QMediaService *svc = player->service();
if (svc != nullptr)
{
    QAudioOutputSelectorControl *out = reinterpret_cast<QAudioOutputSelectorControl*>
                                       (svc->requestControl(QAudioOutputSelectorControl_iid));
    if (out != nullptr)
    {
        new MFAudioEndpointControl_Fixed_Helper(out); // <- the fix; notice that it's a HELPER class

        out->setActiveOutput(this->ui->comboBox->itemData(this->ui->comboBox->currentIndex()).toString()); // we have to pass deviceID, not the name
        svc->releaseControl(out);
    }
}

这篇关于Qt5 +如何设置QMediaPlayer的默认音频设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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