1110
1110

Reputation: 6839

MpMoviePlayer load audio after stop event

My app is 95% over and I have a very nasty bug, and I really need some help. I have table view with list of stream url's > I select movie and it loads > I have overlay with stop, pause, volume and scroll view with streams to switch streams while previous stream play. When I start application everything works fine, all streams works. But when I hit STOP button, current stream is stopped and mpmovieplayer is closed and I am again in table view from the beginning. Stop button code is this (I subclass mpmovieplayer):

- (void) StopAndDismiss: (NSNotification *) notification
{
    [mp stop]; // is MPMoviePlayerController object
    [self dismissModalViewControllerAnimated:YES];  
}

Then I select some stream from table, and process is the same show mpmovieplayercontroller, code for load movie is:

-(void) ChannelFromMainChannelPicker: (NSNotification *)note
{   
    NSString *stringFromNote = (NSString *)[note object];

    NSString *title = [[NSString alloc] initWithFormat:@"%@", stringFromNote];

    cPlayer = [[CustomMoviePlayerController alloc] initWithUrlPath:title];
    [self presentModalViewController:cPlayer animated:NO];

    [cPlayer readyPlayer:stringFromNote];

    [title release];
}
- (void)readyPlayer:(NSString *)p_url
{   
    if(mp != nil)
    {           
        NSString *title = [[NSString alloc] initWithFormat:@"%@", p_url];
        NSURL *n = [[NSURL alloc]initWithString:title];

        [mp setContentURL:n];

        mp.initialPlaybackTime = -1.0;

        [title release];
        [n release];

        [mp play];      
    }
    else
    {   
        mp =  [[MPMoviePlayerController alloc] initWithContentURL:movieUrlPath];

        //[mp setAllowsWirelessPlayback:YES];

        if ([mp respondsToSelector:@selector(loadState)]) 
        {
            // Set movie player layout
            [mp setControlStyle:MPMovieControlStyleNone];
            [mp setFullscreen:YES];

            // May help to reduce latency
            [mp prepareToPlay];

            // Register that the load state changed (movie is ready)
            [[NSNotificationCenter defaultCenter] addObserver:self 
                                                     selector:@selector(moviePlayerLoadStateChanged:) 
                                                         name:MPMoviePlayerLoadStateDidChangeNotification 
                                                       object:nil];

            [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(SwitchToAnotherStreamUrl:) 
                                                         name:@"notiSwitchToAnotherStreamUrl"
                                                       object:nil];

            [[NSNotificationCenter defaultCenter] addObserver:self 
                                                     selector:@selector(moviePlayerPlaybackStateDidChange:) 
                                                         name:MPMoviePlayerPlaybackStateDidChangeNotification 
                                                       object:nil];

            [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(PauseStream:) 
                                                         name:@"notiPauseStream"
                                                       object:nil];

            [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(StopAndDismiss:) 
                                                         name:@"notiStopAndDismiss"
                                                       object:nil];     
        }       
    }

    // Register to receive a notification when the movie has finished playing. 
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(moviePlayBackDidFinish:) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:nil]; 
}

But when I change to some other stream now from overlay stream list, only audio works, but no video (just black screen). But I load the same stream again it loads it normal. But every first time when I start stream after I press STOP button is loads just audio, and I need to call it second time to work. In my custom movie player I overload this method and maybe here is some problem:

- (void) moviePlayerPlaybackStateDidChange: (NSNotification *) notification 
{
    if(mp.playbackState == MPMoviePlaybackStateStopped)
    {
        NSLog(@"Loading so display activity indicator");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"notiShowLoader" object:nil];

    }
    else if (mp.playbackState == MPMoviePlaybackStatePlaying) 
    {
        NSLog(@"Done with loading, hide activity indicator");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"notiHideLoader" object:nil];       

        [[NSNotificationCenter defaultCenter] postNotificationName:@"notiBringCPToFront" object:nil];   
    }
    else if (mp.playbackState == MPMoviePlaybackStatePaused)
    {
        NSLog(@"Movie is paused");
    }
}

PS Is there any way that I can check is audio loaded and video not and somehow force it reload player?

I added NSlog to check playback states. And there is something wired, as I call STOP next streams that I try to play it calls multiple times:

 0  MPMoviePlaybackStateStopped,
 1  MPMoviePlaybackStatePlaying,
 2  MPMoviePlaybackStatePaused,
 3  MPMoviePlaybackStateInterrupted,
 4  MPMoviePlaybackStateSeekingForward,
 5  MPMoviePlaybackStateSeekingBackward


