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?

  • 相关阅读:
    mongodb备份与恢复
    MongoDB-3.4安装文档
    (转)Zabbix 3.2.7编译安装记录
    (转)error while loading shared libraries:libmysqlclient.so.18 错误
    (转)如何使用Journalctl查看并操作Systemd日志
    (转)基于CentOS 7安装Zabbix 3.4和Zabbix4.0
    (转)yum安装MariaDB(使用国内镜像快速安装,三分钟安装完毕)
    (转)nmon和nmon analyser的下载和使用
    (转)Db2 数据库常见堵塞问题分析和处理
    (转)我是一个线程
  • 原文地址:https://www.cnblogs.com/chen1987lei/p/1821773.html
Copyright © 2011-2022 走看看