0

我开始相信 iOS 就是纯粹的魔法。我浏览了 AVAudioPlayer 上的许多帖子以寻求解决方案,但没有一个有效。

当应用程序启动时,我有一个从 MPMoviePlayerController 开始的电影,并且我有 AVAudioPlayer 以如下声音开始:

    -(void)createAndPlayMovieFromURL:(NSURL *)movieURL sourceType:(MPMovieSourceType)sourceType{

        /* Create a new movie player object. */
       player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
       player.controlStyle = MPMovieControlModeDefault;

        if (player) 
        {
            /* Save the movie object. */
            [self setMoviePlayerController:player];

            /* Specify the URL that points to the movie file. */
            [player setContentURL:movieURL];        

            /* If you specify the movie type before playing the movie it can result 
             in faster load times. */
            [player setMovieSourceType:sourceType];

            CGRect frame = CGRectMake(0, 0, 480, 320);

            /* Inset the movie frame in the parent view frame. */
            [[player view] setFrame:frame];

            [player view].backgroundColor = [UIColor blackColor];

            /* To present a movie in your application, incorporate the view contained 
             in a movie player’s view property into your application’s view hierarchy. 
             Be sure to size the frame correctly. */
            [self.view addSubview: [player view]];        
        }
    [[self moviePlayerController] play];
} 

和声音:

- (void)setupAudioPlayBack:(NSString *) path : (NSString *) extension{

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                         pathForResource:path
                                         ofType:extension]];
    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc]
                   initWithContentsOfURL:url
                   error:&error];
    if (error)
    {
        NSLog(@"Error in audioPlayer: %@",
              [error localizedDescription]);
    } else {
        audioPlayer.delegate = self;
        [audioPlayer prepareToPlay];
    }
}

现在让我们谈谈魔术。当第一部电影播放和声音时,声音没有响应手机上的音量控制,也没有响应静音模式开关。

但随后第二部电影以自己的声音播放,一切正常 - 静音模式打开时没有声音,如果静音模式关闭,它会响应音量控制。第三部电影 - 也完美播放。

我尝试使用player.useApplicationAudioSession = NOfor MPMoviePlayerController - 修复了第一次声音对静音模式或音量控制没有响应的问题,但是现在当第二部电影开始播放时它立即冻结并且播放的声音从原始大小减少到大约 2秒。

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error:nil];无论我在哪里设置它都没有帮助。

所以我的问题是 - 如何让 MPMoviePlayerController 与 AVAudioPlayer 交互没有任何问题......

4

1 回答 1

2

好的,我实际上找到了一个解决方案(再一次,魔术)。

当您启动 ViewController 时,您将其设置如下:

 - (void)viewDidLoad
{
    [self setupAudioPlayBack:@"sound1" :@"caf"];
    [self playMovieFile:[self localMovieURL:@"mov1" :@"mov"]];
    audioTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(playAudio) userInfo:nil repeats:NO];

    [super viewDidLoad];
}

playAudio 方法很简单:

 - (void)playAudio{

    [audioPlayer play];
}

Just 0.1 millisecond timer is enough to make everything work (0.0 doesn't work) and make sure a new video doesn't start before the audio finishes or it will destroy the settings so you will have to invalidate the timer and redo the timer again.

于 2013-01-02T21:45:34.440 回答