Adding MPVolumeview programmatically in app

2019-07-17 04:51发布

问题:

Trying to add MPVolumeView programmtically in app by using the following code

 MPVolumeView *_volumeView = [ [MPVolumeView alloc] init];
[_volumeView setShowsVolumeSlider:YES];
[_volumeView setShowsRouteButton:YES];
[_volumeView sizeToFit];
[view addSubview:_volumeView];

but getting so many Semantic issue that use of undeclared identifier MPVolumeView and Invalid operands to binary expression

@property (nonatomic, strong) MPVolumeView *volumeView;

Getting message in red for the above statement that unknown type MPVolumeView and plus property with retain or strong must be of object type.

 @synthesize volumeView = _volumeView;

Is this the right way to add MPVolumeView programmatically in app.

Thanks for help.

回答1:

MPVolumeView is part of the MediaPlayer framework. Did you include this in your application and did you #import <MediaPlayer/MediaPlayer.h> in your .m or .h file?

Also, if you declared a property, you should create a new local variable _volumeView. Replace the first line with this:

_volumeView = [ [MPVolumeView alloc] init];