#import "SctreamAudioViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "SecondViewController.h"
@interface SctreamAudioViewController ()
@property (nonatomic, strong) AVPlayer *player;
@end
@implementation SctreamAudioViewController
#define STREAM @"http://localhost/audio.mp3"
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
[audioSession setActive:YES error:nil];
NSURL *url = [[NSURL alloc] initWithString:STREAM];
self.player = [[AVPlayer alloc] initWithURL:url];
}
- (IBAction)play:(id)sender {
[self.player play];
}
I do quite a lot of researches on the net and I think this should be the right way to play audio in background. However, it just doesn't work out. I have no idea where it goes wrong. Please help. Thanks.