簡體   English   中英

iOS流式背景音頻

[英]iOS Streaming background audio

以下是找到音頻流的有效代碼:

    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,應用播放音頻。 這沒有解決我的問題。

我開始放置這個:

[[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

編輯:有人嗎?

我不知道它是什么,但是我只是將所有代碼轉移到一個新項目中,添加了此代碼

 - (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設置,似乎可以在手機上使用,但不能在模擬器上使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM