AvAudioSession不能在iOS7中工作?

#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]; } 

我在网上做了很多研究,我认为这应该是在后台播放audio的正确方法。 然而,这只是不成功。 我不知道哪里出了问题。 请帮忙。 谢谢。

 [[AVAudioSession sharedInstance] setActive:NO error:nil]; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil]; [[AVAudioSession sharedInstance] setActive: YES error: nil]; 

如果你想在应用程序在后台播放audio,你应该使用类别播放,而不是playAndRecord。 另外,您需要在项目 – >目标 – >function – > backgroundMode中检查Audio和airPlay

查找UIBackgroundModes。 您需要告诉系统您的应用程序将在后台播放声音。

 #define STREAM @"http://" 

我相信http在你的#define中是不正确的。 尝试给它

 #define STREAM (@"file://")