iOS:更改设备音量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4859402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS: Change Device Volume
提问by Tristan Seifert
Is there a way to change the volume of the device? I've seen several apps do it.
有没有办法改变设备的音量?我见过几个应用程序这样做。
I have a desktop version of the iOS app and the device will be able to be controlled to some extent over the network. One of the things I want to allow the user to do is change the device volume and then play a sound. This can help if you loose your iPhone in a crack in your couch again, but can't find it.
我有一个桌面版本的 iOS 应用程序,并且该设备将能够在一定程度上通过网络进行控制。我想让用户做的一件事是改变设备音量,然后播放声音。如果您的 iPhone 再次在沙发裂缝中丢失,但找不到它,这会有所帮助。
Is there any way that you can do this without Apple getting angry?
有什么方法可以做到这一点而不让 Apple 生气?
回答by ceriseche
Using iPodMusicPlayer would affect the actual iPod volume setting as well. If you want to avoid that, use this:
使用 iPodMusicPlayer 也会影响实际的 iPod 音量设置。如果您想避免这种情况,请使用以下命令:
#import <MediaPlayer/MediaPlayer.h>
// ...
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
musicPlayer.volume = 1.0f;
As the user holexcorrectly mentioned the property volume
in MPMusicPlayerController
is deprecated in iOS 7.
回答by Andrii Forkaliuk
You can use a little trick:
你可以使用一个小技巧:
MPMusicPlayerController* musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
musicPlayer.volume = 1; // device volume will be changed to maximum value
回答by Waqas Raja
You cannot change device volume programatically, however MPVolumeView(volume slider) is there to change device volume but only through user interaction.
您不能以编程方式更改设备音量,但是MPVolumeView(音量滑块)可以更改设备音量,但只能通过用户交互。
MPVolumeView is a control in toolbox, you need to add MediaPlayer.frameworkin your project then MPVolumeView will be displayed in toolbox in interface builder.
MPVolumeView是在工具箱中的控件,您需要添加MediaPlayer.framework在您的项目,然后将MPVolumeView在Interface Builder显示在工具箱中。
Edit 1:MPVolumeView uses the device volume which is also used for ringing volume. AVAudioPlayeris there if you want application level volume. In this case you can use volumeproperty to set your application volume (not device volume) programatically. However, you can use UISlidercontrol to get volume input from user and set to your AVAudioPlayer
编辑 1:MPVolumeView 使用也用于响铃音量的设备音量。如果您想要应用程序级别的音量,则有AVAudioPlayer。在这种情况下,您可以使用volume属性以编程方式设置应用程序音量(而不是设备音量)。但是,您可以使用UISlider控件从用户获取音量输入并设置为您的 AVAudioPlayer