如何使用MPMoviePlayerController播放视频时静音/取消静音? [英] How to mute/unmute audio when playing video using MPMoviePlayerController?
问题描述
我已经创建了自己的自定义控件,用于 MPMoviePlayerController
。到目前为止一切正常,除了静音按钮控件。
I've created my own custom controls for use with the MPMoviePlayerController
. So far everything works except the mute button control.
我在使用以下代码之前配置了 AVAudioSession
创建 MPMoviePlayerController的实例
。
I've configured the AVAudioSession
using the following code before I create my instance of the MPMoviePlayerController
.
NSError *modeError = nil;
[self.audioSession setMode:AVAudioSessionModeMoviePlayback error:&modeError];
if (modeError != nil) {
NSLog(@"Error setting mode for AVAudioSession: %@", modeError);
}
NSError *categoryError = nil;
[self.audioSession setCategory:AVAudioSessionCategoryPlayback error:&categoryError];
if (categoryError != nil) {
NSLog(@"Error setting category for AVAudioSession: %@", categoryError);
}
然后在我的静音按钮回调方法中,我有以下代码:
Then in my mute button callback method I have the following code:
NSError *activeError = nil;
[self.audioSession setActive:NO error:&activeError];
if (activeError != nil) {
NSLog(@"Error setting inactive state for AVAudioSession: %@", activeError);
}
点击静音按钮后,我收到以下无用错误:
When clicking the Mute button I get the following unuseful error:
Error Domain=NSOSStatusErrorDomain Code=560030580 "The operation couldn’t be completed. (OSStatus error 560030580.)"
我正在链接到 AVFoundation
框架。
这真的开始让我感到烦恼,因为我无法为我的生活找到一种方法来减少或静音我的应用程序的播放音频。
This is really starting to bug me as I can't for the life of me work out a way to reduce or mute the playback audio of my application.
我不想仅仅根据 AVAudioSession
AVAudioSessionCategoryPlayback $ c定义的应用程序级别更改系统全局卷$ c> category。
I don't want to change the system global volume just the application level volume as defined by the AVAudioSession
AVAudioSessionCategoryPlayback
category.
似乎你可以设置 AVAudioPlayer
的音量而不是的MPMoviePlayerController
。我在SO上看过其他帖子,说只需创建一个 AVAudioPlayer
的实例并设置音量,但这只会导致我的应用程序崩溃,我希望它有事情要做事实上我没有使用 initWithContentsOfURL:错误:
或 initWithData:错误:
而是使用`init' 。
It seems that you can set the volume of the AVAudioPlayer
but not the MPMoviePlayerController
. I've seen other posts here on SO that say just create an instance of AVAudioPlayer
and set the volume but this just causes my app to crash and I expect it has something to do with the fact I'm not using the initWithContentsOfURL:error:
or initWithData:error:
and instead using `init'.
任何帮助将不胜感激。
Any help would be appreciated.
推荐答案
在与Apple技术人员交谈后发现,无法使用 MPMoviePlayerController
。
After speaking to an Apple technician it turns out that it's not possible to control or mute the audio using MPMoviePlayerController
.
相反,你必须使用AVFoundations创建自己的控制器 AVPlayer
class 。
Instead you have to create your own controller using AVFoundations AVPlayer
class.
一旦你使用它,就需要创建自定义音频混合并设置音量。它实际上运作良好。
Once you're using that it's a matter of creating a custom audio mix and setting the volume level. It actually works very well.
示例代码:
AVURLAsset * asset = [AVURLAsset URLAssetWithURL:[self localMovieURL] options:nil];
NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio];
// Mute all the audio tracks
NSMutableArray * allAudioParams = [NSMutableArray array];
for (AVAssetTrack *track in audioTracks) {
AVMutableAudioMixInputParameters *audioInputParams =[AVMutableAudioMixInputParameters audioMixInputParameters];
[audioInputParams setVolume:0.0 atTime:kCMTimeZero ];
[audioInputParams setTrackID:[track trackID]];
[allAudioParams addObject:audioInputParams];
}
AVMutableAudioMix * audioZeroMix = [AVMutableAudioMix audioMix];
[audioZeroMix setInputParameters:allAudioParams];
// Create a player item
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
[playerItem setAudioMix:audioZeroMix]; // Mute the player item
// Create a new Player, and set the player to use the player item
// with the muted audio mix
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
self.mPlayer = player;
[mPlayer play];
我写了 MPMoviePlayerController
替换课程这增加了对音量水平的支持。我将很快上传到github,并在这篇文章中添加链接。
I've written an MPMoviePlayerController
replacement class that adds support for volume level. I will upload the to github shortly and add the link in this post.
这篇关于如何使用MPMoviePlayerController播放视频时静音/取消静音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!