Search code examples
macoscocoaavfoundationavaudioplayer

Change the output device for AVAudioPlayer on OSX


I need to play audio from AVAudioPlayer to a particular audio device such as a USB headset. By default the audio will get played out to the system default device setting. With QTKit, I was able to use SetMovieAudioContext() to do such a task but do not see equivalent in AVFoundation. How is it possible to change this on OSX?


Solution

  • Use NSSound instead:

    Get list of audio devices with CoreAudio functions. Then get the devices' unique identifiers:

    //...
    address.mSelector = kAudioDevicePropertyDeviceUID;
    CFStringRef uid = NULL;
    UInt32 size = sizeof(uid);
    AudioObjectGetPropertyData(deviceId, &address, 0, NULL, &size, &uid);
    //...
    

    Initialize sound item, set playbackDeviceIdentifier property value.