1

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

1 回答 1

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;
        }
    }
}
于 2013-09-26T21:30:10.567 回答