first time from table view
2011-01-21 13:46:04.865[3561:207] CURRENT PLAYBACK STATE ID = 1

second time from overlay
2011-01-21 13:46:30.338[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:46:30.461[3561:207] CURRENT PLAYBACK STATE ID = 1

STOP button press
2011-01-21 13:47:14.797[3561:207] CURRENT PLAYBACK STATE ID = 0

from table view
2011-01-21 13:47:31.123[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:47:31.123[3561:207] CURRENT PLAYBACK STATE ID = 1

from overlay again (ONLY AUDIO)
2011-01-21 13:47:50.001[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:47:50.001[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:47:50.119[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:47:50.119[3561:207] CURRENT PLAYBACK STATE ID = 1
2011-01-21 13:47:50.148[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:47:50.148[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:47:50.149[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:47:50.149[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:47:50.150[3561:207] CURRENT PLAYBACK STATE ID = 1
2011-01-21 13:47:50.150[3561:207] CURRENT PLAYBACK STATE ID = 0

third time (now works and audio and video)
2011-01-21 13:48:21.752[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:48:21.752[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:48:21.852[3561:207] CURRENT PLAYBACK STATE ID = 1
2011-01-21 13:48:21.852[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:48:21.855[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:48:21.855[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:48:21.856[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:48:21.856[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:48:21.858[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:48:21.858[3561:207] CURRENT PLAYBACK STATE ID = 1

STOP second time
2011-01-21 13:49:39.046[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:49:39.046[3561:207] CURRENT PLAYBACK STATE ID = 0

again load from table
2011-01-21 13:50:00.155[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:00.155[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:00.155[3561:207] CURRENT PLAYBACK STATE ID = 1

from overlay 
2011-01-21 13:50:27.211[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.211[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.211[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.325[3561:207] CURRENT PLAYBACK STATE ID = 1
2011-01-21 13:50:27.326[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.326[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.333[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.333[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.333[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.334[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.335[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.335[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.336[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.336[3561:207] CURRENT PLAYBACK STATE ID = 1
2011-01-21 13:50:27.337[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.339[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.339[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.339[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.340[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.340[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.340[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.341[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.341[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:50:27.342[3561:207] CURRENT PLAYBACK STATE ID = 1

again from overlay (only audio)

2011-01-21 13:56:56.198[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.198[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.198[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.335[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.335[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.335[3561:207] CURRENT PLAYBACK STATE ID = 1
2011-01-21 13:56:56.338[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.338[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.338[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.339[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.339[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.340[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.341[3561:207] CURRENT PLAYBACK STATE ID = 1
2011-01-21 13:56:56.341[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.341[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.359[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.359[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.359[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.360[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.360[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.360[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.361[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:56:56.361[3561:207] CURRENT PLAYBACK STATE ID = 1
2011-01-21 13:56:56.361[3561:207] CURRENT PLAYBACK STATE ID = 0

same icon again (works)
2011-01-21 13:57:31.115[3561:207] CURRENT PLAYBACK STATE ID = 1
2011-01-21 13:57:31.115[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.115[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.118[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.118[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.118[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.119[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.119[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.119[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.120[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.120[3561:207] CURRENT PLAYBACK STATE ID = 1
2011-01-21 13:57:31.121[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.131[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.131[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.131[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.132[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.132[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.132[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.133[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.134[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:57:31.134[3561:207] CURRENT PLAYBACK STATE ID = 1

STOP again
2011-01-21 13:58:17.318[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:17.318[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:17.318[3561:207] CURRENT PLAYBACK STATE ID = 0

play from table
2011-01-21 13:58:36.085[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:36.085[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:36.085[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:36.085[3561:207] CURRENT PLAYBACK STATE ID = 1

STOP
2011-01-21 13:58:53.599[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:53.599[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:53.599[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:53.600[3561:207] CURRENT PLAYBACK STATE ID = 0

PLAY from table again
2011-01-21 13:58:57.236[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:57.236[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:57.236[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:57.236[3561:207] CURRENT PLAYBACK STATE ID = 0
2011-01-21 13:58:57.236[3561:207] CURRENT PLAYBACK STATE ID = 1

Upvotes: 0

Views: 1153

Answers (1)

1110
1110

Reputation: 6839

Ok beginner error, I didn't know that I need to remove all observers when I close my custom movie player. This solved problem:

[mp stop];
    [mp.view removeFromSuperview];      
    [self dismissModalViewControllerAnimated:YES];  

    [[NSNotificationCenter defaultCenter] removeObserver:self];

Upvotes: 1

Related Questions