我正在为ios7编写一个应用程序,它需要控制内置音乐播放器的音量或主音量级别。我尝试过使用MediaPlayer.framework,但不确定是否正确。我想知道最好的方法是什么。谢谢!
答案 0 :(得分:2)
根据Apple的说法,在iOS7设置中MPMusicPlayerController
的{{1}}已被弃用:
volume
Apple建议使用// The current volume of playing music, in the range of 0.0 to 1.0.
// This property is deprecated -- use MPVolumeView for volume control instead.
@property(nonatomic) float volume NS_DEPRECATED_IOS(3_0, 7_0);
,所以我想出了这个:
添加MPVolumeView
属性:
volumeSlider
初始化@property (nonatomic, strong) UISlider *volumeSlider;
并在视图中添加(可隐藏,无框架或由于MPVolumeView
和showsRouteButton = NO
而为空):
showsVolumeSlider = NO
查找并保存对MPVolumeView *volumeView = [MPVolumeView new];
volumeView.showsRouteButton = NO;
volumeView.showsVolumeSlider = NO;
[self.view addSubview:volumeView];
的引用:
UISlider
为__weak __typeof(self)weakSelf = self;
[[volumeView subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[UISlider class]]) {
__strong __typeof(weakSelf)strongSelf = weakSelf;
strongSelf.volumeSlider = obj;
*stop = YES;
}
}];
添加目标操作:
UIControlEventValueChanged
然后检测音量变化(即通过硬件音量控制):
[self.volumeSlider addTarget:self action:@selector(handleVolumeChanged:) forControlEvents:UIControlEventValueChanged];
以及其他方式,您可以通过以下方式设置音量:
- (void)handleVolumeChanged:(id)sender
{
NSLog(@"%s - %f", __PRETTY_FUNCTION__, self.volumeSlider.value);
}
希望这会有所帮助(并且Apple不会从MPVolumeView中删除MPVolumeSlider)。
答案 1 :(得分:1)
试试这个
[[MPMusicPlayerController applicationMusicPlayer] setVolume:(use a value between 0.0 and 1.0)]
如果您想提供音量控制,请尝试此操作
MPVolumeView *myVolumeView =
[[MPVolumeView alloc] initWithFrame: CGRectMake(70, 300, 200, 40)];
[myVolumeView setShowsVolumeSlider:YES];
[myVolumeView setShowsRouteButton:YES];
[myVolumeView sizeToFit];
[myVolumeView setBackgroundColor:[UIColor cyanColor]];
CGAffineTransform trans = CGAffineTransformMakeRotation(M_PI * 0.5);
myVolumeView.transform = trans;
[moviePlayerContlr.view addSubview: myVolumeView];
<强>交替:强>
UISlider * aSlider;
aSlider = [[UISlider alloc] initWithFrame:CGRectMake(70, 200, 100, 40)];
[aSlider setMaximumValue:0];
[aSlider setMaximumValue:1];
[aSlider addTarget:self action:@selector(aSliderAction:) forControlEvents:UIControlEventValueChanged];
[moviePlayerContlr.view addSubview:aSlider];
UIImage *stetchTrack = [[UIImage imageNamed:@"faderTrack.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
[aSlider setThumbImage: [UIImage imageNamed:@"faderKey.png"] forState:UIControlStateNormal];
[aSlider setMinimumTrackImage:stetchTrack forState:UIControlStateNormal];
[aSlider setMaximumTrackImage:stetchTrack forState:UIControlStateNormal];
CGAffineTransform tran = CGAffineTransformMakeRotation(M_PI * -0.5);
aSlider.transform = trans;
- (void)aSliderAction:(id)sender
{
NSLog(@"%d",(int)[aSlider value]);;
[[MPMusicPlayerController applicationMusicPlayer] setVolume:aSlider.value];
}
答案 2 :(得分:1)
使用iPodMusicPlayer也会影响实际的iPod音量设置。如果您想避免这种情况,请使用此
#import <MediaPlayer/MediaPlayer.h>
// ...
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
musicPlayer.volume = slider.value; //float value for volume
或试试:
MPMusicPlayerController *musicPlayer = = [MPMusicPlayerController iPodMusicPlayer];
musicPlayer.volume = slider.value; //float value for volume