BergP
BergP

Reputation: 3545

How can i set volume of AVPLayer when it's playing radio stream

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!

Upvotes: 0

Views: 2022

Answers (1)

EmilDo
EmilDo

Reputation: 1177

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

Upvotes: 2

Related Questions