我正在学习iOS开发,我有一个使用MPMoviePlayerController的应用程序。我正在使用的流播放完美,但是如果我按下暂停按钮,则流程会在我按下暂停的同一时刻再次开始播放。当我按下暂停时,我正在寻找它停止流。当我重新启动流时,我希望它再次加载流并播放“直播”#39;就像我第一次打开应用程序时那样。
这是我到目前为止使用过的代码(我想它很简单,但我刚刚开始)......
@interface FirstViewController()
@end
@implementation FirstViewController
@synthesize toolbar, play, pause, player;
- (void)viewDidLoad
{
[super viewDidLoad];
self.pause = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPause target:self action:@selector(playPressed:)];
[self.pause setStyle:UIBarButtonItemStyleBordered];
NSString *streamAddress =@"http://www.bbc.co.uk/radio/listen/live/r4_aaclca.pls";
NSURL *address =[NSURL URLWithString:streamAddress];
MPMoviePlayerController *stream = [[MPMoviePlayerController alloc]initWithContentURL:address];//instance method to create and intialise the stream
stream.movieSourceType = MPMovieSourceTypeStreaming;
stream.view.hidden = YES;
//[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
//[[AVAudioSession sharedInstance]setActive:YES error:nil];
//[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
self.player = stream;
[self.view addSubview:self.player.view];
}
如果有人能帮助我,那将是非常棒的。
谢谢!
修改
-(IBAction)playPressed:(id)sender{
MPMoviePlaybackState playbackState = [self.player playbackState];
NSMutableArray *items = [NSMutableArray arrayWithArray:[self.toolbar items]];
[self.toolbar setBackgroundImage:[UIImage new]
forToolbarPosition:UIBarPositionAny
barMetrics:UIBarMetricsDefault];
[self.toolbar setShadowImage:[UIImage new]
forToolbarPosition:UIToolbarPositionAny];
if (playbackState == MPMoviePlaybackStateStopped || playbackState == MPMoviePlaybackStatePaused){
NSMutableArray *toolbarButtons = [self.toolbarItems mutableCopy];
[toolbarButtons removeObject:self.pause];
[self setToolbarItems:toolbarButtons animated:YES];
[self.player prepareToPlay];
if (![ toolbarButtons containsObject:self.pause]) {
[toolbarButtons addObject:self.pause];
[self setToolbarItems:toolbarButtons animated:YES];
}
//this means that the player is not currently playing and that the toolbar is displaying the play button
[self.player play];
//NSMutableArray *toolbarButtons = [NSMutableArray arrayWithArray:[toolbar items]];
//[toolbarButtons replaceObjectAtIndex:0 withObject:pause];
// [toolbarButtons removeObject:play];
//[items replaceObjectAtIndex:2 withObject:self.pause];
}
else if (playbackState == MPMoviePlaybackStatePlaying){
NSMutableArray *toolbarButtons = [self.toolbarItems mutableCopy];
[toolbarButtons removeObject:self.play];
[self setToolbarItems:toolbarButtons animated:YES];
if (![ toolbarButtons containsObject:self.play]) {
[toolbarButtons addObject:self.play];
[self setToolbarItems:toolbarButtons animated:YES];
}
[self.player pause];
}