【问题标题】:AvAudioSession won't work in iOS7?AvAudioSession 在 iOS7 中不起作用?
【发布时间】:2013-12-04 03:44:38
【问题描述】:
#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];
}

我在网上做了很多研究,我认为这应该是在后台播放音频的正确方法。但是,它只是行不通。我不知道哪里出了问题。请帮忙。谢谢。

【问题讨论】:

  • 您确定localhost 可以访问吗?你在运行http服务器吗?您是否正确配置并启动它?

标签: ios avplayer avaudiosession


【解决方案1】:
[[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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 如何?您的意思是在此 plist 中的“所需背景模式”下添加“应用播放音频”?
    【解决方案3】:
    #define STREAM @"http://"
    

    我认为 http 在您的#define 中不正确。尝试给它作为

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-11
      • 2014-01-24
      • 2013-09-30
      • 2014-03-03
      • 2013-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多