mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			205 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			Matlab
		
	
		
		
			
		
	
	
			205 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			Matlab
		
	
| 
								 
											9 years ago
										 
									 | 
							
								//
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								//  Copyright (c) 2018 Open Whisper Systems. All rights reserved.
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								//
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								#import "OWSAudioPlayer.h"
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								#import "TSAttachmentStream.h"
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								#import <AVFoundation/AVFoundation.h>
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								#import <SessionUtilitiesKit/SessionUtilitiesKit.h>
							 | 
						||
| 
								 | 
							
								#import <SessionMessagingKit/SessionMessagingKit-Swift.h>
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								NS_ASSUME_NONNULL_BEGIN
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								// 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
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    // Do nothing
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								- (void)showInvalidAudioFileAlert
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    // Do nothing
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								@end
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#pragma mark -
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								@interface OWSAudioPlayer () <AVAudioPlayerDelegate>
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								@property (nonatomic, readonly) NSURL *mediaUrl;
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								@property (nonatomic, nullable) AVAudioPlayer *audioPlayer;
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								@property (nonatomic, nullable) NSTimer *audioPlayerPoller;
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								@property (nonatomic, readonly) OWSAudioActivity *audioActivity;
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								@end
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#pragma mark -
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								@implementation OWSAudioPlayer
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								- (instancetype)initWithMediaUrl:(NSURL *)mediaUrl
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								                   audioBehavior:(OWSAudioBehavior)audioBehavior
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    return [self initWithMediaUrl:mediaUrl audioBehavior:audioBehavior delegate:[OWSAudioPlayerDelegateStub new]];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								- (instancetype)initWithMediaUrl:(NSURL *)mediaUrl
							 | 
						||
| 
								 | 
							
								                        audioBehavior:(OWSAudioBehavior)audioBehavior
							 | 
						||
| 
								 | 
							
								                        delegate:(id<OWSAudioPlayerDelegate>)delegate
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 | 
							
								    self = [super init];
							 | 
						||
| 
								 | 
							
								    if (!self) {
							 | 
						||
| 
								 | 
							
								        return self;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    _mediaUrl = mediaUrl;
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    _delegate = delegate;
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    NSString *audioActivityDescription = [NSString stringWithFormat:@"%@ %@", @"OWSAudioPlayer", self.mediaUrl];
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    _audioActivity = [[OWSAudioActivity alloc] initWithAudioDescription:audioActivityDescription behavior:audioBehavior];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								    [[NSNotificationCenter defaultCenter] addObserver:self
							 | 
						||
| 
								 | 
							
								                                             selector:@selector(applicationDidEnterBackground:)
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								                                                 name:OWSApplicationDidEnterBackgroundNotification
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								                                               object:nil];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return self;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								- (void)dealloc
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    [[NSNotificationCenter defaultCenter] removeObserver:self];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    [DeviceSleepManager.sharedInstance removeBlockWithBlockObject:self];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								    [self stop];
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								#pragma mark - Dependencies
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								- (OWSAudioSession *)audioSession
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    return Environment.shared.audioSession;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#pragma mark
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								- (void)applicationDidEnterBackground:(NSNotification *)notification
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    [self stop];
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#pragma mark - Methods
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								- (void)play
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    // get current audio activity
							 | 
						||
| 
								 | 
							
								    [self playWithAudioActivity:self.audioActivity];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								- (void)playWithAudioActivity:(OWSAudioActivity *)audioActivity
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								    [self.audioPlayerPoller invalidate];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    self.delegate.audioPlaybackState = AudioPlaybackState_Playing;
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								    if (!self.audioPlayer) {
							 | 
						||
| 
								 | 
							
								        NSError *error;
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								        self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:self.mediaUrl error:&error];
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								        if (error) {
							 | 
						||
| 
								 | 
							
								            [self stop];
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								            if ([error.domain isEqualToString:NSOSStatusErrorDomain]
							 | 
						||
| 
								 | 
							
								                && (error.code == kAudioFileInvalidFileError || error.code == kAudioFileStreamError_InvalidFile)) {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								                [self.delegate showInvalidAudioFileAlert];
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								            }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								            return;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        self.audioPlayer.delegate = self;
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								        if (self.isLooping) {
							 | 
						||
| 
								 | 
							
								            self.audioPlayer.numberOfLoops = -1;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    [self.audioPlayer play];
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								    [self.audioPlayerPoller invalidate];
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    self.audioPlayerPoller = [NSTimer weakScheduledTimerWithTimeInterval:.05f
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								                                                                  target:self
							 | 
						||
| 
								 | 
							
								                                                                selector:@selector(audioPlayerUpdated:)
							 | 
						||
| 
								 | 
							
								                                                                userInfo:nil
							 | 
						||
| 
								 | 
							
								                                                                 repeats:YES];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								    // Prevent device from sleeping while playing audio.
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    [DeviceSleepManager.sharedInstance addBlockWithBlockObject:self];
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								- (void)setCurrentTime:(NSTimeInterval)currentTime
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    [self.audioPlayer setCurrentTime:currentTime];
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								- (void)pause
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    self.delegate.audioPlaybackState = AudioPlaybackState_Paused;
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								    [self.audioPlayer pause];
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								    [self.audioPlayerPoller invalidate];
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    [self.delegate setAudioProgress:(CGFloat)[self.audioPlayer currentTime] duration:(CGFloat)[self.audioPlayer duration]];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    [self endAudioActivities];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    [DeviceSleepManager.sharedInstance removeBlockWithBlockObject:self];
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								- (void)stop
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    self.delegate.audioPlaybackState = AudioPlaybackState_Stopped;
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								    [self.audioPlayer pause];
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								    [self.audioPlayerPoller invalidate];
							 | 
						||
| 
								 | 
							
								    [self.delegate setAudioProgress:0 duration:0];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    [self endAudioActivities];
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    [DeviceSleepManager.sharedInstance removeBlockWithBlockObject:self];
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								- (void)endAudioActivities
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    [self.audioSession endAudioActivity:self.audioActivity];
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								- (void)togglePlayState
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 
											8 years ago
										 
									 | 
							
								    if (self.delegate.audioPlaybackState == AudioPlaybackState_Playing) {
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								        [self pause];
							 | 
						||
| 
								 | 
							
								    } else {
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								        [self playWithAudioActivity:self.audioActivity];
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#pragma mark - Events
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								- (void)audioPlayerUpdated:(NSTimer *)timer
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 
											7 years ago
										 
									 | 
							
								    [self.delegate setAudioProgress:(CGFloat)[self.audioPlayer currentTime] duration:(CGFloat)[self.audioPlayer duration]];
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    [self stop];
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								@end
							 | 
						||
| 
								 
											9 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								NS_ASSUME_NONNULL_END
							 |