AVAudioPlayer在5.0中运行良好,在4.0-4.3.5中发布

时间:2012-04-27 16:49:03

标签: ios ipad avaudioplayer

我有一个应用程序,可以在不同的ViewControllers中播放和控制音乐。为此,我在app委托的DidFinishLaunchingMethod中创建了两个AVAudioPLayer实例:

NSString *path = [[NSBundle mainBundle] pathForResource:@"minnie1" ofType:@"mp3"];
NSURL *path1 = [[NSURL alloc] initFileURLWithPath:path];
menuLoop =[[AVAudioPlayer alloc] initWithContentsOfURL:path1 error:NULL];
[menuLoop setDelegate:self];  
menuLoop.numberOfLoops = -1;
[menuLoop play];


NSString *path2 = [[NSBundle mainBundle] pathForResource:@"minnie2" ofType:@"mp3"];
NSURL *path3 = [[NSURL alloc] initFileURLWithPath:path2];
gameLoop=[[AVAudioPlayer alloc] initWithContentsOfURL:path3 error:NULL];
gameLoop.numberOfLoops = -1;
[gameLoop setDelegate:self];
[gameLoop prepareToPlay];

在此之后,我在各种viewControllers中调用它,使用如下代码停止或重启:

- (IBAction)playGameLoop{

NSLog(@"Begin playGameLoop");
FPAppDelegate *app = (FPAppDelegate *)[[UIApplication sharedApplication] delegate];

if ([FPAVManager audioEnabled] == NO){
    //DO NOTHING   
}
else {
    if (app.gameLoop.playing ==YES ) {
        //DO NOTHING
    }
    else {  [app.gameLoop play];
    NSLog(@"End playGameLoop");
    }
    }

音频文件第一次播放正常,并在被要求停止时停止播放。但是,在iOS4设备上,再次调用时它们不会重新开始播放。

谢谢!

1 个答案:

答案 0 :(得分:0)

来自文档:

Calling this method [stop] or allowing a sound to finish playing, 
undoes the setup performed upon calling the play or prepareToPlay 
methods.

The stop method does not reset the value of the currentTime property 
to 0. In other words, if you call stop during playback and then call
play, playback resumes at the point where it left off.

在您循环播放声音时,我希望能够拨打stop,然后拨打play。但我对自己遇到这种情况有着模糊的回忆。我发现调用pause而不是stop是一个更好的解决方案。特别是因为我可以再次致电play继续比赛。在致电stop之前,呼叫prepareToPlay似乎总是需要再次致电play,至少根据我的经验。

iOS4.x和iOS5之间的API实现也有可能发生一些变化。您应该检查开发人员网站上的API差异以确定。