AVQueuePlayer音量未更改

时间:2013-04-23 13:29:08

标签: ios objective-c avqueueplayer

我想用UISlider更改音量设置。

我在currentItem内使用AVQueuePlayerps0AVQueuePlayer

当我使用UISlider时,我没有错误和相同的声级:

- (IBAction)volumeSliderMoved:(UISlider *)sender
{
    AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParameters];

    AVPlayerItem *av = [ps0 currentItem];
    CMTime currentTime = [av currentTime];
    float volume = [sender value];

    [audioInputParams setVolume:volume atTime:currentTime];
    AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
    audioMix.inputParameters = [NSArray arrayWithObject:audioInputParams];
    av.audioMix = audioMix;

    [ps0 replaceCurrentItemWithPlayerItem: av];
}

已编辑: 我尝试了Apple的另一个solution来更改音量设置。

正如您在此解决方案中所看到的,它会创建一个新的playerItem和一个新的播放器。

但我的playerItem是当前版本的副本,因为我只想更改声音(而不是项目)。并且它与旧玩家自动相关。

当我尝试使用他们的解决方案时。我有一条错误消息说:

  

AVPlayerItem不能与多个AVPlayer实例关联

有什么建议吗?

再次编辑

使用AVQueuePlayer更改播放

我有一个每个mp3名称为“textMissingTab”的数组

我有一个带有AVPlayerItem“soundItems”的数组

创作:

textMissingTab = [[NSArray alloc] initWithObjects:@"cat",@"mouse",@"dog",@"shark",@"dolphin", @"eagle", @"fish", @"wolf", @"rabbit", @"person", @"bird", nil];

for (NSString *text in textMissingTab)
{
    NSURL *soundFileURL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:text ofType:@"mp3"]];
    AVPlayerItem *item = [AVPlayerItem playerItemWithURL:soundFileURL];
    [soundItems addObject:item];
}

初始化:

NSURL *soundFileURL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"mp3"]];
ps0 = [[AVQueuePlayer alloc] initWithURL:soundFileURL];

更改playItem:文本是NSString

int index = [textMissingTab indexOfObject:text];
[ps0 setActionAtItemEnd:AVPlayerActionAtItemEndNone];
CMTime newTime = CMTimeMake(0, 1);
[ps0 seekToTime:newTime];
[ps0 setRate:1.0f];
[ps0 replaceCurrentItemWithPlayerItem:[soundItems objectAtIndex:(index)]];
[ps0 play];

3 个答案:

答案 0 :(得分:0)

我在使用AVQueuePlayer和更改播放参数时遇到了一些问题。如果您的目标是使用滑块进行音量调整,我会在故事板中用空白UIView替换UISlider,然后在该视图中实例化MPVolumeView。这对我来说可靠。请注意,MPVolumeView不会显示在模拟器上。

答案 1 :(得分:0)

我认为你是对的tigloo。 MPVolumeView是管理声级的最佳方式。

本教程中对此进行了解释:http://www.littlereddoor.co.uk/ios/controlling-system-output-volume-by-adding-an-mpvolumeview-object-to-an-ios-application/

“在用户遇到由设备上设置的当前振铃音量创建的障碍块之前,一切正常。”AVAudioPlayer属性的最大值为1.0,其中1.0等于当前设备的铃声音量设置。简单地说,如果设备铃声音量设置为50%音量,那么AVAudioPlayer音量的100%仍仅等同于已经设置的50%铃声。使用这种方法的局限性不言自明。 “

我再次编辑了我的帖子,以展示我如何使用AVQueuePlayer管理一些歌曲。这部分代码正在运作。

答案 2 :(得分:0)

AVAsset *asset;
NSArray *playerTracks;
NSMutableArray *playerParams;
AVMutableAudioMix *muteAudioMix;
for (int k=0; k<[[audio items] count]; k++)
    {
        //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)
        asset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[soundfile objectAtIndex:k+([soundfile count]-[[audio items] count])] ofType:@"mp3"]] options:nil];

        playerTracks = [asset tracksWithMediaType:AVMediaTypeAudio];
        playerParams = [NSMutableArray array];
        for (AVAssetTrack *track in playerTracks) {
            AVMutableAudioMixInputParameters *audioInputParams =    [AVMutableAudioMixInputParameters audioMixInputParameters];
            [audioInputParams setVolume:1.0 atTime:kCMTimeZero];
            [audioInputParams setTrackID:[track trackID]];
            [playerParams addObject:audioInputParams];
        }
        muteAudioMix = [AVMutableAudioMix audioMix];
        [muteAudioMix setInputParameters:playerParams];

        [[[audio items] objectAtIndex:k] setAudioMix:muteAudioMix];
    }