session-ios/SessionMessagingKit/Utilities/OWSAudioPlayer.m

232 lines
6.0 KiB
Mathematica
Raw Normal View History

//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
2018-02-23 21:44:46 +01:00
#import "OWSAudioPlayer.h"
2017-10-10 22:13:54 +02:00
#import <AVFoundation/AVFoundation.h>
2020-11-26 00:37:56 +01:00
#import <SessionUtilitiesKit/SessionUtilitiesKit.h>
#import <SessionMessagingKit/SessionMessagingKit-Swift.h>
2017-04-20 17:12:58 +02:00
NS_ASSUME_NONNULL_BEGIN
2018-02-23 21:44:46 +01:00
// A no-op delegate implementation to be used when we don't need a delegate.
@interface OWSAudioPlayerDelegateStub : NSObject <OWSAudioPlayerDelegate>
@property (nonatomic) AudioPlaybackState audioPlaybackState;
@end
#pragma mark -
@implementation OWSAudioPlayerDelegateStub
- (void)setAudioProgress:(CGFloat)progress duration:(CGFloat)duration
{
2020-11-26 00:37:56 +01:00
// Do nothing
}
- (void)showInvalidAudioFileAlert
{
// Do nothing
2018-02-23 21:44:46 +01:00
}
2021-01-29 01:46:32 +01:00
- (void)audioPlayerDidFinishPlaying:(OWSAudioPlayer *)player successfully:(BOOL)flag
{
// Do nothing
}
2018-02-23 21:44:46 +01:00
@end
#pragma mark -
@interface OWSAudioPlayer () <AVAudioPlayerDelegate>
@property (nonatomic, readonly) NSURL *mediaUrl;
@property (nonatomic, nullable) AVAudioPlayer *audioPlayer;
@property (nonatomic, nullable) NSTimer *audioPlayerPoller;
2018-10-23 16:40:09 +02:00
@property (nonatomic, readonly) OWSAudioActivity *audioActivity;
@end
#pragma mark -
2018-02-23 21:44:46 +01:00
@implementation OWSAudioPlayer
2018-02-23 21:44:46 +01:00
- (instancetype)initWithMediaUrl:(NSURL *)mediaUrl
2018-10-23 16:40:09 +02:00
audioBehavior:(OWSAudioBehavior)audioBehavior
2018-02-23 21:44:46 +01:00
{
2018-10-23 16:40:09 +02:00
return [self initWithMediaUrl:mediaUrl audioBehavior:audioBehavior delegate:[OWSAudioPlayerDelegateStub new]];
2018-02-23 21:44:46 +01:00
}
2018-10-23 16:40:09 +02:00
- (instancetype)initWithMediaUrl:(NSURL *)mediaUrl
audioBehavior:(OWSAudioBehavior)audioBehavior
delegate:(nullable id<OWSAudioPlayerDelegate>)delegate
{
self = [super init];
if (!self) {
return self;
}
_mediaUrl = mediaUrl;
2018-10-23 16:40:09 +02:00
_delegate = delegate;
2020-11-26 00:37:56 +01:00
NSString *audioActivityDescription = [NSString stringWithFormat:@"%@ %@", @"OWSAudioPlayer", self.mediaUrl];
2018-10-23 16:40:09 +02:00
_audioActivity = [[OWSAudioActivity alloc] initWithAudioDescription:audioActivityDescription behavior:audioBehavior];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidEnterBackground:)
name:OWSApplicationDidEnterBackgroundNotification
object:nil];
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[DeviceSleepManager.sharedInstance removeBlockWithBlockObject:self];
[self stop];
}
#pragma mark - Dependencies
- (OWSAudioSession *)audioSession
{
return SMKEnvironment.shared.audioSession;
}
#pragma mark
- (void)applicationDidEnterBackground:(NSNotification *)notification
{
[self stop];
}
#pragma mark - Methods
2021-01-29 01:46:32 +01:00
- (BOOL)isPlaying
{
2021-01-29 01:46:32 +01:00
return (self.delegate.audioPlaybackState == AudioPlaybackState_Playing);
}
2021-01-29 01:46:32 +01:00
- (void)play
{
2018-10-23 16:40:09 +02:00
// get current audio activity
[self playWithAudioActivity:self.audioActivity];
}
2018-10-23 16:40:09 +02:00
- (void)playWithAudioActivity:(OWSAudioActivity *)audioActivity
{
[self.audioPlayerPoller invalidate];
2017-10-10 22:13:54 +02:00
self.delegate.audioPlaybackState = AudioPlaybackState_Playing;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
if (!self.audioPlayer) {
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.mediaUrl error:&error];
2021-01-29 01:46:32 +01:00
self.audioPlayer.enableRate = YES;
if (error) {
[self stop];
if ([error.domain isEqualToString:NSOSStatusErrorDomain]
&& (error.code == kAudioFileInvalidFileError || error.code == kAudioFileStreamError_InvalidFile)) {
2020-11-26 00:37:56 +01:00
[self.delegate showInvalidAudioFileAlert];
}
return;
}
self.audioPlayer.delegate = self;
2018-02-23 21:44:46 +01:00
if (self.isLooping) {
self.audioPlayer.numberOfLoops = -1;
}
}
[self.audioPlayer play];
[self.audioPlayerPoller invalidate];
2020-10-01 06:24:00 +02:00
self.audioPlayerPoller = [NSTimer weakScheduledTimerWithTimeInterval:.05f
2017-04-20 21:45:52 +02:00
target:self
selector:@selector(audioPlayerUpdated:)
userInfo:nil
repeats:YES];
// Prevent device from sleeping while playing audio.
[DeviceSleepManager.sharedInstance addBlockWithBlockObject:self];
}
2020-10-19 02:21:04 +02:00
- (void)setCurrentTime:(NSTimeInterval)currentTime
{
[self.audioPlayer setCurrentTime:currentTime];
}
2021-01-29 01:46:32 +01:00
- (float)getPlaybackRate
{
return self.audioPlayer.rate;
}
- (NSTimeInterval)duration
{
return [self.audioPlayer duration];
}
2021-01-29 01:46:32 +01:00
- (void)setPlaybackRate:(float)rate
{
[self.audioPlayer setRate:rate];
}
- (void)pause
{
2017-10-10 22:13:54 +02:00
self.delegate.audioPlaybackState = AudioPlaybackState_Paused;
[self.audioPlayer pause];
[self.audioPlayerPoller invalidate];
2018-07-18 03:08:53 +02:00
[self.delegate setAudioProgress:(CGFloat)[self.audioPlayer currentTime] duration:(CGFloat)[self.audioPlayer duration]];
2018-10-23 01:17:05 +02:00
[self endAudioActivities];
[DeviceSleepManager.sharedInstance removeBlockWithBlockObject:self];
}
- (void)stop
{
2017-10-10 22:13:54 +02:00
self.delegate.audioPlaybackState = AudioPlaybackState_Stopped;
[self.audioPlayer pause];
[self.audioPlayerPoller invalidate];
[self.delegate setAudioProgress:0 duration:0];
2018-10-23 01:17:05 +02:00
[self endAudioActivities];
[DeviceSleepManager.sharedInstance removeBlockWithBlockObject:self];
}
2018-10-23 01:17:05 +02:00
- (void)endAudioActivities
{
2018-10-23 16:40:09 +02:00
[self.audioSession endAudioActivity:self.audioActivity];
2018-10-23 01:17:05 +02:00
}
2018-10-23 16:40:09 +02:00
- (void)togglePlayState
{
2021-01-29 01:46:32 +01:00
if (self.isPlaying) {
[self pause];
} else {
2018-10-23 16:40:09 +02:00
[self playWithAudioActivity:self.audioActivity];
}
}
#pragma mark - Events
- (void)audioPlayerUpdated:(NSTimer *)timer
{
2018-07-18 03:08:53 +02:00
[self.delegate setAudioProgress:(CGFloat)[self.audioPlayer currentTime] duration:(CGFloat)[self.audioPlayer duration]];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[self stop];
2021-01-29 01:46:32 +01:00
[self.delegate audioPlayerDidFinishPlaying:self successfully:flag];
}
@end
2017-04-20 17:12:58 +02:00
NS_ASSUME_NONNULL_END