How to change audio output device

73 次查看(过去 30 天)
I want to be able to specify which audio output the sound to be played. Using audioDeviceWriter I am able to get the device info, but I don't know how I can change the output to a specific device. I try .Device(index) but that doesn't work. Please help.
fs = 44100;
T = 1/fs;
t = [0:T:2];
f1 = 250;
omega1 = 2*pi*f1;
phi = 2*pi *0.75;
x1 = cos(omega1*t +phi);
deviceWriter = audioDeviceWriter(fs);
devices = getAudioDevices(deviceWriter);
% I wish to change the audio device here between built-in and my sound card.
deviceWriter.Device(3)
sound(0.9*x1, fs)

回答(1 个)

Auralius Manurung
Auralius Manurung 2018-10-5
deviceWriter is more complex to use as it plays only one frame. It means you have to use it in a loop. You also don't need the code in the last line. Rather then using deviceWriter, why not using audioPlayer?
fs = 44100;
T = 1/fs;
t = [0:T:1];
f1 = 250;
omega1 = 2*pi*f1;
phi = 2*pi *0.75;
x1 = cos(omega1*t +phi)';
info = audiodevinfo
player = audioplayer(0.9*x1, fs, 16, 3);
play(player);
You can explore the info.output variable to see the list of detected hardware.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by