[英]MPMoviePlayer Lock Screen Play/Pause for Audio
我正在通過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];
}
您沒有在代碼中的任何地方顯示您實際上正在接收遠程控制事件。 就像是:
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent
{
if ( receivedEvent.type == UIEventTypeRemoteControl ) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlPlay:
case UIEventSubtypeRemoteControlPause:
case UIEventSubtypeRemoteControlStop:
case UIEventSubtypeRemoteControlTogglePlayPause:
if ( self.player.isPlaying ) {
[self.player pause];
[[MPMusicPlayerController applicationMusicPlayer] pause];
} else {
[self.player play];
[[MPMusicPlayerController applicationMusicPlayer] play];
}
break;
case UIEventSubtypeRemoteControlBeginSeekingBackward:
case UIEventSubtypeRemoteControlBeginSeekingForward:
case UIEventSubtypeRemoteControlEndSeekingBackward:
case UIEventSubtypeRemoteControlEndSeekingForward:
case UIEventSubtypeRemoteControlPreviousTrack:
case UIEventSubtypeRemoteControlNextTrack:
self.player.currentTime = 0;
break;
default:
break;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.