From 3e8437ae559487362fae3525beb79c534c213a51 Mon Sep 17 00:00:00 2001 From: 单军华 Date: Thu, 12 Jul 2018 13:44:34 +0800 Subject: [PATCH] bug修复和功能优化 --- screendisplay/Pods/EaseUI/EaseUI/EMUIKit/3rdparty/DeviceHelper/internal/EMAudioPlayerUtil.m | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 157 insertions(+), 0 deletions(-) diff --git a/screendisplay/Pods/EaseUI/EaseUI/EMUIKit/3rdparty/DeviceHelper/internal/EMAudioPlayerUtil.m b/screendisplay/Pods/EaseUI/EaseUI/EMUIKit/3rdparty/DeviceHelper/internal/EMAudioPlayerUtil.m new file mode 100755 index 0000000..ca9221f --- /dev/null +++ b/screendisplay/Pods/EaseUI/EaseUI/EMUIKit/3rdparty/DeviceHelper/internal/EMAudioPlayerUtil.m @@ -0,0 +1,157 @@ +/************************************************************ + * * Hyphenate CONFIDENTIAL + * __________________ + * Copyright (C) 2016 Hyphenate Inc. All rights reserved. + * + * NOTICE: All information contained herein is, and remains + * the property of Hyphenate Inc. + * Dissemination of this information or reproduction of this material + * is strictly forbidden unless prior written permission is obtained + * from Hyphenate Inc. + */ + +#import "EMAudioPlayerUtil.h" +#import <AVFoundation/AVFoundation.h> +#import "EaseLocalDefine.h" + +static EMAudioPlayerUtil *audioPlayerUtil = nil; + +@interface EMAudioPlayerUtil () <AVAudioPlayerDelegate> { + AVAudioPlayer *_player; + void (^playFinish)(NSError *error); +} + +@end + +@implementation EMAudioPlayerUtil + +#pragma mark - public ++ (BOOL)isPlaying{ + return [[EMAudioPlayerUtil sharedInstance] isPlaying]; +} + ++ (NSString *)playingFilePath{ + return [[EMAudioPlayerUtil sharedInstance] playingFilePath]; +} + ++ (void)asyncPlayingWithPath:(NSString *)aFilePath + completion:(void(^)(NSError *error))completon{ + [[EMAudioPlayerUtil sharedInstance] asyncPlayingWithPath:aFilePath + completion:completon]; +} + ++ (void)stopCurrentPlaying{ + [[EMAudioPlayerUtil sharedInstance] stopCurrentPlaying]; +} + + +#pragma mark - private ++ (EMAudioPlayerUtil *)sharedInstance{ + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + audioPlayerUtil = [[self alloc] init]; + }); + + return audioPlayerUtil; +} + +- (BOOL)isPlaying +{ + return !!_player; +} + +// Get the path of what is currently being played +- (NSString *)playingFilePath +{ + NSString *path = nil; + if (_player && _player.isPlaying) { + path = _player.url.path; + } + + return path; +} + +- (void)asyncPlayingWithPath:(NSString *)aFilePath + completion:(void(^)(NSError *error))completon{ + playFinish = completon; + NSError *error = nil; + NSFileManager *fm = [NSFileManager defaultManager]; + if (![fm fileExistsAtPath:aFilePath]) { + error = [NSError errorWithDomain:NSEaseLocalizedString(@"error.notFound", @"File path not exist") + code:-1 + userInfo:nil]; + if (playFinish) { + playFinish(error); + } + playFinish = nil; + + return; + } + + NSURL *wavUrl = [[NSURL alloc] initFileURLWithPath:aFilePath]; + _player = [[AVAudioPlayer alloc] initWithContentsOfURL:wavUrl error:&error]; + if (error || !_player) { + _player = nil; + error = [NSError errorWithDomain:NSEaseLocalizedString(@"error.initPlayerFail", @"Failed to initialize AVAudioPlayer") + code:-1 + userInfo:nil]; + if (playFinish) { + playFinish(error); + } + playFinish = nil; + return; + } + + _player.delegate = self; + [_player prepareToPlay]; + [_player play]; +} + +- (void)stopCurrentPlaying{ + if(_player){ + _player.delegate = nil; + [_player stop]; + _player = nil; + } + if (playFinish) { + playFinish = nil; + } +} + +- (void)dealloc{ + if (_player) { + _player.delegate = nil; + [_player stop]; + _player = nil; + } + playFinish = nil; +} + +#pragma mark - AVAudioPlayerDelegate +- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player + successfully:(BOOL)flag{ + if (playFinish) { + playFinish(nil); + } + if (_player) { + _player.delegate = nil; + _player = nil; + } + playFinish = nil; +} + +- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player + error:(NSError *)error{ + if (playFinish) { + NSError *error = [NSError errorWithDomain:NSEaseLocalizedString(@"error.palyFail", @"Play failure") + code:-1 + userInfo:nil]; + playFinish(error); + } + if (_player) { + _player.delegate = nil; + _player = nil; + } +} + +@end -- Gitblit v1.8.0