Skip to content

Instantly share code, notes, and snippets.

@manmal
Created April 24, 2012 11:42
Show Gist options
  • Save manmal/2478975 to your computer and use it in GitHub Desktop.
Save manmal/2478975 to your computer and use it in GitHub Desktop.
AVPlayer URL & volume Additions
#import "AVPlayer+MOAdditions.h"
#include <CoreMedia/CMBase.h>
#import "AVPlayerItem+MOAdditions.h"
@implementation AVPlayer (MOAdditions)
- (NSURL *)currentURL {
AVAsset *asset = self.currentItem.asset;
if ([asset isMemberOfClass:[AVURLAsset class]])
return ((AVURLAsset *)asset).URL;
return nil;
}
- (void)setVolume:(CGFloat)volume {
NSArray *audioTracks = [self.currentItem.asset tracksWithMediaType:AVMediaTypeAudio];
NSMutableArray *allAudioParams = [NSMutableArray array];
for (AVAssetTrack *track in audioTracks) {
AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParameters];
[audioInputParams setVolume:volume atTime:kCMTimeZero];
[audioInputParams setTrackID:[track trackID]];
[allAudioParams addObject:audioInputParams];
}
AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
[audioMix setInputParameters:allAudioParams];
[self.currentItem setAudioMix:audioMix];
}
@end
#import "AVPlayer+MOAdditions.h"
#include <CoreMedia/CMBase.h>
@implementation AVPlayer (MOAdditions)
- (NSURL *)currentURL {
AVAsset *asset = self.currentItem.asset;
if ([asset isMemberOfClass:[AVURLAsset class]])
return ((AVURLAsset *)asset).URL;
return nil;
}
- (void)setVolume:(CGFloat)volume {
NSArray *audioTracks = [self.currentItem.asset tracksWithMediaType:AVMediaTypeAudio];
NSMutableArray *allAudioParams = [NSMutableArray array];
for (AVAssetTrack *track in audioTracks) {
AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParameters];
[audioInputParams setVolume:volume atTime:kCMTimeZero];
[audioInputParams setTrackID:[track trackID]];
[allAudioParams addObject:audioInputParams];
}
AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
[audioMix setInputParameters:allAudioParams];
[self.currentItem setAudioMix:audioMix];
}
@end
@julian-weinert
Copy link

This won't work for live streaming. My tracks array is always empty...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment