简体   繁体   中英

Audio playback for movie despite in silent mode?

sorry for my question but i´ve implemented a intro- video and despite of the hardware silent-switch of my iPad, the audio is playing. I´m also using the AVAudioplayer within my app just for playing short sound samples. Within this class, its the only region where i´ve set up the "AVAudioSessionCategory". But for all audio playback only, there´s nothing hearable. Its just for my intro-video.

Any help how to fix that "audio-bug" so the movie player is silent? Thanks you

Here´s my Audio-class:

- (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];
}

And here´s my video-playback method:

- (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];
}

Like @Till mentioned: When I change the MoviePlayerController property to useApplicationAudioSession=TRUE , it fixes my problem. Audio Playback is silent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM