Xcode 在iOS 5中通过HTTP播放远程MP3文件
我试图在我的Xcode项目中播放一个远程MP3文件,但是下面使用MPMoviePlayerController的实现对我不起作用,并且引发了一个异常 AVPlayerItem在键值观察家仍在时被解除分配 向它登记 My.h文件Xcode 在iOS 5中通过HTTP播放远程MP3文件,xcode,cocoa-touch,Xcode,Cocoa Touch,我试图在我的Xcode项目中播放一个远程MP3文件,但是下面使用MPMoviePlayerController的实现对我不起作用,并且引发了一个异常 AVPlayerItem在键值观察家仍在时被解除分配 向它登记 My.h文件 #import <MediaPlayer/MediaPlayer.h> @property (nonatomic, strong) MPMoviePlayerController *moviePlayer; @synthesize moviePlayer
#import <MediaPlayer/MediaPlayer.h>
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
@synthesize moviePlayer = _moviePlayer;
- (void)playEnglish
{
NSURL *url = [NSURL URLWithString:_audioUrlEnglish];
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer];
_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player
respondsToSelector:@selector(setFullscreen:animated:)])
{
[player.view removeFromSuperview];
}
}
您已经为播放器指定了一个合成属性,但随后将直接分配给ivar 而不是:
__moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
你应该:
MPMoviePlayer *aPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self setMoviePlayer:aPlayer];
这将确保正确保留对象(如果使用自动引用计数)。如果没有这一点,你的球员似乎不会被保留,这将解释你的错误
此外,您正在代码中的其他几个位置分配/访问实例变量。Cocoa中的最佳实践通常完全避免直接接触IVAR(也有一些例外,但使用ARC时则更少,我在这里没有看到任何值得直接分配的示例)