我有一臺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.
}
應用程序崩潰,當我連接到互聯網後,點擊播放按鈕,任何解決方案?
你應該使用一點額外的時間格式化你的代碼。它接近無法讀取。你越容易讓讀者越容易得到好的和有用的答案。 – Till
有很多可能的原因。步驟1;在viewDidAppear中初始化您的播放器,而不是在initWithNib中。 – Till
@Till抱歉沒有正確安排代碼,我認爲當我爲後臺活動添加代碼AVAudioSession時,問題(錯誤)即將到來,實際上我在哪裏啓動了該播放器及其屬性? –