博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
Hello,
I would to play a video file in my iphone. Everything works fine but the result video does not show the volume control (I can listen audio!).
What's wrong?



This is the code (an example project is here: http://dl.dropbox.com/u/103260/MoviePlayer-ios4.zip):

Any idea?
Code:
@implementation CSMoviePlayer



- (id) initWithMovieAtPath:(NSURL *) path

{

     self = [super init];

     if (self != nil) {

          inPlay = NO;

          movieURL = [path retain];

          NSLog(@"LOAD MOVIE %@",[movieURL absoluteString]);

          [self.view setBackgroundColor:[UIColor blackColor]];

          

          

     }

     return self;

}







// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad {

    [super viewDidLoad];

     // Register that the load state changed (movie is ready)

     [[NSNotificationCenter defaultCenter] addObserver:self 

                                                        selector:@selector(moviePlayerLoadStateChanged:) 

                                                             name:MPMoviePlayerLoadStateDidChangeNotification 

                                                          object:nil];

}



- (void) viewDidAppear:(BOOL)animated {

     

          // When tapping movie, status bar will appear, it shows up

          // in portrait mode by default. Set orientation to landscape

     [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];

     

          // Rotate the view for landscape playback

     [[self view] setBounds:CGRectMake(0, 0, 480, 320)];

     [[self view] setCenter:CGPointMake(160, 240)];

     [[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)]; 

          [self readyPlayer];



}



- (void) readyPlayer

{

     [[MCHTTPFetcher httpCentral] networkActivityIncrement:YES];



     inPlay = YES;

      mp =  [[MPMoviePlayerController alloc] initWithContentURL:movieURL];

     

     if ([mp respondsToSelector:@selector(loadState)]) 

     {

               // Set movie player layout

          [mp setControlStyle:MPMovieControlStyleFullscreen];

          [mp setFullscreen:YES];

          

               // May help to reduce latency

          [mp prepareToPlay];

          

     

     }  

     else

     {

               // Register to receive a notification when the movie is in memory and ready to play.

          [[NSNotificationCenter defaultCenter] addObserver:self 

                                                             selector:@selector(moviePreloadDidFinish:) 

                                                                  name:MPMoviePlayerContentPreloadDidFinishNotification 

                                                               object:nil];

     }

     

          // Register to receive a notification when the movie has finished playing. 

     [[NSNotificationCenter defaultCenter] addObserver:self 

                                                        selector:@selector(moviePlayBackDidFinish:) 

                                                             name:MPMoviePlayerPlaybackDidFinishNotification 

                                                          object:nil];

}



/*---------------------------------------------------------------------------

 * For 3.2 and 4.x devices

 * For 3.1.x devices see moviePreloadDidFinish:

 *--------------------------------------------------------------------------*/

- (void) moviePlayerLoadStateChanged:(NSNotification*)notification 

{



          // Unless state is unknown, start playback

     if ([mp loadState] != MPMovieLoadStateUnknown)

     {

               // Remove observer

          [[NSNotificationCenter      defaultCenter] 

           removeObserver:self

           name:MPMoviePlayerLoadStateDidChangeNotification 

           object:nil];

          

               // When tapping movie, status bar will appear, it shows up

               // in portrait mode by default. Set orientation to landscape

          [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];

          

               // Rotate the view for landscape playback

          [[self view] setBounds:CGRectMake(0, 0, 480, 320)];

          [[self view] setCenter:CGPointMake(160, 240)];

          [[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)]; 

     

               // Set frame of movieplayer

          [[mp view] setFrame:CGRectMake(0, 0, 480, 320)];

          

               // Add movie player as subview

          [[self view] addSubview:[mp view]];   

          

               // Play the movie

          [mp play];

     }

}



/*---------------------------------------------------------------------------

 * For 3.1.x devices

 * For 3.2 and 4.x see moviePlayerLoadStateChanged: 

 *--------------------------------------------------------------------------*/

- (void) moviePreloadDidFinish:(NSNotification*)notification 

{

          // Remove observer

     [[NSNotificationCenter      defaultCenter] 

      removeObserver:self

      name:MPMoviePlayerContentPreloadDidFinishNotification

      object:nil];

     

          // Play the movie

      [mp play];

}



/*---------------------------------------------------------------------------

 * 

 *--------------------------------------------------------------------------*/

- (void) moviePlayBackDidFinish:(NSNotification*)notification 

{    

     [[UIApplication sharedApplication] setStatusBarHidden:NO];

     [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];



          // Remove observer

     [[NSNotificationCenter      defaultCenter] 

      removeObserver:self

      name:MPMoviePlayerPlaybackDidFinishNotification 

      object:nil];

     

     [self dismissModalViewControllerAnimated:YES];     

}

 

 

I think I've solved.
Without audio enabled (iphone audio hardware switch is off) audio controller disappear.
It will appear only when hw is on.
This because the audio inherit the application's audio session.
Using:
Code:
// Indicates if the movie player should inherit the application's audio session instead of creating a new session (which would interrupt the application's session).
// Defaults to YES. Setting this property during playback will not take effect until playback is stopped and started again.

mp.useApplicationAudioSession = NO;
Audio controller appear every time and it's indipendent from the switch.
The only question is...why on simulator it's always off?