MPVolumeView,避免显示“无可用卷”

发布于 2024-09-01 10:41:45 字数 556 浏览 3 评论 0原文

我有一个包含 MPVolumeView 的项目。它已设置完毕,并且可以正常工作,唯一的问题是,当我将设备静音时,会出现文本“无可用卷”,而不是 MPVolumeView。我希望在设备静音时禁用 MPVolumeView 的滑块。

VolumeView 在视图 volumeBounds 中使用该视图的边界进行初始化。

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

谢谢:)

如果您有兴趣帮助我解决其他问题,请查看此问题

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不语却知心 2024-09-08 10:41:45

使用 AudioServices 来监听硬件体积。当音量变为零时,将 MPVolumeSlider 的 alpha 设置为零,并将您自己禁用的 UISlider 放置在同一位置。使滑块外观看起来像音量滑块。

AudioSessionAddPropertyListener( kAudioSessionProperty_CurrentHardwareOutputVolume , ... );

kAudioSessionProperty_AudioRouteChanged 也可能有用。

如果您在 MPVolumeView 下浏览视图层次结构,您应该会找到一个 UISlider。如果没有,或者它被隐藏,您就知道静音字符串正在显示。

编辑:

描述了您的侦听器的函数原型。要将消息传递给类的实例,请执行类似以下操作:

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 );

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 );
一场信仰旅途 2024-09-08 10:41:45

为了简单起见,我最终采用了这个解决方案。

在 Objective-C 中:

[UILabel appearanceWhenContainedIn: [MPVolumeView class], nil].textColor = [UIColor clearColor];

在 Swift 中:

UILabel.appearanceWhenContainedWithin([MPVolumeView.self]).textColor = UIColor.clearColor()

请参阅以下 appearanceWhenContainedWithin 方法的答案:
Swift 中的appearanceWhenContainedIn

它只是隐藏“没有可用卷”文本,而不是替换为禁用的文本UISlider 不用担心 MPVolumeView 滑块和 UISlider 之间的对齐。

AVPlayer 有 volume 属性,但其文档说:

使用此属性来控制播放器相对于其他音频输出的音频音量。

AVAudioSession 具有只读 outputVolume 属性,其文档如下:

系统范围的输出音量只能由用户直接设置;要在您的应用中提供音量控制,请使用 MPVolumeView 类。

对于这些限制,简单的解决方案(或解决方法)是将文本颜色设置为透明。

I ended up with this solution for simplicity.

In Objective-C:

[UILabel appearanceWhenContainedIn: [MPVolumeView class], nil].textColor = [UIColor clearColor];

In Swift:

UILabel.appearanceWhenContainedWithin([MPVolumeView.self]).textColor = UIColor.clearColor()

Refer to the following answer for appearanceWhenContainedWithin method:
appearanceWhenContainedIn in Swift

It just hides "No Volume Available" text rather than replacing with a disabled UISlider not to worry about the alignment between the MPVolumeView slider and UISlider.

AVPlayer has volume property but its document says:

Use this property to control a player’s audio volume relative to other audio output.

AVAudioSession has read-only outputVolume property and its document says:

The system wide output volume can be set directly only by the user; to provide volume control in your app, use the MPVolumeView class.

For the limitations, the simple solution (or workaround) is just setting the text color to clear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文