#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];
}
我在网上做了很多研究,我认为这应该是在后台播放音频的正确方法。但是,它就是行不通。我不知道哪里出了问题。请帮忙。谢谢。
最佳答案
[[AVAudioSession sharedInstance] setActive:NO error:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
如果你想在应用程序处于后台时播放音频,你应该使用类别 Playback 而不是 playAndRecord。 另外需要勾选project->target->Capabilities->backgroundMode中的Audio和airPlay
关于ios - AvAudioSession 在 iOS7 中不能工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20366392/