4

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

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

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

4

4 回答 4

8

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

player.movieSourceType = MPMovieSourceTypeStreaming;
于 2012-07-23T13:33:25.757 回答
2

我认为这是由于静态播放器。尝试使用movieplayer的属性

采用:

@property (nonatomic,retain) MPMoviePlayerController *player;

在实施中:

@synthesize player = _player;

在初始化中:

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

在您的代码中

[controller setContentURL:movieURL];
于 2012-05-02T12:14:38.203 回答
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"]];
于 2012-05-02T12:15:01.260 回答
0

我不确定这是否正确,但如果您检查过链接

MPMoviewPlayerController 类参考

其中包括来自苹果的代码:

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,然后将其作为子视图添加到您的视图中。

我不确定,但希望这会有所帮助..

于 2012-05-02T12:17:00.420 回答