Search code examples
iosmpmovieplayercontroller

How to make a radio app keep playing in the background of my iOS app?


I am developing an app that streams a radio station. The view contains a button, which after being pressed calls the MPMoviePlayerViewController. (See screenshots here and here).

The IBAction for the play button looks like:

- (IBAction)Play:(id)sender {

    NSURL *url = [NSURL URLWithString:@"http://fm.radiokfor.com:8080"];
    MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc]initWithContentURL:url];


    [self presentViewController:playercontroller animated:NO completion:nil];

    playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;

    [playercontroller.moviePlayer play];

}

My question is: is there a way so that I could play the stream without having the MPMoviePlayerController on top of the View and allow me to navigate to other tabs? Is it possible to play the audio in the background only by clicking on the button?


Solution

  • I fixed it by using the following method:

    - (IBAction)Play:(id)sender
    {
    
        self.player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:@"http://fm.radiokfor.com:8080"]];
        [self.player play];
        self.radioStationLabel.text = @"Now playing: Radio KFOR";
    }
    
    - (IBAction)Stop:(id)sender
    {
    
        [self.player pause];
        self.radioStationLabel.text = @"Press 'Play' to start stream";
    }