Rust Rodio 获取输出设备列表
Rust Rodio get a list of OutputDevices
我是 Rust 的新手,我一直在玩 Rodio audio library。
我可以像这样在默认音频输出设备上播放音频文件:
use std::fs::File;
use std::io::BufReader;
use rodio::{OutputStream, Sink};
fn main() {
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
let sink = Sink::try_new(&stream_handle).unwrap();
let file = File::open("m.mp3").unwrap();
let source = rodio::Decoder::new(BufReader::new(file)).unwrap();
sink.append(source);
loop {}
}
我看到 Rodio 提供了一种为流设置音频输出设备的方法 try_from_device(&Device) 但我不知道如何获取可用音频输出设备的列表并提供任意一个这个函数。
----更新----
根据E_net4的回答,我做了两个简单的函数来列出主机设备并为特定设备创建一个OutputStream
,然后在我需要播放音频文件的任何地方使用它像这样的设备:
use std::fs::File;
use std::io::BufReader;
use rodio::*;
use rodio::cpal::traits::{HostTrait,DeviceTrait};
fn listHostDevices(){
let host = cpal::default_host();
let devices = host.output_devices().unwrap();
for device in devices{
let dev:rodio::Device = device.into();
let devName:String=dev.name().unwrap();
println!(" # Device : {}", devName);
}
}
fn getOutputStream(device_name:&str) -> (OutputStream,OutputStreamHandle) {
let host = cpal::default_host();
let devices = host.output_devices().unwrap();
let ( mut _stream, mut stream_handle) = OutputStream::try_default().unwrap();
for device in devices{
let dev:rodio::Device = device.into();
let devName:String=dev.name().unwrap();
if devName==device_name {
println!("Device found: {}", devName);
( _stream, stream_handle) = OutputStream::try_from_device(&dev).unwrap();
}
}
return (_stream,stream_handle);
}
然后我使用这样的函数:
fn main() {
listHostDevices();
let (_stream, stream_handle) = getOutputStream("Speakers (Realtek(R) Audio)");
let sink = Sink::try_new(&stream_handle).unwrap();
let file = File::open("m.mp3").unwrap();
let source = rodio::Decoder::new(BufReader::new(file)).unwrap();
sink.append(source);
loop {}
}
rodio
使用 cpal
作为基础音频库。这就是 host 和 device 概念的来源。使用 rodio
中的 re-exported cpal
模块获取系统主机并获取输出设备列表。
use rodio::cpal;
let host = cpal::default_host();
let devices = host.output_devices()?;
for device in devices {
// use device
}
获得的设备值将执行DeviceTrait
, but rodio
works with the dynamically polymorphic type rodio::Device
instead. Fortunately, we can easily convert what we have via From
or Into
。
let device: rodio::Device = device.into();
// ...
stream.try_from_device(&device)?;
我是 Rust 的新手,我一直在玩 Rodio audio library。 我可以像这样在默认音频输出设备上播放音频文件:
use std::fs::File;
use std::io::BufReader;
use rodio::{OutputStream, Sink};
fn main() {
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
let sink = Sink::try_new(&stream_handle).unwrap();
let file = File::open("m.mp3").unwrap();
let source = rodio::Decoder::new(BufReader::new(file)).unwrap();
sink.append(source);
loop {}
}
我看到 Rodio 提供了一种为流设置音频输出设备的方法 try_from_device(&Device) 但我不知道如何获取可用音频输出设备的列表并提供任意一个这个函数。
----更新----
根据E_net4的回答,我做了两个简单的函数来列出主机设备并为特定设备创建一个OutputStream
,然后在我需要播放音频文件的任何地方使用它像这样的设备:
use std::fs::File;
use std::io::BufReader;
use rodio::*;
use rodio::cpal::traits::{HostTrait,DeviceTrait};
fn listHostDevices(){
let host = cpal::default_host();
let devices = host.output_devices().unwrap();
for device in devices{
let dev:rodio::Device = device.into();
let devName:String=dev.name().unwrap();
println!(" # Device : {}", devName);
}
}
fn getOutputStream(device_name:&str) -> (OutputStream,OutputStreamHandle) {
let host = cpal::default_host();
let devices = host.output_devices().unwrap();
let ( mut _stream, mut stream_handle) = OutputStream::try_default().unwrap();
for device in devices{
let dev:rodio::Device = device.into();
let devName:String=dev.name().unwrap();
if devName==device_name {
println!("Device found: {}", devName);
( _stream, stream_handle) = OutputStream::try_from_device(&dev).unwrap();
}
}
return (_stream,stream_handle);
}
然后我使用这样的函数:
fn main() {
listHostDevices();
let (_stream, stream_handle) = getOutputStream("Speakers (Realtek(R) Audio)");
let sink = Sink::try_new(&stream_handle).unwrap();
let file = File::open("m.mp3").unwrap();
let source = rodio::Decoder::new(BufReader::new(file)).unwrap();
sink.append(source);
loop {}
}
rodio
使用 cpal
作为基础音频库。这就是 host 和 device 概念的来源。使用 rodio
中的 re-exported cpal
模块获取系统主机并获取输出设备列表。
use rodio::cpal;
let host = cpal::default_host();
let devices = host.output_devices()?;
for device in devices {
// use device
}
获得的设备值将执行DeviceTrait
, but rodio
works with the dynamically polymorphic type rodio::Device
instead. Fortunately, we can easily convert what we have via From
or Into
。
let device: rodio::Device = device.into();
// ...
stream.try_from_device(&device)?;