SetVolume does not work on AVMutableAudioMixInputParameters with playback from the local iPod library library

I'm currently trying to play music from the local iPod library using AVPlayer, not MPMusicPlayerController using the application controller.

I can get him to select a track from the local iPod lib and MPMediaQuery, but when I try to play the sound at a lower level using AVMutableAudioMixInputParameters, it doesn't seem to lower the volume at all.

The reason I use AVPlayer rather than MPMusicPlayerController is because I play some other sound in the background at the same time, and MPMusicPlayerController cannot play as an existing audio file using AVPlayer. The code that I have at the moment is:

// just blindly select all music for now.
MPMediaQuery *everything = [[MPMediaQuery alloc] init];

NSArray *itemsFromGenericQuery = [everything items];
MPMediaItem *song;
NSMutableArray *array = [[NSMutableArray alloc] init];

for (song in itemsFromGenericQuery) 
{
    [array addObject:song];
}

, .

, AVPlayer:

// grab the first song off the array
MPMediaItem *song = [array objectAtIndex:0];
NSURL *assetURL = [song valueForProperty:MPMediaItemPropertyAssetURL];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:assetURL options:nil];

NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio];
AVAssetTrack *track = [audioTracks objectAtIndex:0];

NSMutableArray *allAudioParams = [NSMutableArray array];
AVMutableAudioMixInputParameters *audioInputParams = 
        [AVMutableAudioMixInputParameters audioMixInputParameters];

// this is the key line - turn the volume down.
[audioInputParams setVolume:0.3 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];
[playerItem seekToTime:CMTimeMake(30, 1)];

[self setLocalPlayer:[AVPlayer playerWithPlayerItem:playerItem]];
[localPlayer play];

, AVPlayer URL- (); .

, ; . , , .

!

.

+5
4

AVMutableAudioMix , .

+1

():

NSString *songName = [NSString stringWithFormat:@"http://xx.xx.xx.xx/%d%@", bitrate, url];
playerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:songName]];

NSArray *audioTracks = [[playerItem asset] tracks];

AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:audioTracks[0]];
[audioInputParams setVolumeRampFromStartVolume:0.0 toEndVolume:1.0 timeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(0.0, 1.0), CMTimeMakeWithSeconds(3.0, 1.0))];

AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
[audioMix setInputParameters:@[audioInputParams]];

[playerItem setAudioMix:audioMix];

AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];

:

- (void)fadeOut {
    seconds = CMTimeGetSeconds(playerItem.currentTime);

    NSArray *audioTracks = [[playerItem asset] tracks];

    AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:audioTracks[0]];
    [audioInputParams setVolumeRampFromStartVolume:1.0 toEndVolume:0.0 timeRange:CMTimeRangeMake(CMTimeMakeWithSeconds(seconds, 1.0), CMTimeMakeWithSeconds(3.0, 1.0))];

    AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
    [audioMix setInputParameters:@[audioInputParams]];

    [playerItem setAudioMix:audioMix];
}
+1

Have you tried replacing kCMTimeZero with CMTimeMakeWithSeconds (0,1) or just 0?

If this still fails, try the fade in / out effect and set the start and end time to 0.

0
source

Try using a negative time value, for example, CMTimeMake(-1, 1)instead kCMTimeZero.

0
source

All Articles