AvAudioSession在iOS7中不起作用? [英] AvAudioSession won't work in iOS7?

查看:63
本文介绍了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];
}

我在网络上进行了大量研究,我认为这应该是在后台播放音频的正确方法.但是,它根本无法解决.我不知道哪里出了问题.请帮忙.谢谢.

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.

推荐答案

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

如果要在应用程序在后台播放音频时,应使用播放"类别,而不是playAndRecord. 另外,您需要在项目->目标->功能-> backgroundMode中检查音频和airPlay

if you want to play audio when app is in the background, you should use category Playback instead of playAndRecord. Also, you need to check Audio and airPlay in project-> target-> Capabilities-> backgroundMode

这篇关于AvAudioSession在iOS7中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