我正在通过 MPMoviePlayer 运行流式音频。我可以将远程事件发送到锁定屏幕和停靠,以便我能够看到标题和作者,并且音频在后台模式下播放,但我无法让锁定屏幕/停靠播放按钮启动和停止音频。我完全错过了什么吗?
这是我的代码:
#import "teachingsDetailViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "RSSItem.h"
#import "RSSLoader.h"
@implementation teachingsDetailViewController
@synthesize moviePlayerController;
-(void)viewDidLoad:(BOOL)animated
{
//Make sure the system follows our playback status
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
NSLog(@"The System ran this");
//Load the audio into memory
[moviePlayerController prepareToPlay];
[super viewDidLoad];
}
-(void)viewDidAppear:(BOOL)animated {
[super loadView];
RSSItem* item = (RSSItem*)self.detailItem;
self.title = item.title;
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:item.link];
NSLog(@"The url is %@", item.link);
[self.view addSubview:moviePlayerController.view];
moviePlayerController.movieSourceType = MPMovieSourceTypeUnknown;
moviePlayerController.fullscreen = YES;
if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
NSLog(@"The System ran this");
[self becomeFirstResponder];
NSLog(@"Responds!");
}
[moviePlayerController play];
//here
Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
if (playingInfoCenter) {
NSError *error= nil;
if ([[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&error]) {
NSLog(@"Error setting audio session: %@", error);
}
NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] init];
[songInfo setObject:self.title forKey:MPMediaItemPropertyTitle];
[songInfo setObject:@"AU One Place" forKey:MPMediaItemPropertyArtist];
[songInfo setObject:@"Teachings" forKey:MPMediaItemPropertyAlbumTitle];
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
}
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
//End recieving events
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
NSLog(@"Stopped receiving remote control events");
[self resignFirstResponder];
}
-(void)viewDidDisappear:(BOOL)animated
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidUnload {
[super viewDidUnload];
}