[英]app crashes after checking network reachability in iphone?
i have a mpmovieplayercontroller to play online music and avaudiosession to play the same music at background, when the first time app launches without network access, normally i shows "no internet connection" , when i tried after connecting to internet and playing it shows the error 我有一个mpmovieplayer控制器播放在线音乐和avaudiosession在背景播放相同的音乐,当第一次应用程序启动没有网络访问,通常我显示“没有互联网连接”,当我尝试连接到互联网并播放后显示错误
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An AVPlayerItem cannot be associated with more than one instance of AVPlayer'
my code is here 我的代码在这里
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.
}
app crashes when i click play button after connecting to internet, any solution? 当我连接到互联网后单击播放按钮,应用程序崩溃,任何解决方案?
I had the same problem. 我有同样的问题。 The solution that worked for me was remove the instruction
对我有用的解决方案是删除指令
player.movieSourceType = MPMovieSourceTypeStreaming;
I think this is due to static player. 我认为这是由静态播放器引起的。 Try using a property of movieplayer
尝试使用电影播放器的属性
Use: 采用:
@property (nonatomic,retain) MPMoviePlayerController *player;
In implementation: 在实施中:
@synthesize player = _player;
In init: 在init中:
MPMoviePlayerController *plyr = [MPMoviePlayerController alloc] init];
self.player = plyr;
[plyr relaease];
In your code 在你的代码中
[controller setContentURL:movieURL];
I think this might be due to a player already existing. 我认为这可能是因为玩家已经存在。
try to add something like this: 尝试添加这样的东西:
if (player != nil) {
[player release];
player = nil; //i prefer to do both, just to be sure.
}
put it after the 把它放在后面
[MainViewController stopStreaming];
and before the 之前
player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"http://159.253.145.180:7104"]];
i'm not sure if this is right but if you have checked the link 我不确定这是否正确,但如果您检查了链接
MPMoviewPlayerController Class Reference MPMoviewPlayerController类参考
which includes the code from apple : 其中包括来自apple的代码:
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];
maybe you need to prepareToPlay first and then add it to your view as subview.. 也许您需要先准备好PlayPlay然后将其作为子视图添加到您的视图中。
i'm not sure but hope this helps.. 我不确定,但希望这会有所帮助..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.