Reputation: 2510
How can i control my app's output volume.
I've got an app that uses https://github.com/mattgallagher/AudioStreamer to stream mp3 files from the internet. The AudioStreamer
class does not have a way to change output volume and I don't want to change system volume.
Many apps do this:
Edit: If you're hear about AudioStreamer, I've since switched to Apple's AVPlayer, which i've found far simpler and superior. Easy volume adjustment too!
Upvotes: 0
Views: 673
Reputation: 2510
AudioStreamer
and I'm guessing most OSX media players use the AudioToolbox framework. AudioToolbox uses a programming interface called AudioQueue
to playback media files. Here is the way to adjust the volume using AudioQueue
.
AudioQueueSetParameter(audioQueue, kAudioQueueParam_Volume, 0.5);
audioQueue
is an AudioQueRefkAudioQueueParam_Volume
tells AudioQueueSetParameter()
to change the Volume Parameter0.5
is the volume from 0.0 - 1.0More details on AudioQueue: https://developer.apple.com/library/mac/#documentation/MusicAudio/Reference/AudioQueueReference/Reference/reference.html
Upvotes: 2
Reputation: 35131
You can use AVAudioPlayer
, it has an instance method setVolume:
to set the output volume:
AVAudioPlayer * audioPlayer = ...
float volume = aVolumeValue / 100.f; // aVolumeValue can be 0~100
[audioPlayer setVolume:volume];
[audioPlayer play];
By the way, you can store the aVolumeValue
into NSUserDefaults
, and control it by UISlider
object.
Upvotes: 0