2013-09-05 33 views
0

我希望我的应用程序音频在后台运行,并且我遵循this tutorial但它不起作用。当我按下home按钮时,我的应用程序音频仍然停止,我意识到它没有调用“applicationDidBecomeActive”或“applicationDidEnterBackground”(即使我禁用了“应用程序不在后台运行”的设置,问题仍然存在)。过去一周我一直在处理这个问题。IOS应用程序不会在后台运行

到目前为止,我已经做到了这步:

- 新增AVFoundation框架,并宣布

#import <AVFoundation/AVFoundation.h> 

enter image description here

- 设置了音频

NSString *audioName = [NSString stringWithFormat:@"audio%d", (nimages)]; 
     NSString *soundPath =[[NSBundle mainBundle]pathForResource:audioName ofType:@"mp3"]; 
     NSURL *soundURL = [NSURL fileURLWithPath:soundPath]; 
     NSError *error = nil; 
     AVAudioPlayer *audio = nil; 
     audio = [[AVAudioPlayer alloc]initWithContentsOfURL:soundURL error:&error]; 
     audio.numberOfLoops = -1; 
     audio.volume = 0.9; 
     if (error) { 
      NSLog(@"%@", [error localizedDescription]); 
      NSLog(@"Unable to load file");     
     }else { 
      //Make sure the system follows our playback status 
      [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 
      [[AVAudioSession sharedInstance] setActive: YES error: nil]; 
      //Load the audio into memory 
      [audio prepareToPlay]; 
     } 

- 添加行AVAudioSession in plist

enter image description here

更新时间:

我的应用程序是一个音频应用程序,用户可以播放特定的配乐我的应用程序,即使它进入后台。可能吗?

回答

0

我认为它必须积极播放音频才能发挥作用。如果您的应用程序不是一个真正的音频应用程序,您可以尝试将应用程序设置为VoIP;这不需要活动连接。它也不会阻止其他想要使用音频的应用程序。只需设置后台模式为“应用程序提供IP语音服务”,然后它将在后台运行长达十分钟;你可以连接VoIP套接字并将内容推送到它,如果你想保持它活着或唤醒。

+0

我觉得我的应用程序考虑音频应用程序,因为用户可以继续在后台监听我的应用程序音频音乐。 –

0

我建议你clicking here

阅读“苹果人机界面”存在其中应用程序被允许在后台运行有限的情况下,如(音频,下载,Update..etc),你可以找到它所有在我提供给你的链接。

0

使用它将使用它的运行代码。您可以使用NSURL的URLURUSWLetPath:[NSString stringWithFormat:@“%@/notification.wav”,[[NSBundle mainBundle] resourcePath]]];}

  NSError *error; 

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback误差:无];

  AudioSessionInitialize(NULL, NULL, NULL,NULL); 
      UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord; 
      AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory); 
      UInt32 value = YES; 
      AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(value), &value); 
      AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck, sizeof(value), &value); 
      [[AVAudioSession sharedInstance] setActive: YES error: nil]; 
      AudioSessionSetActive(true); 



      // self.appAudioPlayer=audioPlayer; 
      AVAudioPlayer *audioPlayer101 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; 
      self.audioPlayerForPlay.delegate = self; 
      self.audioPlayerForPlay.numberOfLoops=-1; 
      [self.audioPlayerForPlay prepareToPlay]; 
      UInt32 doChangeDefaultRoute = 1; 
      AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(doChangeDefaultRoute), &doChangeDefaultRoute); 





      self.audioPlayerForPlay = audioPlayer101; 
      [audioPlayer101 release]; 
      [self.audioPlayerForPlay play]; 
+0

我需要在哪里放置代码?心向我解释?我对此有点模糊。谢谢。 @Ankur –

+0

你在哪里在前台播放音频.. – Ankur

+0

它没有为我工作。 –