2013-05-29 223 views
5

在特定时间我的应用程序中,我使用AVAudioPlayer提醒用户发出哔声。使用AVAudioPlayer播放声音而不停止iPod音乐

现在我想要这个声音播放,即使当应用程序在背景。所以我可以通过将AVAudioSession类别设置为AVAudioSessionCategoryPlayback来实现此目的,我也希望这种声音不应该阻止iPod音乐,我可以使用它设置AVAudioPlayer类别AVAudioSessionCategoryAmbient

但我希望他们在一起意味着声音也应该在后台播放,它也应该不会阻止iPod音乐。

那么我该如何做到这一点?

如果无法在背景中播放AVAudioSessionCategoryAmbient类别的声音,那么还有其他解决方法吗?

道歉,如果我做任何错误。

谢谢。

+0

pleae看看这个:-http://stackoverflow.com/questions/7619794/play-music-in-the-background-using-avaudioplayer –

+0

它不工作 – Unnati

+0

你可以没有停止iPod音乐(或任何其他后台播放应用程序)的背景播放。 – rckoenes

回答

4

您需要在您的project.plist文件Required background modes中添加一行,如下图所示并设置它。

enter image description here

,并把波纹管代码到你的项目的appdelegate didFinishLaunchingWithOptions

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil]; 
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 
    [[AVAudioSession sharedInstance] setActive: YES error: nil]; 
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 
    NSError *error; 
    BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error]; 
    if (!success) { 
     //Handle error 
     NSLog(@"%@", [error localizedDescription]); 
       } else { 
        // Yay! It worked! 
       } 

它的工作对我来说希望它有用我对你的FRND人是最好的。

UPDATE

你不能在同一时间运行AVAudioPlayer和iPod播放器或MPMusicPlayer或MPMoviePlayer,没有做更多的工作。如果您想要简单,请使用Audio Toolbox的系统声音。

请参阅此波纹管链接: -

iPhone AVAudioPlayer stopping background music

请检查波纹管连接有在后台播放音频文件的演示。请检查

http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_background-audio/

+0

当应用程序处于后台时,这不起作用。 – Unnati

+0

检查我编辑的答案,并检查演示 –

+0

它在后台工作,但它是停止iPod音乐。 – Unnati