简体   繁体   中英

How can I pause/resume a playing sound that was started with repeatActionForever

I have started playing a sound loop in the following way:

SKAction *soundAction = [SKAction playSoundFileNamed:@"walking.wav" waitForCompletion:YES];
[self runAction:[SKAction repeatActionForever:soundAction] withKey:@"walking"];

How can I pause/resume it immediately?

For things like background music or sounds you want more control over, apple docs recommend using AVFoundation instead of SKAction-based sounds.

There's a handful of workarounds if you don't want to do that. You can remove specific actions

[self removeActionForKey:@"walking"];

but then you'd either have to re-instantiate the initial action, or you could make it an iVar, to re-start it.

The other SpriteKit workaround would be to give it its own non-visual node, and have the node be an iVar or property. Then run your sound effect action on this node instead of on self, and to pause it, you can just pause the node.

node.paused = YES;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM