2012-05-02 56 views
4

我有一臺mpmovieplayercontroller可以播放在線音樂和背景音樂,在後臺播放相同的音樂時,當第一次啓動應用程序時沒有網絡訪問,通常我會顯示「沒有互聯網連接」,當我嘗試連接到互聯網並播放後顯示錯誤在iphone中檢查網絡可達性後應用程序崩潰?

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

應用程序崩潰,當我連接到互聯網後,點擊播放按鈕,任何解決方案?

+0

你應該使用一點額外的時間格式化你的代碼。它接近無法讀取。你越容易讓讀者越容易得到好的和有用的答案。 – Till

+0

有很多可能的原因。步驟1;在viewDidAppear中初始化您的播放器,而不是在initWithNib中。 – Till

+0

@Till抱歉沒有正確安排代碼,我認爲當我爲後臺活動添加代碼AVAudioSession時,問題(錯誤)即將到來,實際上我在哪裏啓動了該播放器及其屬性? –

回答

2

我認爲這是由於靜態球員。嘗試使用movieplayer

使用的屬性:

@property (nonatomic,retain) MPMoviePlayerController *player; 

在實施:

@synthesize player = _player; 

在INIT:

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

在你的代碼

[controller setContentURL:movieURL]; 
+0

我試過了,但問題仍然存在...... –

+0

「一個AVPlayerItem不能與多個AVPlayer實例關聯」 –

+0

什麼是使用它的屬性? – 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"]]; 
+0

對不起,但問題仍然存在,它顯示「一個AVPlayerItem不能與多個AVPlayer實例關聯」,只有在第一次啓動應用程序時纔會出現此錯誤,而沒有網絡。 –

0

不知道這是正確的,但如果您已檢查鏈接

MPMoviewPlayerController Class Reference

其中包括來自蘋果的代碼:

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第一,然後將其添加到您的視圖作爲子視圖..

我不知道,但希望這有助於..

8

我有同樣的問題。我的解決方案是刪除指令

player.movieSourceType = MPMovieSourceTypeStreaming; 
+3

謝謝!這就是幫助我的 - 使用簡單的init(initWithContentURL的intead)初始化,然後設置movieSourceType,然後設置contentURL。 –

+0

謝謝@PavelAlexeev,那是我需要的。 – htafoya