ios - AvAudioSession 在 iOS7 中不能工作?

标签 ios avplayer avaudiosession

#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/

相关文章:

ios - iOS Xcode 10 的 Flutter 构建失败

swift - 如何在其他 View 中使用 Swift 中的 AVPlayer 暂停和播放音乐

objective-c - 启用静音开关后如何在 iOS 上播放声音

ios - 在 iOS 设备上检测事件的 AVAudioSession

ios - 如何在完成 block 之前解决嵌套的异步调用

ios - 为什么查询(DBAccess)第一次在 Id 属性中返回 nil?

ios - 将数据从 ViewController 传递到 AppDelegate

ios - 使用 AVPlayer 的全功能音乐播放器

ios - Swift:视频以一种尺寸录制,但以错误的尺寸呈现

ios - 无法删除我的 ios 应用程序(完全)