简体   繁体   English

iOS流式背景音频

[英]iOS Streaming background audio

The following is the code that works just find to get the audio to stream: 以下是找到音频流的有效代码:

    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];

When I attempt to take the app into background mode, i seizes to play. 当我尝试使应用进入后台模式时,我会抓住机会玩。 I have gone into the .plist and have entered Required background modes, item 0, App plays Audio. 我进入.plist并进入必填背景模式,项目0,应用播放音频。 This did not fix my problem. 这没有解决我的问题。

I began placing this: 我开始放置这个:

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

Inside of 代替

- (void)applicationWillResignActive:(UIApplication *)application

- (void)applicationDidEnterBackground:(UIApplication *)application

and

inside of the viewDidLoad in the main ViewController. 在主ViewController的viewDidLoad内部。 As well as right after [player prepareToPlay]. 以及在[player prepareToPlay]之后。

I am not sure if the issue is with me not setting the proper background settings or with the app cutting out the connection. 我不确定问题是否出在我设置的背景设置不正确或应用程序断开了连接。 Basically, I am not sure what I am missing. 基本上,我不确定我缺少什么。

I have been looking at: 我一直在看:

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

and

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

EDIT: anyone? 编辑:有人吗?

I have no idea what it was, but I just transferred all of code to a new project, added this code 我不知道它是什么,但是我只是将所有代码转移到一个新项目中,添加了此代码

 - (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.
}

and the plist settings, and it seems to work on the phone, but not the simulator.. 和plist设置,似乎可以在手机上使用,但不能在模拟器上使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM