0

对不起我的问题,但我已经实现了一个介绍视频,尽管我的 iPad 有硬件静音开关,但音频仍在播放。我还在我的应用程序中使用 AVAudioplayer 来播放简短的声音样本。在这个类中,它是我设置“AVAudioSessionCategory”的唯一区域。但是仅对于所有音频播放,都听不到任何声音。它只是为了我的介绍视频。

任何帮助如何解决“音频错误”,使电影播放器​​保持沉默?谢谢

这是我的音频课:

- (id)initWithSoundfileName:(NSString*) file 
{
    if ((self = [super init]))
    {
        NSString* filename =       [file stringByDeletingPathExtension];
        NSString* fileextension =  [file pathExtension];

        // get file path from bundle
        NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: filename ofType: fileextension];
        NSLog(@"AudioPlayer init: %@", soundFilePath);

        NSURL* fileurl = [[NSURL alloc] initFileURLWithPath:soundFilePath];
        NSError* error = nil;

        AVAudioPlayer* audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileurl error:&error ];
        if (error) { NSLog(@"Error creating AVAudioPlayer %@", [error description]);}

        // set audio policy
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:NULL];

        self.player =  audioplayer;
        [self.player prepareToPlay];
        [self.player setDelegate:self];
    }
    return self;

}
-(void) play{
    [self.player play];
}

这是我的视频播放方法:

- (void)playIntroVideo
{    
    NSString *movpath = [[NSBundle mainBundle] pathForResource:@"mymovie" ofType:@"mp4"];

    NSURL *fileURL    =   [NSURL fileURLWithPath:movpath];  
    self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 
    self.moviePlayerController.fullscreen = YES;
    self.moviePlayerController.scalingMode = MPMovieScalingModeAspectFit;
    self.moviePlayerController.controlStyle = MPMovieControlStyleNone;
    self.moviePlayerController.movieSourceType = MPMovieSourceTypeFile;

    self.moviePlayerController.useApplicationAudioSession = NO;     
    [self.moviePlayerController.view setFrame: self.view.bounds];   

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackComplete:)  
                                                 name:MPMoviePlayerPlaybackDidFinishNotification  
                                               object:self.moviePlayerController];  


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlaybackStateChanged:)  
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification  
                                               object:self.moviePlayerController];  

    [self.view addSubview:self.moviePlayerController.view];

    [self.moviePlayerController prepareToPlay];
    [self.moviePlayerController play];
}
4

1 回答 1

2

就像@Till 提到的:当我将MoviePlayerController属性更改为 时useApplicationAudioSession=TRUE,它解决了我的问题。音频播放无声。

于 2012-07-17T21:12:45.937 回答