QA1016: オーディオ装置の音量変更
原文 Technical Q&A QA1016: Changing the volume of audio devices http://developer.apple.com/qa/qa2006/qa1016.html
質問: オーディオ装置の音量を変更するにはどうすればよいのでしょうか。
回答: オーディオハードウェアの音量を変更するには、まずAudioDeviceを取得し、その装置が音量調節が出来るかどうかを確認しなければなりません。
AudioHardwareGetProperty
メソッドとkAudioHardwarePropertyDefaultOutputDevice
/ kAudioHardwarePropertyDefaultSystemOutputDevice
定数を使うことで、デフォルト出力装置やシステム出力装置を得ることが出来ます。
全ての装置がマスターボリューム調節や各チャンネルごとの調節に対応しているとは限りません。
Listing 1: デフォルト出力装置を取得する
UInt32 size; AudioDeviceID outputDevice; AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOuputDevice, &size, &outputDevice);
装置を取得してから、その装置が音量コントロールを持っているか、そしてその内容を書き換えられるかどうか確認します。
Listing 2: 装置の能力を取得する
bool CAAudioHardwareDevice::HasProperty( UInt32 inChannel, CAAudioHardwareDeviceSectionID inSection, AudioHardwarePropertyID inPropertyID) const { OSStatus theError = AudioDeviceGetPropertyInfo(mAudioDeviceID, inChannel, inSection, inPropertyID, NULL, NULL); return theError == 0; } bool CAAudioHardwareDevice::PropertyIsSettable( UInt32 inChannel, CAAudioHardwareDeviceSectionID inSection, AudioHardwarePropertyID inPropertyID) const { Boolean isWritable = false; OSStatus theError = AudioDeviceGetPropertyInfo(mAudioDeviceID, inChannel, inSection, inPropertyID, NULL, &isWritable); ThrowIfError(theError, CAException(theError), "CAAudioHardwareDevice::PropertyIsSettable: " "プロパティ情報取得中にエラー発生"); return isWritable != 0; } bool CAAudioHardwareDevice::HasVolumeControl( UInt32 inChannel, CAAudioHardwareDeviceSectionID inSection) const { return HasProperty(inChannel, inSection, kAudioDevicePropertyVolumeScalar); } bool CAAudioHardwareDevice::VolumeControlIsSettable( UInt32 inChannel, CAAudioHardwareDeviceSectionID inSection) const { return PropertyIsSettable(inChannel, inSection, kAudioDevicePropertyVolumeScalar); }
kAudioDevicePropertyVolumeScalar
定数を引数にAudioDeviceSetProperty
メソッドを呼ぶと、装置の音量を設定することが出来ます。
音量は0~1のFloat32値で表し、装置の音量値へとスケーリングされます。
Listing 3: マスターチャンネルの音量を設定する
AudioDeviceSetProperty(theDevice, NULL, // タイムスタンプ(必要ありません) 0, // チャンネル0がマスターチャンネル false, // 出力装置用 kAudioDevicePropertyVolumeScalar, sizeof(Float32), &theValue );
関連項目
- CAAudioHardwareDevice - /Developer/Examples/CoreAudio/PublicUtility/CAAudioHardwareDevice
- PublicUtility - /Developer/Examples/CoreAudio/PublicUtility/
改訂履歴
Date | Notes |
---|---|
2006-05-02 | 第1版 |
掲載日: 2006-05-02