0

I'm using AVPlayer for playing radio stream. For initializing player i'm using next code:

self.asset = [AVAsset assetWithURL:self.url];
self.item = [AVPlayerItem playerItemWithAsset:self.asset];
self.player = [AVPlayer playerWithPlayerItem:self.item];

I've read this question Adjusting the volume of a playing AVPlayer

and i'm trying to set volume with slider value

- (void)setVolume:(float )volume
{
    _volume = volume;
    NSArray *audioTracks = self.asset.tracks;
    NSMutableArray *allAudioParams = [NSMutableArray array];

    for (AVAssetTrack *track in audioTracks) {
        AVMutableAudioMixInputParameters *audioInputParams =
        [AVMutableAudioMixInputParameters audioMixInputParameters];
        [audioInputParams setVolume:volume atTime:kCMTimeZero];
        [allAudioParams addObject:audioInputParams];
        [audioInputParams setTrackID:[track trackID]];
    }

    AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
    [audioMix setInputParameters:allAudioParams];
    NSLog(@"%@", self.player.currentItem);
    NSLog(@"%f", volume);
    [self.player.currentItem setAudioMix:audioMix];
}

I'm also trying to set

[audioInputParams setVolume:volume atTime:self.player.currentTime];

but there are not any tracks in that array (self.asset.tracks), and its have not any effect on volume. (also i can't get any metadata), I can't use AVAudioPlayer for playing audio stream by url

An instance of the AVAudioPlayer class, called an audio player, provides playback of audio data from a file or memory.

Apple recommends that you use this class for audio playback unless you are playing audio captured from a network stream or require very low I/O latency.

I'm thinking about using that code https://github.com/DigitalDJ/AudioStreamer/downloads for playing radio stream, but i don't know how set volume with Audiotoolbox.

Please, help me! Thanks!

Community
  • 1
  • 1
BergP
  • 3,453
  • 4
  • 33
  • 58

1 Answers1

2

i have tried lots of solutions, but the best way to do that is to implement an instance ov MPVolumeVIew:

 self.myViewVolume = [[MPVolumeView alloc] initWithFrame:CGRectMake(20, 330, 280, 50)];
[self.myViewVolume sizeToFit];
[self.view addSubview:self.myViewVolume];

You can add this to any UIView you want, just change the class to MPVolumeView Do not forget to add MediaPlayer framework.

Here is what you have to do if you want to get metadata from your stream:

 [playerItem addObserver:self forKeyPath:@"timedMetadata" options:NSKeyValueObservingOptionNew context:NULL];

With this you can simply add the information to any UILabel

EmilDo
  • 1,177
  • 3
  • 16
  • 33
  • Could you tell me, what also i should know to add MPVolumeView to my view. I have some problems with it - when i add it in my view with this code, it isn't appears on my view. I can't understand why. Thanks – BergP Nov 26 '12 at 06:09
  • 1
    the slider does not appear in Simulator, MPVolumeView slider is linked to the hardware volume buttons, it only appears when you test it on iPhone/iPad. But apart from this it works great and the UI is the same as the other volume slider. – EmilDo Nov 26 '12 at 12:40
  • @Emk0DJ you saved my bacon with this! I tried everything so cheers! – Alex McPherson Sep 16 '13 at 19:30
  • You cannot use MPVolumeView from simulator. – Mashhadi Nov 25 '14 at 14:03