AVPlayer 的音频音量超过 Airplay?

Posted

技术标签:

【中文标题】AVPlayer 的音频音量超过 Airplay?【英文标题】:AVPlayer audio volume over Airplay? 【发布时间】:2012-10-18 11:13:42 【问题描述】:

有谁知道通过 Airplay 播放 AVPlayer 曲目时调整音量的方法?我已经尝试过 AVAudioMix 和 MPVolumeView 但它们都不起作用。我已经在 ios 5 和 iOS 6 上尝试过,并且正在使用最新的 xcode 4.5.1。这不起作用的一个简单示例是 Apple 的 AVPlayerTestApp,它使用 setVolumeRampFromStartVolume 进行简单的淡出。这在设备上可以正常工作,但如果通过 Airplay 连接则不能。

在 ViewDidLoad 中,我加载了一个曲目并开始播放(这一切都取自 AVPlayerTestApp)

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:TRUE error:nil];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];

self.mediaItems = [[[MPMediaQuery songsQuery] items] mutableCopy];

NSURL *anUrl = [[mediaItems objectAtIndex: 0] valueForProperty:MPMediaItemPropertyAssetURL];
AVAsset *asset = [AVURLAsset URLAssetWithURL:anUrl options:nil];
AVPlayerItem *myPlayerItem = [AVPlayerItem playerItemWithAsset:asset];


self.myPlayer1 = [[[AVPlayer alloc] initWithPlayerItem:myPlayerItem] retain];

[myPlayer1 play];

然后我有一个按钮,它打开一个警报视图,允许用户设置音量并打开播放

MPVolumeView *volumeView = [[[MPVolumeView alloc] initWithFrame: CGRectMake(10, 37, 260, 20)] autorelease];
UIAlertView *volumeAlert = [[UIAlertView alloc] initWithTitle:@"Volume" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[volumeView sizeToFit];
[volumeAlert addSubview:volumeView];
[volumeAlert show];
[volumeAlert release];

然后是淡出当前播放曲目的另一个按钮

AVAsset *asset = [myPlayer1.currentItem asset];
NSArray *keys = [NSArray arrayWithObject:@"tracks"];
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^(void) 
    NSError *error = nil;
    AVKeyValueStatus trackStatus = [asset statusOfValueForKey:@"tracks" error:&error];
    CMTime currentTime;
    switch (trackStatus) 
        case AVKeyValueStatusLoaded:
            if(myPlayer1)
            
                NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeAudio];
                NSMutableArray * allAudioParams = [NSMutableArray array];
                for (AVAssetTrack *t in tracks) 
                    AVMutableAudioMixInputParameters *params =[AVMutableAudioMixInputParameters audioMixInputParameters];

                    float fadeOutSeconds = 5;

                    currentTime = [myPlayer1 currentTime];

                    [params setVolumeRampFromStartVolume: 1.0 toEndVolume: 0.0 timeRange: CMTimeRangeMake(currentTime, CMTimeMakeWithSeconds(fadeOutSeconds, 1))];

                    [params setTrackID:[t trackID]];
                    [allAudioParams addObject:params];
                
                AVMutableAudioMix * zeromix = [AVMutableAudioMix audioMix];
                [zeromix setInputParameters:allAudioParams];

                [myPlayer1.currentItem setAudioMix:zeromix];
            

            break;
        case AVKeyValueStatusFailed:
            // error occured loading AVAsset
            NSLog(@"error occured loading AVAsset");
            break;
        case AVKeyValueStatusCancelled:
            // loading of the AVAsset was cancelled
            NSLog(@"loading of the AVAsset was cancelled");
            break;
        default:
            break;
    
];

这在设备上按预期工作,并在点击按钮时减弱音量。但是,如果打开 Airplay,则无法更改音量。使用 MPMoviePlayController 我可以自己淡化,这在 Airplay 上工作,但 AVPlayer 的延迟比 Airplay 少,所以我宁愿使用它。

非常感谢任何帮助。

【问题讨论】:

我的应用程序成功地通过 Airplay 淡入淡出歌曲。你能发布一些你用来调整音量的代码吗? 感谢山姆的回复,我已经在我的问题中发布了一些代码。 我对我的应用进行了进一步的尝试,并确认它实际上没有正确地进行淡入淡出。当我测试它时,我在打开 Airplay 镜像进行测试,效果很好。仅将 airplay 用于音频时,播放效果不佳。当我上次仅使用音频进行测试时,我确信它在 iOS 5 中可以正常工作,但当时我使用的是不同的设备。我目前正在使用AirServerApp for mac 进行测试,您在使用什么设备进行测试? 使用 ipad3 和 AirServer 应用程序进行测试,但我也在 Mac 上使用 Reflection 应用程序进行了测试。我的 4s 上仍然有 ios 5,并且出现了同样的问题。关于它与镜像一起工作的非常有趣的观察,我没有尝试过,它就像你说的那样工作。我需要考虑一下,看看这是否可以作为一种解决方法。非常感谢山姆。 Sam,我决定处理这个问题的唯一方法是使用 Airplay Mirroring 并删除 MPVolumeView。如果你提出你的建议作为答案,我会承认的。谢谢。 【参考方案1】:

由于 Sam_899 没有发布答案,我将不得不这样做。据我所知,除非在打开镜像的情况下建立连接,否则无法通过 airplay 控制 avplayer 的音量。由于打开镜像的唯一方法是在您的应用程序外部,因此唯一的选择是通知用户如何自己打开它并希望他们对此感到满意!对于那些不知道如何通过airplay打开镜像的人;双击home键,向左滑动到音量控制,点击airplay图标,选择要连接的airplay设备,然后开启镜像。

【讨论】:

以上是关于AVPlayer 的音频音量超过 Airplay?的主要内容,如果未能解决你的问题,请参考以下文章

AirPlay:连接到 Apple TV 时音量控制被禁用

如何在 AVplayer 中为视频文件实现音量控制?

使用allowExternalPlayback禁用Airplay,但音频仍在播放

AVPlayer 音量控制

为 AirPlay 播放视频时添加 UIBackgroundModes“音频”会导致应用被拒绝?

使用 AVPlayer 进行 Airplay?