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

标签 iphone mpmovieplayercontroller avaudiosession

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

*** 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.
}

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

最佳答案

我也遇到了同样的问题。对我有用的解决方案是删除指令

player.movieSourceType = MPMovieSourceTypeStreaming;

关于iphone - 检查 iPhone 中的网络可达性后应用程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10413433/

相关文章:

ios - 'MPMoviePlayerController' 已弃用 : first deprecated in iOS 9. 0

ios - 在应用程序中更改 AVAudioSession 模式

iphone - iOS5锁屏后如何继续播放视频音轨?

iphone - beginAnimations方法中的animationID列表

iphone - 将字符串转换为 NSDate 对象

ios - iPad MoviePlayer 在设备重启之前拒绝播放视频

iphone - 在没有控件的情况下在 iPhone 中打开视频

iphone - 如何在一个viewClass中通知另一个View类

ios - 使用 AVAudioSession 设置麦克风增益时出现不稳定行为

ios - 在应用程序内打开 native iOS 音频输出目标选择器