Skip to content

macOS speaker data doesnt flow #986

Open
@Jerolivine

Description

@Jerolivine

I am using the code block to see if i am able to retrieve speaker data. It works fine with windows but the same code doesnt work for macOS.

Is there some arrangement can be done to make it work for macOS such as accessibility or any code changes that i share below?

use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use cpal::{Host, Stream, StreamConfig, SupportedStreamConfig};
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
        let host = cpal::default_host();

     let device = host
         .default_output_device()
         .ok_or_else(|| anyhow::anyhow!("no output device available"))?;

     let mut supported_configs_range = device
         .supported_output_configs()
         .map_err(|err| anyhow::anyhow!("error while querying configs: {}", err))?;

     let supported_config: SupportedStreamConfig = supported_configs_range
         .next()
         .ok_or_else(|| anyhow::anyhow!("no supported output config"))?
         .with_max_sample_rate();
    
     let err_fn = move |err| {
         eprintln!("an error occurred on stream: {}", err);
     };

     println!("{}",supported_config.sample_format());

     let stream = match supported_config.sample_format() {
                cpal::SampleFormat::I8
         | cpal::SampleFormat::I16
         | cpal::SampleFormat::I32
         | cpal::SampleFormat::I64
         | cpal::SampleFormat::U8
         | cpal::SampleFormat::U16
         | cpal::SampleFormat::U32
         | cpal::SampleFormat::U64
                 | cpal::SampleFormat::F32 => device.build_input_stream(
                     &supported_config.into(),
                     move |data:&[f32], _: &_| {
                         println!("data flows.")
                     },
                     err_fn,
                     None
                 )?,
     _ => {panic!("sample format");}
     };

    
    // Start the stream
    stream.play()?;
    
    // Keep the application running to keep receiving data
    std::thread::sleep(std::time::Duration::from_secs(10));
    
    Ok(())
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions