views:

592

answers:

1

Hey.

I have a project with a MPVolumeView in it. It is set up, and it works, the only thing is that when I mute the device, the text "No Volume Available" comes up instead of the MPVolumeView. I would rather like the slider of the MPVolumeView to be disabled when the device is muted.

The volumeView is initialized in the view volumeBounds, with that view's bounds.

MPVolumeView *volumeView = [[[MPVolumeView alloc] initWithFrame:volumeBounds.bounds] autorelease];
[volumeBounds addSubview:volumeView]; 
[volumeView sizeToFit];

Thanks :)

If you are interested in helping me with something else, check out this question

A: 

Use AudioServices to listen for the hardware volume. When the volume goes to zero, set the alpha of the MPVolumeSlider to zero and place your own disabled UISlider in the same position. Skin your slider to look like the volume slider.

AudioSessionAddPropertyListener( kAudioSessionProperty_CurrentHardwareOutputVolume , ... );

kAudioSessionProperty_AudioRouteChanged might also be useful.

If you walk the view hierarchy under the MPVolumeView, you should find a UISlider. If not, or if it is hidden, you know the mute string is showing.

Edit:

This describes the function prototype for your listener. To pass the message to an instance of your class, do something similar to:

void MyPropertyListener ( void *inClientData, AudioSessionPropertyID inID, UInt32 inDataSize, const void *inData );

void MyPropertyListener ( void *inClientData, AudioSessionPropertyID inID, UInt32 inDataSize, const void *inData ) {
  if ( inID == kAudioSessionProperty_CurrentHardwareOutputVolume ) {
    Float32 volume = *(Float32 *)inData;
    [(MyDelegateClass *)inClientData hardwareVolumeChanged:volume];
  }
}

AudioSessionAddPropertyListener( kAudioSessionProperty_CurrentHardwareOutputVolume ,
  MyPropertyListener , aDelegateInstance );
drawnonward
I just don't seem to get it; take a look at this code: `AudioSessionAddPropertyListener(kAudioSessionProperty_CurrentHardwareOutputVolume, @selector(volumeChanged), self);`. That gets a warning that argument 2 is an incompitable pointer type.
Emil
and again, when I do this: `- (void) volumeChanged (void *inUserData, AudioSessionPropertyID inPropertyID, UInt32 inPropertyValueSize, const void *inPropertyValue) { code }` I get the error "Expected { before ( token". What's wrong? All of this AudioSession-stuff is very new to me...
Emil
You have to pass a C function pointer to AddPropertyListener, not a selector, and have the C function call your Objective-C member. Return type of volumeChanged should be `void` not `-(void)` as it is a C function.
drawnonward
Do I need to type anything more than volumeChanged where the selector is now? I will try it.
Emil
I no longer get an error at the `void`-part, but I get an error saying that `volumeView` is undeclared. I have declared it allready in the .h-file and connected it properly in IB and everything, but it still won't say that it is declared..
Emil
In .h: ` IBOutlet MPVolumeView *volumeView;` and `@property (retain) MPVolumeView *volumeView;`..m: `@synthesize volumeView;`
Emil
I did not understand what your post said, that is why I didn't accept it, if you wondered. How can I use Obective-C objects in these `C`-functions?
Emil
You could as easily ask how to use a C++ object from C. You just do it. Objective-C is C.
drawnonward
I wish I were able to select this as the accepted answer.. Stupid SO..
Emil