zoukankan      html  css  js  c++  java
  • MPMovieViewController does not show audio control (I can listen it too!)

    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?

  • 相关阅读:
    Qt Create or VS 2015 使用 Opencv330 相机静态库链接错误如何解决?
    Qt create 如何构建 ActiveX 控件?
    VB 如何调用 c++ DLL?
    Opencv 330 如何裁剪图片中大的目标?
    Opencv 330 如何進行圖像的旋轉?
    Qt 开发 MS VC 控件终极篇
    Qt 5.9.4 如何静态编译和部署?
    图片理论基础
    iOS平台设置系统状态栏(通知栏、顶部状态栏)样式背景颜色或透明
    jQuery基础
  • 原文地址:https://www.cnblogs.com/chen1987lei/p/1821773.html
Copyright © 2011-2022 走看看