2

当我在运行开始时播放并停止音乐时,我的应用程序运行良好,但是当我后来停止音乐并让应用程序闲置一段时间并单击播放按钮时,应用程序没有输出任何警告并且没有音乐甚至我的音乐正在播放的日志输出也会播放。我不知道导致这种情况的错误是什么,因为没有什么是错的。这是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");
        }        
    }    
}
4

2 回答 2

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...
于 2013-09-05T12:22:04.130 回答
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.
}
于 2013-09-05T06:53:05.247 回答