0

I wanted to change volume settings with a UISlider.

I used currentItem inside AVQueuePlayer. ps0 is an AVQueuePlayer.

I have no error and same sound level when I use my 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];
}

EDITED : I tried another solution from Apple to change volume settings.

As you can see in this solution, it create a new playerItem and a new player.

But my playerItem is a copy of the current one because I just want to change the sound (not the item). And it is automatically related to the old player.

When I try to use their solution. I have an error message saying:

An AVPlayerItem cannot be associated with more than one instance of AVPlayer

Any suggestion?

EDITED again

To change playback with AVQueuePlayer

I have an array with every mp3 name “textMissingTab”

I have an array with AVPlayerItem “soundItems”

Creation :

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

Init :

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

Change playItem : text is 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 Answers 3

0

I had several problems using AVQueuePlayer and changing playback parameters. If your goal is to use a slider for volume adjustment, I would replace the UISlider with a blank UIView in your storyboard and then instantiate an MPVolumeView within that view. This works reliably for me. Note that MPVolumeView does not show up on the simulator.

1
  • Thank you, it is a great idea to use MPVolumeView to manage sound level
    – user1056113
    Apr 24, 2013 at 7:39
0

I think you are right tigloo. MPVolumeView is the best way to manage sound level.

It is explained in this tutorial :http://www.littlereddoor.co.uk/ios/controlling-system-output-volume-by-adding-an-mpvolumeview-object-to-an-ios-application/

"Everything works exactly as we would expect until the user hits the stumbling block that is created by the current ringer volume that is set on the device. The maximum value of the AVAudioPlayer property is 1.0, where 1.0 is equal to the current ringer volume setting of the device. Simply put, if the device ringer volume is set to 50% volume, then 100% of the AVAudioPlayer volume still only equates to the already set 50% ringer. The limitations of using this method speak for themselves."

I edited again my post to show how I manage some songs with AVQueuePlayer. This part of code is working.

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];
    }
1
  • Add setvolume flag with slider value, I have not tested in that way but might be solved your problem . Best Luck Nov 28, 2013 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.