2013-07-22 203 views
1

嗨我有一个使用定位服务留在后台的计步器应用程序,现在我需要每1000步发出一声嘟嘟声。当应用程序在后台播放音频或声音

我想这样玩:

AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil]; 

NSURL *audioFileLocationURL = [[NSBundle mainBundle] URLForResource:@"beep" withExtension:@"mp3"]; 

success = [audioSession setActive:YES error:nil]; 
AVPlayerItem * item = [[AVPlayerItem alloc] initWithURL:audioFileLocationURL]; 
queuePlayer = [[AVQueuePlayer alloc] initWithItems:@[item]]; 

[queuePlayer play]; 

但它不玩,如果我运行此代码,而应用程序是在前台它不玩,但我运行它,而它的在背景中它什么都不做。

有什么想法?

谢谢!

+0

此代码是否正在使用'UIBackgroundTaskIdentifier'执行,还是正在后台线程上运行? –

+0

UIBackgroundTaskIdentifier – Ecarrion

回答

1

我仍然不明白为什么,但我管理它的工作。

我所要做的就是在应用程序处于活动状态时稍微播放音乐(无声音),然后暂停播放。

这样做后,如果我在后台恢复播放,一切正常。