Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QAudioOutputSelectorControl How to change device, or find out why it doesn't work



  • I'm trying to change the audio device for a player, but i can't seem to get it to work... I started using qt5-how-to-set-default-audio-device-for-qmediaplayer for an example.

    I don't quite get why he's using the mfaudioendpoint helper thing... It seems like QAudioOutputSelectorControl isn't sufficient to change the active output on its own? The second solution in that stack overflow shows how to change the default on a system level. I would like to only change it for the application.

    void Backend::setVirtualAudioDevice(const QString & device)
    {
    	m_sVirtualSoundDevice = device;
    	qDebug() << "Changing output device" <<  device;
    
    	// Get the windows ID from the vista "API"
    	QString id = m_audioDeviceList.getId({WindowsComWrapper::render, WindowsComWrapper::console}, device);
    	qDebug() << id;
    
    	QString guid = id.split('.').last().toUpper();
    
    
    	QMediaService *svc = m_defaultOut.service();
    	if (svc == nullptr)
    		return;
    
    	auto mediaControl = svc->requestControl(QAudioOutputSelectorControl_iid);
    	QAudioOutputSelectorControl *out = dynamic_cast<QAudioOutputSelectorControl*>(mediaControl);
    
    	if(out == nullptr)
    		return;
    
    	FINALLY(svc->releaseControl(out));
    
    	QStringList outputs = out->availableOutputs();
    	// Whats the difference between wav/directsound?
    	QStringList dsOutput = outputs.filter(QRegularExpression(guid + '$')).filter(QRegularExpression("DirectSound"));
    
    	Q_ASSERT(1 == dsOutput.count());
    
    	QString matchedDevice = dsOutput.first();
    	qDebug() << "Matched device " << matchedDevice;
    	out->setActiveOutput(matchedDevice); // we have to pass deviceID, not the name
    	qDebug() << "Active output " << out->activeOutput();
    	qDebug() << "Default output " << out->defaultOutput();
    }
    

    Console output

    Changing output device "VoiceMeeter Input (VB-Audio VoiceMeeter VAIO)"
    "{0.0.0.00000000}.{f3fed304-f74c-4040-96bd-71c9452cc090}"
    Matched device  "@device:cm:{E0F158E1-CB04-11D0-BD4E-00A0C911CE86}\\DirectSound:{F3FED304-F74C-4040-96BD-71C9452CC090}"
    Active output  ""
    Default output  "@device:cm:{E0F158E1-CB04-11D0-BD4E-00A0C911CE86}\\Default DirectSound Device"
    

    Alternatively I think QAudioOutput would work fine, and seems to be more geared to shoving sounds out a particular device... but... I do not know how to set the QAudioFormat based on a file format. Since I'm trying to use it more like a media player to play random sounds/music.

    Another alternative which has less Qt involved is see if there is an API to change the default device per process while its running.. that could be fun.


Log in to reply