2013-01-08 110 views
0

以下是工作得找得到音頻流代碼:的iOS流背景音響

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 

的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

編輯:有人嗎?

回答

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中的設置,它似乎在手機上運行,​​但不模擬器..