将MPVolumeView添加到底部工具栏

问题描述:

我正在尝试将MPVolumeView的实例添加到导航控制器的底部栏中.(工具栏的方框已打勾) 我的代码没有出现任何错误,但是当我在设备上运行项目时,音量滑块没有显示.在此先感谢您的任何建议,这是我的代码:

I am trying to add an instance of MPVolumeView to the bottom bar of my navigation controller.(the box for toolbar is ticked) I do not get any errors with my code, but when I run the project on my device the volume slider is not showing. Thanks in advance for any suggestions, here is my code:

@interface ViewController ()
{
AVPlayer *vPlayer;
AVPlayerItem *playerItem;
UISegmentedControl *segm;
UIToolbar *toolbar;
}
@end

@implementation ViewController
@synthesize myViewVolume;
@synthesize nowPlaying;

- (void)viewDidLoad
{
[super viewDidLoad];
playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"http://vibesradio.org:8000  /listen.pls"]];
vPlayer = [[AVPlayer alloc] initWithPlayerItem:playerItem];
[playerItem addObserver:self forKeyPath:@"timedMetadata" options:NSKeyValueObservingOptionNew context:NULL];


//self.myViewVolume = [[MPVolumeView alloc] initWithFrame:CGRectMake(20, 330, 280, 50)];


self.myViewVolume = [[MPVolumeView alloc] initWithFrame: toolbar.bounds];
[self.myViewVolume sizeToFit];
[self.view addSubview:toolbar];
[toolbar addSubview:self.myViewVolume];

}

您不能将子视图添加到工具栏.您必须添加一个UIBarButtonItem,其视图是您要显示的视图.

You cannot add subviews to a toolbar. You must add a UIBarButtonItem whose view is the view you want to show.

UIBarButtonItem* b = [[UIBarButtonItem alloc] initWithCustomView: myVolumeView];
myToolbar.items = @[b];

这与MPVolumeView没有关系.您想要添加到工具栏或导航栏的任何任意视图都是如此.

This has nothing to do with MPVolumeView. It's true of any arbitrary view you'd like to add to a toolbar or nav bar.

以下是在我的设备上适用于导航界面中的视图控制器的实际代码:

Here's actual code that works on my device for a view controller in a navigation interface:

MPVolumeView* vv = [[MPVolumeView alloc] initWithFrame: CGRectMake(0, 0, 150, 40)];
UIBarButtonItem* b = [[UIBarButtonItem alloc] initWithCustomView: vv];
self.toolbarItems = @[b];
self.navigationController.toolbarHidden = NO;

也请注意,您必须在设备上进行测试; Simulator中没有用于体积视图的界面. :(

Note too that you must test on a device; there's no interface for the volume view in the Simulator. :(