MPMoviePlayer锁屏播放/暂停音频

时间:2013-08-22 17:52:53

标签: iphone ios xcode mpmovieplayercontroller mpmovieplayer

我正在通过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];
}

1 个答案:

答案 0 :(得分:2)

您没有在代码中的任何位置显示您实际接收的远程控制事件。类似的东西:

- (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;
        }
    }
}