views:

113

answers:

0

I am writing an iPhone app that play streaming over http, here is my code


- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self 
            selector:@selector(moviePlayerPlaybackStateDidChange:) 
             name:MPMoviePlayerPlaybackStateDidChangeNotification 
              object:nil];

    //livePlayer is MPMoviePlayerController declared in header
    livePlayer =  [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString: liveStreamURL]];
     livePlayer.controlStyle = MPMovieControlStyleNone; 
    [livePlayer prepareToPlay];
    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(moviePlayerLoadStateChanged:) 
              name:MPMoviePlayerLoadStateDidChangeNotification 
               object:nil];
}

    - (void) moviePlayerLoadStateChanged:(NSNotification*)notification {
     if ([livePlayer loadState] != MPMovieLoadStateUnknown) {
             [[NSNotificationCenter  defaultCenter] removeObserver:self 
               name:MPMoviePlayerLoadStateDidChangeNotification 
                object:nil];
             [livePlayer play];
         }

    }

    -(void) moviePlayerPlaybackStateDidChange:(NSNotification*)notification {

 NSLog(@"playbackDidChanged");
 MPMoviePlayerController *moviePlayer = notification.object;
    MPMoviePlaybackState playbackState = moviePlayer.playbackState;
 if(playbackState == MPMoviePlaybackStateStopped) {
  NSLog(@"MPMoviePlaybackStateStopped");
 } else if(playbackState == MPMoviePlaybackStatePlaying) {
  NSLog(@"MPMoviePlaybackStatePlaying");
 } else if(playbackState == MPMoviePlaybackStatePaused) {
  NSLog(@"MPMoviePlaybackStatePaused");
 } else if(playbackState == MPMoviePlaybackStateInterrupted) {
  NSLog(@"MPMoviePlaybackStateInterrupted");
 } else if(playbackState == MPMoviePlaybackStateSeekingForward) {
  NSLog(@"MPMoviePlaybackStateSeekingForward");
 } else if(playbackState == MPMoviePlaybackStateSeekingBackward) {
  NSLog(@"MPMoviePlaybackStateSeekingBackward");
 }
}

the method moviePlayerPlaybackStateDidChange only called once when it start to play the stream. because the network state is not good, some time it stop play and show a black screen, I want to display a UIActivityIndicateView when buffering stream, can any one tell me how to that? I am using SDK4.0

related questions