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?

  • 相关阅读:
    如何让nodejs使用多线程执行
    web input光标的颜色
    web视频自定义规划
    webgl 学习注意事项
    前端url创建,以及base64与url的转化
    微信 input 照相机 呼出
    js 资源下载方法
    在React项目中使用React-intl实现多语言支持,以及对react-init各组件的解读
    React Native 的组件定义及使用
    AMD模块&CommonJs模块&ES6模块
  • 原文地址:https://www.cnblogs.com/chen1987lei/p/1821773.html
Copyright © 2011-2022 走看看