【问题标题】:iOS Streaming background audioiOS 流式传输背景音频
【发布时间】:2012-12-21 19:26:57
【问题描述】:

以下是刚刚找到的用于使音频流式传输的代码:

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:stringURL]];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    connectionPlay = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    NSError *playerError;
    player = [[AVAudioPlayer alloc] initWithData:streamData error:&playerError];
    player.numberOfLoops = 0;
    player.volume = 1.0f;
    [player prepareToPlay];

    if (playerError) {
        NSLog(@"audio player error: %@", [playerError localizedDescription]);
    }
    if (player == nil)
        NSLog(@"%@", [playerError description]);
    else
        [player play];

当我尝试将应用程序置于后台模式时,我会抓住播放。我已经进入 .plist 并进入了必需的背景模式,第 0 项,App 播放音频。这并没有解决我的问题。

我开始放置这个:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
                                       error:nil];
[[AVAudioSession sharedInstance] setActive:YES
                                     error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

里面

- (void)applicationWillResignActive:(UIApplication *)application

- (void)applicationDidEnterBackground:(UIApplication *)application

在主 ViewController 中的 viewDidLoad 内部。以及 [player prepareToPlay] 之后。

我不确定问题是我没有设置正确的背景设置还是应用程序切断了连接。基本上,我不确定我错过了什么。

我一直在看:

http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioSession_ClassReference/Reference/Reference.html

https://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/Introduction/Introduction.html

编辑:有人吗?

【问题讨论】:

    标签: ios stream streaming avaudioplayer


    【解决方案1】:

    我不知道它是什么,但我只是将所有代码转移到一个新项目中,添加了这段代码

     - (void)applicationWillResignActive:(UIApplication *)application
    {
    
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
                                               error:nil];
        [[AVAudioSession sharedInstance] setActive:YES
                                             error:nil];
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }
    

    还有plist设置,手机上好像可以,模拟器不行。。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 2013-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多