Skip to content

Commit

Permalink
Stop audio in iOS when backgrounded.
Browse files Browse the repository at this point in the history
- Fixes a crash when a sound is still playing and Artboard is de-allocated. Easy to cause with long sounds as sound resources would get nuked and the engine would keep trying to decode from a dead buffer.
- Stops audio when going in background, resumes when coming to foreground.

The way I did the backgrounding is on the RiveRendererView as I couldn't find a better general spot to do this but maybe @mjtalbot has an idea.

This does not stop long sounds (like songs) when exiting a view, we can address that separately...

Diffs=
4a9947630 Stop audio in iOS when backgrounded. (#7055)

Co-authored-by: Luigi Rosso <luigi-rosso@users.noreply.github.com>
  • Loading branch information
luigi-rosso and luigi-rosso committed Apr 16, 2024
1 parent e824aa8 commit ff8648e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cb2ea5b2d31eb6888a96cb25524392087bdf47c7
4a9947630d3848ff70813cf5e2d8718adb9a1b90
2 changes: 1 addition & 1 deletion .rive_renderer
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0eeb44647783f7ad0fc3607c9f7be76bce9fdea4
ecf23d0e1921e3d8318e2c9a75f18911aa4daaf4
29 changes: 29 additions & 0 deletions Source/Renderer/rive_renderer_view.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,36 @@
#import "RivePrivateHeaders.h"
#import <RenderContext.h>
#import <RenderContextManager.h>
// We manually need to provide this as our build-time config isn't shared with xcode.
#define WITH_RIVE_AUDIO
#include "rive/audio/audio_engine.hpp"

@implementation RiveRendererView
{
RenderContext* _renderContext;
rive::Renderer* _renderer;
}

- (void)didEnterBackground:(NSNotification*)notification
{
rive::AudioEngine::RuntimeEngine()->stop();
}

- (void)didEnterForeground:(NSNotification*)notification
{
rive::AudioEngine::RuntimeEngine()->start();
}

- (instancetype)initWithCoder:(NSCoder*)decoder
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didEnterForeground:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
self = [super initWithCoder:decoder];

_renderContext = [[RenderContextManager shared] getDefaultContext];
Expand All @@ -36,6 +57,14 @@ - (instancetype)initWithCoder:(NSCoder*)decoder

- (instancetype)initWithFrame:(CGRect)frameRect
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didEnterForeground:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
_renderContext = [[RenderContextManager shared] getDefaultContext];
assert(_renderContext);

Expand Down
2 changes: 1 addition & 1 deletion submodules/rive-cpp

1 comment on commit ff8648e

@rlods
Copy link

@rlods rlods commented on ff8648e May 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change may be the cause of this issue

Other music apps having their music stops when a new app is launched and displayed a Rive component

Is there any plan to fix?

Thanks in advance

Please sign in to comment.