停止闲置一段时间后,IOS Audio将无法工作

时间:2013-09-05 04:33:25

标签: iphone ios audio ios6 avaudioplayer

当我在运行开始时播放和停止音乐时我的应用程序工作正常,但是当我后来停止音乐并让应用程序闲置一段时间并单击播放按钮时,应用程序没有输出任何警告,甚至没有音乐播放我的音乐正在播放的日志输出。我不知道导致这种错误是什么,因为没有错。这是IOS模拟器中的常见行为吗?

这是我的音频代码以防万一。

-(void)musicIsPlaying:(id)sender
{
    AVAudioPlayer *theAudio = [sender objectForKey:@"audio"];
    UIButton *btn = [sender objectForKey:@"button"];
    if (btn.selected == TRUE) 
    {
        [btn setTitle:@"Stop" forState:UIControlStateNormal];
        [btn setSelected:FALSE];    
        if(theAudio.isPlaying == NO)
        {
            [theAudio prepareToPlay];
            [theAudio play];
            NSLog(@"music playing");
        }                                    
    }
    else
    {
        [btn setTitle:@"Play" forState:UIControlStateNormal];
        [btn setSelected:TRUE];
        if(theAudio.isPlaying==YES)
        {
            [theAudio stop];
            NSLog(@"music stop playing");
        }        
    }    
}

2 个答案:

答案 0 :(得分:0)

当应用程序变为活动状态时,您应该调用方法来恢复播放。所以,编写代码以恢复在Appdelegate中的游戏。

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

答案 1 :(得分:0)

    you need to Declare AVAudioPlayer in Interface

    @interface AudioViewController ()
    {
    AVAudioPlayer *theAudio;
    }
    @end

the use this "theAudio" object in your method(musicIsPlaying)..like 
theAudio = [sender objectForKey:@"audio"];
    Hope..it will work for you...