iPhone中检查网络可达性后应用程序崩溃?

4

我有一个MPMoviePlayerController用于播放在线音乐和AVAudiosession用于在后台播放相同的音乐,在第一次应用程序启动时没有网络访问,通常显示“无互联网连接”,当我尝试连接到互联网并播放它时显示错误。

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An AVPlayerItem cannot be associated with more than one instance of AVPlayer'

我的代码在这里

static MPMoviePlayerController *player=nil;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    {
        // Custom initialization

        [MainViewController  stopStreaming];

        player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"http://159.253.145.180:7104"]];
        player.movieSourceType = MPMovieSourceTypeStreaming;
        player.view.hidden = YES;
        [self.view addSubview:player.view];
        [player prepareToPlay];
        [player play];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSError *setCategoryErr = nil;
    NSError *activationErr  = nil;
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryErr];
    [[AVAudioSession sharedInstance] setActive: YES error: &activationErr];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;
    newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];

    Reachability* reachabile = [Reachability reachabilityWithHostName:@"www.apple.com"];
    NetworkStatus remoteHostStatus = [reachabile currentReachabilityStatus];

    if(remoteHostStatus == NotReachable) 
    {
        NSLog(@"not reachable");
        UIAlertView *notReachableAlert=[[UIAlertView alloc]initWithTitle:@"NO INTERNET CONNECTION" message:@"This Application Need Internet To Run" delegate:self cancelButtonTitle:@"Okay Buddy" otherButtonTitles: nil];
        notReachableAlert.delegate=self;
        [notReachableAlert show];
        [notReachableAlert release];
    }

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackStateDidChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:player];

    MPVolumeView *volumeView = [[[MPVolumeView alloc] initWithFrame:CGRectMake(22, 320, 200, 15)] autorelease];
    volumeView.center = CGPointMake(152,372);
    [volumeView sizeToFit];
    [self.view addSubview:volumeView];    // Do any additional setup after loading the view from its nib.
}

连接到互联网后,当我点击播放按钮时应用程序崩溃了,有什么解决方法吗?


你应该花一点额外的时间来格式化你的代码。它几乎无法阅读。你让读者更容易理解,就越有可能得到好的和有帮助的答案。 - Till
有很多可能的原因。第一步:在viewDidAppear中初始化您的播放器,而不是在initWithNib中。 - Till
@Till 很抱歉没有正确地排列代码,我认为问题(错误)是在我添加了AVAudioSession的后台活动代码时出现的,实际上我在哪里初始化了播放器及其属性? - Neeraj Neeru
在viewDidAppear中初始化播放器(如前所述),在初始化播放器之前初始化会话。基本上只需在viewDidAppear中执行您当前执行的所有操作。 - Till
4个回答

8
我也遇到了相同的问题。对于我有效的解决方案是删除这条指令。
player.movieSourceType = MPMovieSourceTypeStreaming;

3
谢谢!那就是帮助我的方法 - 使用简单的初始化(而不是initWithContentURL),然后设置movieSourceType,最后设置contentURL。 - Pavel Alexeev

2

我认为这是由于静态播放器引起的。尝试使用movieplayer的属性。

用法:

@property (nonatomic,retain) MPMoviePlayerController *player;

在实现中:

@synthesize player = _player;

在 init 中:
MPMoviePlayerController *plyr = [MPMoviePlayerController alloc] init];
self.player = plyr;
[plyr relaease];

在您的代码中
[controller setContentURL:movieURL];

“AVPlayerItem不能与多个AVPlayer实例相关联。” - Neeraj Neeru
你在那里使用了什么属性u? - Saad

1

我认为这可能是由于已经存在一个播放器。

尝试添加类似以下内容的东西:

if (player != nil) {
    [player release];
    player = nil; //i prefer to do both, just to be sure.
}

将其放在这个

标签之后。
[MainViewController  stopStreaming];  

在此之前,

player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"http://159.253.145.180:7104"]];

抱歉,问题仍然存在,它显示“AVPlayerItem不能与多个AVPlayer实例关联”,这个错误仅发生在应用程序第一次在没有网络的情况下启动时。 - Neeraj Neeru

0

我不确定这是否正确,但如果您已经检查了链接

MPMoviewPlayerController类参考

其中包括来自苹果的代码:

MPMoviePlayerController *player =
        [[MPMoviePlayerController alloc] initWithContentURL: myURL];
[player prepareToPlay];
[player.view setFrame: myView.bounds];  // player's frame must match parent's
[myView addSubview: player.view];
// ...
[player play];

也许你需要先调用prepareToPlay方法,然后将其作为子视图添加到你的视图中。

我不确定,但希望这可以帮到你。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接