在检查iphone中的网络可达性后app崩溃了?

Nee*_*eru 4 iphone mpmovieplayercontroller avaudiosession

我有一个mpmovieplayer控制器播放在线音乐和avaudiosession在背景播放相同的音乐,当第一次应用程序启动没有网络访问,通常我显示"没有互联网连接",当我尝试连接到互联网并播放后显示错误

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An AVPlayerItem cannot be associated with more than one instance of AVPlayer'
Run Code Online (Sandbox Code Playgroud)

我的代码在这里

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.
}
Run Code Online (Sandbox Code Playgroud)

当我连接到互联网后单击播放按钮,应用程序崩溃,任何解决方案?

nic*_*o86 8

我有同样的问题.对我有用的解决方案是删除指令

player.movieSourceType = MPMovieSourceTypeStreaming;
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!这就是帮助我 - 用简单的init(initWithContentURL的内部)初始化,然后设置movieSourceType然后设置contentURL. (3认同)