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

*** 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?

Answers


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:

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

In your code

[controller setContentURL:movieURL];

I had the same problem. The solution that worked for me was remove the instruction

player.movieSourceType = MPMovieSourceTypeStreaming;

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

which includes the code from 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..

i'm not sure but hope this helps..


Need Your Help

Pointing a subdomain to an external server?

.htaccess hyperlink subdomain

I have a subdomain which I would like to point to a page on an external server. I am hoping I can do this without having ftp access to the external server? I can obtain access if I really HAVE to, ...