苹果是否提供任何媒体传输控件以在我的应用程序中使用
Does apple provide any media transport controls to use in my app
我正在编写一个播放配乐的应用程序。我正在使用 AVAudioPlayer
,我实现了一个播放列表和处理它的所有逻辑以及传输控制(播放、停止、倒回、前进和提示)。现在我开始连接我自己创建的界面,但我想知道 cocoa touch 是否有我可以使用的界面。
传输控件称为 Remote Control Events,您可以像这样实现它们:
// Init
AVAudioSession *session = [AVAudioSession sharedInstance];
// TODO: handle errors
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setActive:YES error:nil];
__weak typeof(self) weakSelf = self;
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
[commandCenter.playCommand addTargetWithHandler:^(MPRemoteCommandEvent *event) {
[weakSelf.player play];
return MPRemoteCommandHandlerStatusSuccess;
}];
[commandCenter.pauseCommand addTargetWithHandler:^(MPRemoteCommandEvent *event) {
[weakSelf.player pause];
return MPRemoteCommandHandlerStatusSuccess;
}];
NSString *url = @"file url here";
// TODO: handle errors
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:url] error:nil];
[self.player play];
这将为您提供锁定屏幕和 "swipe up" 屏幕上的控件。您可以通过 MPNowPlayingInfoCenter.
添加更多功能(如搜索等)和元数据
我正在编写一个播放配乐的应用程序。我正在使用 AVAudioPlayer
,我实现了一个播放列表和处理它的所有逻辑以及传输控制(播放、停止、倒回、前进和提示)。现在我开始连接我自己创建的界面,但我想知道 cocoa touch 是否有我可以使用的界面。
传输控件称为 Remote Control Events,您可以像这样实现它们:
// Init
AVAudioSession *session = [AVAudioSession sharedInstance];
// TODO: handle errors
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setActive:YES error:nil];
__weak typeof(self) weakSelf = self;
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
[commandCenter.playCommand addTargetWithHandler:^(MPRemoteCommandEvent *event) {
[weakSelf.player play];
return MPRemoteCommandHandlerStatusSuccess;
}];
[commandCenter.pauseCommand addTargetWithHandler:^(MPRemoteCommandEvent *event) {
[weakSelf.player pause];
return MPRemoteCommandHandlerStatusSuccess;
}];
NSString *url = @"file url here";
// TODO: handle errors
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:url] error:nil];
[self.player play];
这将为您提供锁定屏幕和 "swipe up" 屏幕上的控件。您可以通过 MPNowPlayingInfoCenter.
添加更多功能(如搜索等)和元数据