1

我有一个带音频的视频和一个 mp3。我想在播放 mp3 时静音播放视频(音频关闭)。我MPMoviePlayerController用来播放视频和AVAudioPlayer播放 mp3。有没有办法做到这一点?

4

3 回答 3

0

MPMoviePlayerController does not appear to give you any control over volume. I would suggest you have a look at AVPlayer and the accompanying examples. It's a bit more work to play video, but you can use the "muted" property to do exactly what you want.

于 2013-03-22T18:42:39.727 回答
0

您实际上可以使用MPMusicPlayerController来实现您的目标。

- (void)muteVidepPlayer:(BOOL)muteFlag
{
    MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
    if (muteFlag)
    {
        self.lastVolume = musicPlayer.volume; 
        [musicPlayer setVolume:0.0f];
    }
    else
    {
        [musicPlayer setVolume:self.lastVolume];
    }
}

它的作用是使用一个MPMusicPlayerController允许您调整音量的实例。不要太困惑,这也将有效地调整您的MPMoviePlayerController实例的音量。这是因为MPMoviePlayerControllerMPMusicPlayerController 都使用应用程序音频会话(默认情况下)。

为了确保您能够将音量重置为以前的设置,此方法尝试将该值存储在播放控制器/视图控制器/其他类型的float( ) 类型的实例变量中。self.lastVolume

于 2013-03-24T00:11:39.017 回答
0

我选择回答我自己的问题:@nickbona 答案很好,但没有简单的示例代码。所以解决的办法是用AVPlayer代替MPMoviePlayerController。

因此,如果其他人需要它,我在这里提供一个简单的源代码示例:

//init player
AVAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"yourfile" ofType:@"mp4"]] options:nil];
AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];
AVPlayer *myPlayerController = [AVPlayer playerWithPlayerItem:anItem];

//disable audio (this is the version when you have more than one video in the playlist: i write this version so it should be more useful)
NSArray *playerTracks = [asset tracksWithMediaType:AVMediaTypeAudio];
NSMutableArray *playerParams = [NSMutableArray array];
for (AVAssetTrack *track in playerTracks) {
    AVMutableAudioMixInputParameters *audioInputParams =    [AVMutableAudioMixInputParameters audioMixInputParameters];
    [audioInputParams setVolume:0.0 atTime:kCMTimeZero];
    [audioInputParams setTrackID:[track trackID]];
    [playerParams addObject:audioInputParams];
}
AVMutableAudioMix *muteAudioMix = [AVMutableAudioMix audioMix];
[muteAudioMix setInputParameters:playerParams];
[[myPlayerController currentItem] setAudioMix:muteAudioMix];

希望它可以提供帮助。

于 2013-03-27T09:55:46.317 回答