应用在iphone检查网络的可达性后崩溃?(app crashes after checking n

2019-09-16 23:12发布

我有一个的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.
}

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

Answer 1:

我认为,这是由于静态的球员。 尝试使用movieplayer的属性

采用:

@property (nonatomic,retain) MPMoviePlayerController *player;

在实施:

@synthesize player = _player;

在INIT:

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

在您的代码

[controller setContentURL:movieURL];


Answer 2:

我有同样的问题。 为我工作的解决办法是删除指令

player.movieSourceType = MPMovieSourceTypeStreaming;


Answer 3:

我认为这可能给玩家已经存在到期。

尝试添加这样的事情:

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"]];


Answer 4:

我不知道这是否是正确的,但如果你已经检查了链接

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第一,然后将其添加到您的视图作为子视图..

我不知道,但希望这有助于..



文章来源: app crashes after checking network reachability in iphone?