How to change audio output device

65 ビュー (過去 30 日間)
Jiajun Yang
Jiajun Yang 2018 年 9 月 13 日
回答済み: Auralius Manurung 2018 年 10 月 5 日
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.

カテゴリ

Help Center および File ExchangeAudio and Video Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by