SpectrumAnalyzerSample icon indicating copy to clipboard operation
SpectrumAnalyzerSample copied to clipboard

nice work.

Open abidullah opened this issue 10 years ago • 11 comments

how to add the settings for live streaming

abidullah avatar Dec 23 '14 16:12 abidullah

Which settings do you want?

666tos avatar Dec 23 '14 16:12 666tos

i want to play music from online stream rather than a local mp3

abidullah avatar Dec 23 '14 16:12 abidullah

Well, in my project code that plays music from given URL looks like this:

 - (void)preloadMixWithUrl:(NSUrl*)url
{
    self.player = nil;

    NSURL *URL = [self.class URLForMix:self.mix];
    if (url == nil) 
    {
        return;
    }

    self.playerItem = [AVPlayerItem playerItemWithURL:URL];
    self.player = [AVPlayer playerWithPlayerItem:self.playerItem];

    [self setupAudioSession];
}

- (void)setupAudioSession
{
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];

    NSError *error = nil;
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:&error];

    if (error)
    {
    }

    [audioSession setActive:YES error:&error];

    if (error)
    {
    }
}

- (void)setPlayer:(AVPlayer *)player
{
    if (_player == player)
    {
        return;
    }

    if (_player)
    {
        [_player pause];
        [_player.currentItem removeObserver:self forKeyPath:AVPlayerTracksKeyPath];
        [_player removeObserver:self forKeyPath:AVPlayerStatusKeyPath];
        [_player removeTimeObserver:self.playerTimeObserver];
    }

    _player = player;

    self.playerIsReady = NO;

    if (_player == nil)
    {
        return;
    }

    __weak id *weakSelf = self;

    self.playerTimeObserver =
    [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 2) // 0.5 seconds
                                          queue:NULL
                                     usingBlock:^(CMTime time)
    {
        [weakSelf updateProgress];
    }];

    [self.player addObserver:self forKeyPath:AVPlayerStatusKeyPath options:kNilOptions context:NULL];
    [self.player.currentItem addObserver:self forKeyPath:AVPlayerTracksKeyPath options:kNilOptions context:NULL];

    self.player.actionAtItemEnd = AVPlayerActionAtItemEndPause;
}

- (void)beginRecordingAudioFromTrack:(AVAssetTrack *)audioTrack
{
    // Configure an MTAudioProcessingTap to handle things.
    MTAudioProcessingTapRef tap;
    MTAudioProcessingTapCallbacks callbacks;
    callbacks.version = kMTAudioProcessingTapCallbacksVersion_0;
    callbacks.clientInfo = (__bridge void *)(self);
    callbacks.init = init;
    callbacks.prepare = prepare;
    callbacks.process = process;
    callbacks.unprepare = unprepare;
    callbacks.finalize = finalize;

    OSStatus err = MTAudioProcessingTapCreate(kCFAllocatorDefault,
                                          &callbacks,
                                          kMTAudioProcessingTapCreationFlag_PostEffects,
                                          &tap);

    if(err)
    {
        return;
    }

    // Create an AudioMix and assign it to our currently playing "item", which
    // is just the stream itself.
    AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
    AVMutableAudioMixInputParameters *inputParams = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:audioTrack];
    inputParams.audioTapProcessor = tap;
    audioMix.inputParameters = @[inputParams];
    self.player.currentItem.audioMix = audioMix;
}

#pragma mark - NSKeyValueObserving

- (void)observeValueForKeyPath:(NSString *)keyPath
                  ofObject:(id)object
                    change:(NSDictionary *)change
                   context:(void *)context
{
    if ([keyPath isEqualToString:AVPlayerStatusKeyPath])
    {
        switch (self.player.status)
        {
            case AVPlayerStatusUnknown:
            case AVPlayerStatusFailed:
            {
               //deal with error
                break;
            }

            case AVPlayerStatusReadyToPlay:
            {

            }

            default:
                break;
        }
    }
    else if ([keyPath isEqualToString:AVPlayerTracksKeyPath])
    {
        if (self.player.currentItem.tracks.count > 0)
        {
            self.playerIsReady = YES;

            AVURLAsset *asset = (AVURLAsset *)self.playerItem.asset;

            NSArray *tracks = [asset tracksWithMediaType:AVMediaTypeAudio];
            if (tracks.count > 0)
            {
                AVAssetTrack *audioTrack = tracks[0];

                [self beginRecordingAudioFromTrack:audioTrack];
            }

            self.nowPlayingInfoCenter.playbackDuration = self.duration;

            if (_paused == NO)
            {
                [self.player play];
            }
        }
    }
}

666tos avatar Dec 23 '14 16:12 666tos

where do i add these codes on which file, is it possible to help me via teamviewer

abidullah avatar Dec 23 '14 16:12 abidullah

That is meant to be in NIAudioManager, sorry, can not help via teamviewer

666tos avatar Dec 23 '14 16:12 666tos

i get lots of errors

abidullah avatar Dec 23 '14 16:12 abidullah

if u want my team viewer id and pass

id 288 162 237 pass 9442

abidullah avatar Dec 23 '14 16:12 abidullah

Ah, wait, all necessary code is there, just try to pass URL of stream.

NSURL *url = [NSURL URLWithString:@"http://music.myradio.ua/Jazz_news128.mp3"];
[NIAudioManager defaultManager].fileToPlay = url;
[NIAudioManager defaultManager].paused = NO;

666tos avatar Dec 23 '14 16:12 666tos

these are the errors

Cannot initialize a variable of type '__weak id *' with an lvalue of type 'NIAudioManager *const __strong'

Property 'mix' not found on object of type 'NIAudioManager *'

abidullah avatar Dec 23 '14 17:12 abidullah

Dont' look on my code from first answers, that is basically NIAudioManager's prototype. Just add this: NSURL *url = [NSURL URLWithString:@"http://mixes.bassblog.pro/Promo_Audio_Podcast_005_mixed_by_Death_Addams.mp3"]; [NIAudioManager defaultManager].fileToPlay = url; [NIAudioManager defaultManager].paused = NO;

to NIViewController's -viewDidLoad.

BTW, there is some problem with my code: it does not work with real stream (like this - http://music.myradio.ua/Jazz_news128.mp3). But it works with remote files (like this - http://mixes.bassblog.pro/Promo_Audio_Podcast_005_mixed_by_Death_Addams.mp3)

666tos avatar Dec 23 '14 17:12 666tos

ya i too had the same, so it only works for mp3 files i guess

http://radionujoom.ddns.me:8000 this is my stream and i am trying to use it with this url

abidullah avatar Dec 23 '14 17:12 abidullah