Get an audio stream from URI and play it on iPhone

前端 未结 4 961
轻奢々
轻奢々 2020-12-08 05:24

I would like to get an audio stream from URL and play it on iPhone. Unfortunately, there is only C# example of how to play that stream, but I need a realization on Objective

相关标签:
4条回答
  • 2020-12-08 06:08

    Look into the AVPlayer class, part of the AVFoundation framework... this is the objective-c (iOS) class you use for streaming audio.

    0 讨论(0)
  • 2020-12-08 06:16

    Hey I faced this problem of audio streaming for a radio application. I had searched for the answers and they did not work. AVAudioPlayer is not the class to be used here. Downloading the contents of URL using NSData object is not the solution either as it downloads the contents then the code further executes. It is similar to downloading a mp3 and then playing it locally on a player.

    [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]]; //Downloads the contents of the url and in case of radio the url's content is unlimited. The program gets frozen on executing this.
    

    Instead use AVPlayer class.

    This code worked in my radio application

    AVPlayer *objAVPlayer = [[AVPlayer alloc] initWithURL:url];
    [objAVPlayer play];
    

    This code does not download the contents at the url but it plays the stream as it is received.

    0 讨论(0)
  • 2020-12-08 06:20
    MPMoviePlayerController *player;
    player = [[MPMoviePlayerController alloc] init] ;
    player.view.frame = CGRectMake(0, 0, 0, 0);
    player.movieSourceType = MPMovieSourceTypeStreaming;
    player.view.hidden = YES;
    [self.view addSubview: [wikiMusicPlayer view]];
    
    NSString *address = Your URL;
    address = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSURL *url = [NSURL URLWithString:address];
    [wikiMusicPlayer setContentURL:url];
    [wikiMusicPlayer prepareToPlay];
    [wikiMusicPlayer setShouldAutoplay: NO];
    [wikiMusicPlayer play];
    
    0 讨论(0)
  • 2020-12-08 06:25
    NSString* resourcePath = url; //your url
    NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]];
    NSError *error;
    
    app.audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
    app.audioPlayer.numberOfLoops = 0;
    app.audioPlayer.volume = 1.0f;
    [app.audioPlayer prepareToPlay];
    
    if (app.audioPlayer == nil)
        NSLog(@"%@", [error description]);
    else
        [app.audioPlayer play];
    
    0 讨论(0)
提交回复
热议问题