Commit 7de32c40 cgx

使用系统AVPlayer解决播放点击卡顿问题

1 个父辈 4583f834
...@@ -58,6 +58,8 @@ ...@@ -58,6 +58,8 @@
D051032E282A4E9200560A0D /* instant_eternity.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = D051032C282A4E9200560A0D /* instant_eternity.mp3 */; }; D051032E282A4E9200560A0D /* instant_eternity.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = D051032C282A4E9200560A0D /* instant_eternity.mp3 */; };
D051032F282A4E9200560A0D /* fse.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = D051032D282A4E9200560A0D /* fse.mp3 */; }; D051032F282A4E9200560A0D /* fse.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = D051032D282A4E9200560A0D /* fse.mp3 */; };
D053BEE9280F9E310028CCFC /* CloseAlertView.m in Sources */ = {isa = PBXBuildFile; fileRef = D053BEE8280F9E310028CCFC /* CloseAlertView.m */; }; D053BEE9280F9E310028CCFC /* CloseAlertView.m in Sources */ = {isa = PBXBuildFile; fileRef = D053BEE8280F9E310028CCFC /* CloseAlertView.m */; };
D054CCB52830DE5600420DC4 /* NoisePlayerManager.m in Sources */ = {isa = PBXBuildFile; fileRef = D054CCB42830DE5600420DC4 /* NoisePlayerManager.m */; };
D054CCB82830E3AC00420DC4 /* NoisePlayItem.m in Sources */ = {isa = PBXBuildFile; fileRef = D054CCB72830E3AC00420DC4 /* NoisePlayItem.m */; };
D055BEB928250D3400BC11A4 /* CourseCollectionCell.m in Sources */ = {isa = PBXBuildFile; fileRef = D055BEB828250D3400BC11A4 /* CourseCollectionCell.m */; }; D055BEB928250D3400BC11A4 /* CourseCollectionCell.m in Sources */ = {isa = PBXBuildFile; fileRef = D055BEB828250D3400BC11A4 /* CourseCollectionCell.m */; };
D055BEBC28250E8F00BC11A4 /* CourseModel.m in Sources */ = {isa = PBXBuildFile; fileRef = D055BEBB28250E8F00BC11A4 /* CourseModel.m */; }; D055BEBC28250E8F00BC11A4 /* CourseModel.m in Sources */ = {isa = PBXBuildFile; fileRef = D055BEBB28250E8F00BC11A4 /* CourseModel.m */; };
D055BEBF2825267C00BC11A4 /* DSBaseViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = D055BEBE2825267C00BC11A4 /* DSBaseViewController.m */; }; D055BEBF2825267C00BC11A4 /* DSBaseViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = D055BEBE2825267C00BC11A4 /* DSBaseViewController.m */; };
...@@ -122,7 +124,6 @@ ...@@ -122,7 +124,6 @@
D0A75108282E2A8500589B0E /* NoisePlayBar.m in Sources */ = {isa = PBXBuildFile; fileRef = D0A75107282E2A8500589B0E /* NoisePlayBar.m */; }; D0A75108282E2A8500589B0E /* NoisePlayBar.m in Sources */ = {isa = PBXBuildFile; fileRef = D0A75107282E2A8500589B0E /* NoisePlayBar.m */; };
D0A7510E282E2FEF00589B0E /* NoisePlayView.m in Sources */ = {isa = PBXBuildFile; fileRef = D0A7510D282E2FEF00589B0E /* NoisePlayView.m */; }; D0A7510E282E2FEF00589B0E /* NoisePlayView.m in Sources */ = {isa = PBXBuildFile; fileRef = D0A7510D282E2FEF00589B0E /* NoisePlayView.m */; };
D0A75111282E3D0D00589B0E /* NoisePlayCell.m in Sources */ = {isa = PBXBuildFile; fileRef = D0A75110282E3D0D00589B0E /* NoisePlayCell.m */; }; D0A75111282E3D0D00589B0E /* NoisePlayCell.m in Sources */ = {isa = PBXBuildFile; fileRef = D0A75110282E3D0D00589B0E /* NoisePlayCell.m */; };
D0A75115282E499200589B0E /* NoiseDataManager.m in Sources */ = {isa = PBXBuildFile; fileRef = D0A75114282E499200589B0E /* NoiseDataManager.m */; };
D0AE1E3528281B6F008CEF27 /* TimerProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = D0AE1E3428281B6F008CEF27 /* TimerProxy.m */; }; D0AE1E3528281B6F008CEF27 /* TimerProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = D0AE1E3428281B6F008CEF27 /* TimerProxy.m */; };
D0AEFE79281781CF00230DC6 /* MyFeedModel.m in Sources */ = {isa = PBXBuildFile; fileRef = D0AEFE78281781CF00230DC6 /* MyFeedModel.m */; }; D0AEFE79281781CF00230DC6 /* MyFeedModel.m in Sources */ = {isa = PBXBuildFile; fileRef = D0AEFE78281781CF00230DC6 /* MyFeedModel.m */; };
D0AEFE7C2817D13400230DC6 /* UITableViewCell+CardRadius.m in Sources */ = {isa = PBXBuildFile; fileRef = D0AEFE7A2817D13400230DC6 /* UITableViewCell+CardRadius.m */; }; D0AEFE7C2817D13400230DC6 /* UITableViewCell+CardRadius.m in Sources */ = {isa = PBXBuildFile; fileRef = D0AEFE7A2817D13400230DC6 /* UITableViewCell+CardRadius.m */; };
...@@ -279,6 +280,10 @@ ...@@ -279,6 +280,10 @@
D051032D282A4E9200560A0D /* fse.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = fse.mp3; sourceTree = "<group>"; }; D051032D282A4E9200560A0D /* fse.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = fse.mp3; sourceTree = "<group>"; };
D053BEE7280F9E310028CCFC /* CloseAlertView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CloseAlertView.h; sourceTree = "<group>"; }; D053BEE7280F9E310028CCFC /* CloseAlertView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CloseAlertView.h; sourceTree = "<group>"; };
D053BEE8280F9E310028CCFC /* CloseAlertView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CloseAlertView.m; sourceTree = "<group>"; }; D053BEE8280F9E310028CCFC /* CloseAlertView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CloseAlertView.m; sourceTree = "<group>"; };
D054CCB32830DE5600420DC4 /* NoisePlayerManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NoisePlayerManager.h; sourceTree = "<group>"; };
D054CCB42830DE5600420DC4 /* NoisePlayerManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NoisePlayerManager.m; sourceTree = "<group>"; };
D054CCB62830E3AC00420DC4 /* NoisePlayItem.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NoisePlayItem.h; sourceTree = "<group>"; };
D054CCB72830E3AC00420DC4 /* NoisePlayItem.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NoisePlayItem.m; sourceTree = "<group>"; };
D055BEB728250D3400BC11A4 /* CourseCollectionCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CourseCollectionCell.h; sourceTree = "<group>"; }; D055BEB728250D3400BC11A4 /* CourseCollectionCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CourseCollectionCell.h; sourceTree = "<group>"; };
D055BEB828250D3400BC11A4 /* CourseCollectionCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CourseCollectionCell.m; sourceTree = "<group>"; }; D055BEB828250D3400BC11A4 /* CourseCollectionCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CourseCollectionCell.m; sourceTree = "<group>"; };
D055BEBA28250E8F00BC11A4 /* CourseModel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CourseModel.h; sourceTree = "<group>"; }; D055BEBA28250E8F00BC11A4 /* CourseModel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CourseModel.h; sourceTree = "<group>"; };
...@@ -410,8 +415,6 @@ ...@@ -410,8 +415,6 @@
D0A7510F282E3D0D00589B0E /* NoisePlayCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NoisePlayCell.h; sourceTree = "<group>"; }; D0A7510F282E3D0D00589B0E /* NoisePlayCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NoisePlayCell.h; sourceTree = "<group>"; };
D0A75110282E3D0D00589B0E /* NoisePlayCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NoisePlayCell.m; sourceTree = "<group>"; }; D0A75110282E3D0D00589B0E /* NoisePlayCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NoisePlayCell.m; sourceTree = "<group>"; };
D0A75112282E493D00589B0E /* Singleton.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Singleton.h; sourceTree = "<group>"; }; D0A75112282E493D00589B0E /* Singleton.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Singleton.h; sourceTree = "<group>"; };
D0A75113282E499200589B0E /* NoiseDataManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NoiseDataManager.h; sourceTree = "<group>"; };
D0A75114282E499200589B0E /* NoiseDataManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NoiseDataManager.m; sourceTree = "<group>"; };
D0AE1E3328281B6F008CEF27 /* TimerProxy.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TimerProxy.h; sourceTree = "<group>"; }; D0AE1E3328281B6F008CEF27 /* TimerProxy.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TimerProxy.h; sourceTree = "<group>"; };
D0AE1E3428281B6F008CEF27 /* TimerProxy.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TimerProxy.m; sourceTree = "<group>"; }; D0AE1E3428281B6F008CEF27 /* TimerProxy.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TimerProxy.m; sourceTree = "<group>"; };
D0AEFE77281781CF00230DC6 /* MyFeedModel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MyFeedModel.h; sourceTree = "<group>"; }; D0AEFE77281781CF00230DC6 /* MyFeedModel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MyFeedModel.h; sourceTree = "<group>"; };
...@@ -1137,12 +1140,14 @@ ...@@ -1137,12 +1140,14 @@
D0D20C42282F68EE00C9374D /* Model */ = { D0D20C42282F68EE00C9374D /* Model */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
D054CCB62830E3AC00420DC4 /* NoisePlayItem.h */,
D054CCB72830E3AC00420DC4 /* NoisePlayItem.m */,
D01BD426282C003900BA86B3 /* NoiseTypeModel.h */, D01BD426282C003900BA86B3 /* NoiseTypeModel.h */,
D01BD427282C003900BA86B3 /* NoiseTypeModel.m */, D01BD427282C003900BA86B3 /* NoiseTypeModel.m */,
D01BD429282C03FC00BA86B3 /* NoiseAudioModel.h */, D01BD429282C03FC00BA86B3 /* NoiseAudioModel.h */,
D01BD42A282C03FC00BA86B3 /* NoiseAudioModel.m */, D01BD42A282C03FC00BA86B3 /* NoiseAudioModel.m */,
D0A75113282E499200589B0E /* NoiseDataManager.h */, D054CCB32830DE5600420DC4 /* NoisePlayerManager.h */,
D0A75114282E499200589B0E /* NoiseDataManager.m */, D054CCB42830DE5600420DC4 /* NoisePlayerManager.m */,
); );
path = Model; path = Model;
sourceTree = "<group>"; sourceTree = "<group>";
...@@ -1622,6 +1627,7 @@ ...@@ -1622,6 +1627,7 @@
D01C9D7028168BFC00163567 /* NSDate+Extension.m in Sources */, D01C9D7028168BFC00163567 /* NSDate+Extension.m in Sources */,
D0F9AC582826602400FD7A3B /* SPButton.m in Sources */, D0F9AC582826602400FD7A3B /* SPButton.m in Sources */,
D0DB33A528204E7C00E15914 /* FirstLeadAlertView.m in Sources */, D0DB33A528204E7C00E15914 /* FirstLeadAlertView.m in Sources */,
D054CCB82830E3AC00420DC4 /* NoisePlayItem.m in Sources */,
D0C50B3027FD1BEB00DC68F0 /* PrivacyView.m in Sources */, D0C50B3027FD1BEB00DC68F0 /* PrivacyView.m in Sources */,
D0B5ECD527F2F0B2003EDFE3 /* AdaptationUtil.m in Sources */, D0B5ECD527F2F0B2003EDFE3 /* AdaptationUtil.m in Sources */,
D070509A28071BAF006D72E1 /* DSNetworkTool.m in Sources */, D070509A28071BAF006D72E1 /* DSNetworkTool.m in Sources */,
...@@ -1680,6 +1686,7 @@ ...@@ -1680,6 +1686,7 @@
D0F9AC542826563400FD7A3B /* MusicPlayerController.m in Sources */, D0F9AC542826563400FD7A3B /* MusicPlayerController.m in Sources */,
D0A72E3C282B56E600EED7BE /* GoodSleepSoundCell.m in Sources */, D0A72E3C282B56E600EED7BE /* GoodSleepSoundCell.m in Sources */,
D0506B1528051ED400229278 /* SafeSleepModel.m in Sources */, D0506B1528051ED400229278 /* SafeSleepModel.m in Sources */,
D054CCB52830DE5600420DC4 /* NoisePlayerManager.m in Sources */,
D0874BBF2816E61000CECB3C /* RescuePlanView.m in Sources */, D0874BBF2816E61000CECB3C /* RescuePlanView.m in Sources */,
D07DACA42810557D0067A1BF /* CGGeometry+RSKImageCropper.m in Sources */, D07DACA42810557D0067A1BF /* CGGeometry+RSKImageCropper.m in Sources */,
D09D0E96280D3FE9008DEDAB /* NSDate+Extras.m in Sources */, D09D0E96280D3FE9008DEDAB /* NSDate+Extras.m in Sources */,
...@@ -1715,7 +1722,6 @@ ...@@ -1715,7 +1722,6 @@
D0B5ECC827F2E97A003EDFE3 /* MacroFuncUtil.m in Sources */, D0B5ECC827F2E97A003EDFE3 /* MacroFuncUtil.m in Sources */,
D055BEB928250D3400BC11A4 /* CourseCollectionCell.m in Sources */, D055BEB928250D3400BC11A4 /* CourseCollectionCell.m in Sources */,
D01814E227FFDBB800583D4E /* HomeHeaderView.m in Sources */, D01814E227FFDBB800583D4E /* HomeHeaderView.m in Sources */,
D0A75115282E499200589B0E /* NoiseDataManager.m in Sources */,
D055BEBC28250E8F00BC11A4 /* CourseModel.m in Sources */, D055BEBC28250E8F00BC11A4 /* CourseModel.m in Sources */,
D09BF33A280E94E300E5F06C /* LoginUtils.m in Sources */, D09BF33A280E94E300E5F06C /* LoginUtils.m in Sources */,
D0F808F52803D4E70097899F /* Track.m in Sources */, D0F808F52803D4E70097899F /* Track.m in Sources */,
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
#import "SafeSleepRequestModel.h" #import "SafeSleepRequestModel.h"
#import "WhiteNoiseRequestModel.h" #import "WhiteNoiseRequestModel.h"
#import "NoisePlayBar.h" #import "NoisePlayBar.h"
#import "NoiseDataManager.h" #import "NoisePlayerManager.h"
@interface HomeViewController () @interface HomeViewController ()
@property (nonatomic, strong) DSHomeView *homeTV; @property (nonatomic, strong) DSHomeView *homeTV;
...@@ -62,7 +62,7 @@ ...@@ -62,7 +62,7 @@
- (void)viewDidDisappear:(BOOL)animated { - (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated]; [super viewDidDisappear:animated];
[[NoiseDataManager sharedNoiseDataManager] stopNoiseAudio]; [[NoisePlayerManager sharedNoisePlayerManager] stopAll];
} }
- (void)dealloc { - (void)dealloc {
......
...@@ -7,8 +7,7 @@ ...@@ -7,8 +7,7 @@
#import "NoiseListController.h" #import "NoiseListController.h"
#import "WhiteNoiseRequestModel.h" #import "WhiteNoiseRequestModel.h"
#import "NoiseAudioCell.h" #import "NoisePlayerManager.h"
#import "NoiseDataManager.h"
@interface NoiseListController () <UICollectionViewDelegate, UICollectionViewDataSource> @interface NoiseListController () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *noiseAudioView; @property (nonatomic, strong) UICollectionView *noiseAudioView;
...@@ -44,11 +43,7 @@ ...@@ -44,11 +43,7 @@
self.noiseAudioArr = requestModel.noiseAudioArr; self.noiseAudioArr = requestModel.noiseAudioArr;
[self.noiseAudioView reloadData]; [self.noiseAudioView reloadData];
// 重置 // 重置
NSArray *playList = [NoiseDataManager sharedNoiseDataManager].playList; [[NoisePlayerManager sharedNoisePlayerManager] removeAllNoiseAudioCell];
for (NoiseAudioCell *audioCell in playList) {
[audioCell.model.audioStream stop];
}
[NoiseDataManager sharedNoiseDataManager].playList = @[];
} else { } else {
self.exceptionView.hidden = NO; self.exceptionView.hidden = NO;
} }
...@@ -85,31 +80,20 @@ ...@@ -85,31 +80,20 @@
NoiseAudioModel *model = [self.noiseAudioArr objectAtIndex:indexPath.row]; NoiseAudioModel *model = [self.noiseAudioArr objectAtIndex:indexPath.row];
NoiseAudioCell *cell = (NoiseAudioCell *)[collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]]; NoiseAudioCell *cell = (NoiseAudioCell *)[collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
NSArray *playList = [NoiseDataManager sharedNoiseDataManager].playList;
NSMutableArray *selectedDataArr = [NSMutableArray arrayWithArray:playList];
// 白噪音未被选择则添加到播放列表 // 白噪音未被选择则添加到播放列表
if (cell.audioIV.selected == NO) { if (cell.audioIV.selected == NO) {
if (selectedDataArr.count >= 8) { if ([NoisePlayerManager sharedNoisePlayerManager].playCellList.count >= 8) {
[DSProgressHUD showToast:@"音频最多同时播放8个"]; [DSProgressHUD showToast:@"音频最多同时播放8个"];
return; return;
} }
NoisePlayItem *playItem = [[NoisePlayItem alloc] initWithUrl:model.audio_url volume:.5];
// 添加播放器 cell.playItem = playItem;
FSAudioStream *audioStream = [[FSAudioStream alloc] init]; [[NoisePlayerManager sharedNoisePlayerManager] addNoiseAudioCell:cell];
audioStream.strictContentTypeChecking = NO;
audioStream.defaultContentType = @"audio/mpeg";
audioStream.volume = .5;
model.audioStream = audioStream;
[selectedDataArr addObject:cell];
playList = [selectedDataArr copy];
} else { } else {
// 白噪音已经选择了则从播放列表移除 cell.playItem = nil;
if (model.audioStream) { [model.audioStream stop]; } [[NoisePlayerManager sharedNoisePlayerManager] removeNoiseAudioCell:cell];
[selectedDataArr removeObject:cell];
} }
cell.audioIV.selected = !cell.audioIV.selected; cell.audioIV.selected = !cell.audioIV.selected;
[NoiseDataManager sharedNoiseDataManager].playList = [selectedDataArr copy];
} }
#pragma mark - lazy #pragma mark - lazy
......
...@@ -6,8 +6,6 @@ ...@@ -6,8 +6,6 @@
// //
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import <FSAudioController.h>
#import "NoiseDataManager.h"
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
...@@ -25,9 +23,6 @@ NS_ASSUME_NONNULL_BEGIN ...@@ -25,9 +23,6 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, copy) NSString *audio_url; @property (nonatomic, copy) NSString *audio_url;
// 是否推荐(默认0:不推荐,1:推荐) // 是否推荐(默认0:不推荐,1:推荐)
@property (nonatomic, copy) NSString *is_recommend; @property (nonatomic, copy) NSString *is_recommend;
/// 播放器
@property (nonatomic, strong) FSAudioStream *audioStream;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END
//
// NoiseDataManager.h
// DreamSleep
//
// Created by peter on 2022/5/13.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
/// 白噪音音频数据共享单例对象
@interface NoiseDataManager : NSObject
SingletonH(NoiseDataManager)
/// 选中的白噪音数据
@property (nonatomic, strong) NSArray *playList;
/// 停止所有白噪音播放
- (void)stopNoiseAudio;
@end
NS_ASSUME_NONNULL_END
//
// NoiseDataManager.m
// DreamSleep
//
// Created by peter on 2022/5/13.
//
#import "NoiseDataManager.h"
#import "NoiseAudioCell.h"
@implementation NoiseDataManager
SingletonM(NoiseDataManager)
- (void)setPlayList:(NSArray *)playList {
_playList = playList;
[[NSNotificationCenter defaultCenter] postNotificationName:NoisePlaylistHasChange object:nil userInfo:@{@"playList":self.playList}];
}
- (void)stopNoiseAudio {
if (self.playList && self.playList.count) {
for (int i = 0; i < self.playList.count; i++) {
NoiseAudioCell *audioCell = self.playList[i];
[audioCell.model.audioStream stop];
}
}
}
@end
//
// NoisePlayItem.h
// DreamSleep
//
// Created by peter on 2022/5/15.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
/// 白噪音播放对象
@interface NoisePlayItem : NSObject
/// 播放状态(YES:正在播放中...,NO:未播放)
@property (nonatomic, assign) BOOL isPlaying;
/// 是否循环播放
@property (nonatomic, assign) BOOL isLoopPlayback;
/// 是否无缝循环播放
@property (nonatomic, assign) BOOL isSeamlessLoopPlayback;
/// 音频资源链接
@property (nonatomic, copy) NSString *url;
/// 音频音量大小
@property (nonatomic, assign) float volume;
- (instancetype)initWithUrl:(NSString *)url volume:(float)volume;
- (void)play;
- (void)pause;
- (void)stop;
@end
NS_ASSUME_NONNULL_END
//
// NoisePlayItem.m
// DreamSleep
//
// Created by peter on 2022/5/15.
//
#import "NoisePlayItem.h"
#import <AVFoundation/AVFoundation.h>
#import "NoisePlayerManager.h"
@interface NoisePlayItem ()
/// 系统播放器
@property (nonatomic, strong) AVPlayer *player;
/// 无缝循环播放辅助播放器
@property (nonatomic, strong) AVPlayer *assistPlayer;
@end
@implementation NoisePlayItem
- (instancetype)initWithUrl:(NSString *)url volume:(float)volume {
if (self = [super init]) {
self.url = url;
self.volume = volume;
}
return self;
}
- (void)setVolume:(float)volume {
_volume = volume;
self.player.volume = volume;
}
- (void)play {
if (self.player.timeControlStatus != AVPlayerTimeControlStatusPlaying) {
[self.player play];
}
}
- (void)pause {
if (self.player.timeControlStatus == AVPlayerTimeControlStatusPlaying) {
[self.player pause];
}
}
- (void)stop {
if (self.player.timeControlStatus == AVPlayerTimeControlStatusPlaying) {
[self.player pause];
[self.player seekToTime:CMTimeMake(0, 1)];
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"status"]) {
DSLog(@"changechange:%@", change);
AVPlayerStatus status = [[change objectForKey:@"new"] intValue];
if (status == AVPlayerStatusReadyToPlay) {
self.isPlaying = YES;
[NoisePlayerManager sharedNoisePlayerManager].isPlaying = self.isPlaying;
} else if (status == AVPlayerStatusFailed) {
self.isPlaying = NO;
DSLog(@"AVPlayerStatusFailed");
} else {
self.isPlaying = NO;
DSLog(@"AVPlayerStatusUnknown");
}
} else if ([keyPath isEqualToString:@"loadedTimeRanges"]) {
DSLog(@"change:%@", change);
}
}
- (void)playbackFinished:(NSNotification *)notification {
DSLog(@"音频播放完成通知:%@", notification.object);
AVPlayerItem *playerItem = [notification object];
[playerItem seekToTime:kCMTimeZero completionHandler:nil];
[self.player play];
// if ([notification.object isEqualToString:@"major"]) {
// self.isPlaying = NO;
// AVPlayerItem *playerItem = [notification object];
// [playerItem seekToTime:kCMTimeZero completionHandler:nil];
// if (self.isLoopPlayback) {
// [self.player play];
// }
// if (self.isSeamlessLoopPlayback) {
// [self.assistPlayer play];
// }
// }
}
- (void)dealloc {
[_player.currentItem removeObserver:self forKeyPath:@"status" context:nil];
[_player.currentItem removeObserver:self forKeyPath:@"loadedTimeRanges" context:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
}
#pragma mark - lazy
- (AVPlayer *)player {
if (!_player) {
_player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:self.url]];
[_player.currentItem addObserver:self forKeyPath:@"status" options:(NSKeyValueObservingOptionNew) context:nil];
[_player.currentItem addObserver:self forKeyPath:@"loadedTimeRanges" options:(NSKeyValueObservingOptionNew) context:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:_player.currentItem];
}
return _player;
}
@end
//
// NoisePlayerManager.h
// DreamSleep
//
// Created by peter on 2022/5/15.
//
#import <Foundation/Foundation.h>
#import "NoiseAudioCell.h"
NS_ASSUME_NONNULL_BEGIN
/// 白噪音音频播放管理器
@interface NoisePlayerManager : NSObject
SingletonH(NoisePlayerManager)
/// 播放列表,存放被选中的cell对象
@property (nonatomic, strong, readonly) NSArray<NoiseAudioCell *> *playCellList;
/// 播放状态(所有播放列表中只要有1个在播放则为YES,全部暂停为NO)
@property (nonatomic, assign) BOOL isPlaying;
/// 一键播放
- (void)playAll;
/// 一键暂停
- (void)pauseAll;
/// 一键停止
- (void)stopAll;
- (BOOL)addNoiseAudioCell:(NoiseAudioCell *)cell;
- (BOOL)removeNoiseAudioCell:(NoiseAudioCell *)cell;
- (void)removeAllNoiseAudioCell;
@end
NS_ASSUME_NONNULL_END
//
// NoisePlayerManager.m
// DreamSleep
//
// Created by peter on 2022/5/15.
//
#import "NoisePlayerManager.h"
@interface NoisePlayerManager ()
/// 临时操作使用的可变数组
@property (nonatomic, strong) NSMutableArray *tmpArry;
@end
@implementation NoisePlayerManager
SingletonM(NoisePlayerManager)
- (void)setIsPlaying:(BOOL)isPlaying {
_isPlaying = isPlaying;
[[NSNotificationCenter defaultCenter] postNotificationName:@"playStatusNoti" object:nil userInfo:@{@"isPlaying":@(self.isPlaying)}];
}
- (NSMutableArray *)tmpArry {
if (!_tmpArry) {
_tmpArry = [NSMutableArray array];
}
return _tmpArry;
}
- (NSArray<NoiseAudioCell *> *)playCellList {
return [self.tmpArry copy];
}
- (void)playAll {
BOOL isPlaying = NO;
for (NoiseAudioCell *cell in self.playCellList) {
NoisePlayItem *item = cell.playItem;
isPlaying = item.isPlaying;
[item play];
}
self.isPlaying = isPlaying;
}
- (void)pauseAll {
for (NoiseAudioCell *cell in self.playCellList) {
NoisePlayItem *item = cell.playItem;
[item pause];
}
self.isPlaying = NO;
}
- (void)stopAll {
for (NoiseAudioCell *cell in self.playCellList) {
cell.audioIV.selected = NO;
NoisePlayItem *item = cell.playItem;
[item stop];
}
self.isPlaying = NO;
}
- (BOOL)addNoiseAudioCell:(NoiseAudioCell *)cell {
[self.tmpArry addObject:cell];
[cell.playItem play];
[[NSNotificationCenter defaultCenter] postNotificationName:NoisePlaylistHasChange object:nil];
return YES;
}
- (BOOL)removeNoiseAudioCell:(NoiseAudioCell *)cell {
if (![self.tmpArry containsObject:cell]) { return NO; }
[cell.playItem stop];
[self.tmpArry removeObject:cell];
[[NSNotificationCenter defaultCenter] postNotificationName:NoisePlaylistHasChange object:nil];
return YES;
}
- (void)removeAllNoiseAudioCell {
[self stopAll];
[self.tmpArry removeAllObjects];
[[NSNotificationCenter defaultCenter] postNotificationName:NoisePlaylistHasChange object:nil];
}
@end
...@@ -7,8 +7,7 @@ ...@@ -7,8 +7,7 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#import "NoiseAudioModel.h" #import "NoiseAudioModel.h"
#import "NoisePlayItem.h"
NS_ASSUME_NONNULL_BEGIN
/// 白噪音小音频cell /// 白噪音小音频cell
@interface NoiseAudioCell : UICollectionViewCell @interface NoiseAudioCell : UICollectionViewCell
...@@ -16,6 +15,6 @@ NS_ASSUME_NONNULL_BEGIN ...@@ -16,6 +15,6 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, strong) UIButton *audioIV; @property (nonatomic, strong) UIButton *audioIV;
/// 数据model /// 数据model
@property (nonatomic, strong) NoiseAudioModel *model; @property (nonatomic, strong) NoiseAudioModel *model;
/// 播放器
@property (nonatomic, strong) NoisePlayItem *playItem;
@end @end
NS_ASSUME_NONNULL_END
...@@ -7,8 +7,8 @@ ...@@ -7,8 +7,8 @@
#import "NoisePlayBar.h" #import "NoisePlayBar.h"
#import "NoisePlayView.h" #import "NoisePlayView.h"
#import "NoiseAudioCell.h"
#import "TimingView.h" #import "TimingView.h"
#import "NoisePlayerManager.h"
@interface NoisePlayBar () @interface NoisePlayBar ()
@property (nonatomic, strong) UIView *bgView; @property (nonatomic, strong) UIView *bgView;
...@@ -66,6 +66,8 @@ ...@@ -66,6 +66,8 @@
// 添加对单例对象白噪音播放列表变化通知 // 添加对单例对象白噪音播放列表变化通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noisePlaylistHasChange:) name:NoisePlaylistHasChange object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noisePlaylistHasChange:) name:NoisePlaylistHasChange object:nil];
// 监听播放按钮状态通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playStatusNoti:) name:@"playStatusNoti" object:nil];
// 监听定时按钮被点击事件通知 // 监听定时按钮被点击事件通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(openTimingView) name:NoiseTimingDidClick object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(openTimingView) name:NoiseTimingDidClick object:nil];
} }
...@@ -74,6 +76,7 @@ ...@@ -74,6 +76,7 @@
- (void)dealloc { - (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:NoisePlaylistHasChange object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:NoisePlaylistHasChange object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"playStatusNoti" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NoiseTimingDidClick object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:NoiseTimingDidClick object:nil];
[_timer invalidate]; [_timer invalidate];
...@@ -84,29 +87,19 @@ ...@@ -84,29 +87,19 @@
#pragma mark - noti #pragma mark - noti
- (void)noisePlaylistHasChange:(NSNotification *)noti { - (void)noisePlaylistHasChange:(NSNotification *)noti {
NSArray *playList = noti.userInfo[@"playList"]; NSArray *playList = [NoisePlayerManager sharedNoisePlayerManager].playCellList;
if (playList) {
WS(weakSelf);
self.hidden = !(playList.count > 0); self.hidden = !(playList.count > 0);
NSMutableString *mStr = [NSMutableString string]; NSMutableString *mStr = [NSMutableString string];
for (int i = 0; i < playList.count; i++) { for (int i = 0; i < playList.count; i++) {
NoiseAudioCell *audioCell = playList[i]; NoiseAudioCell *audioCell = playList[i];
__weak FSAudioStream *audioStream = audioCell.model.audioStream;
audioStream.onStateChange = ^(FSAudioStreamState state) {
weakSelf.playBtn.selected = state == kFsAudioStreamPlaying;
if (self->_noisePlayView) {
weakSelf.noisePlayView.selected = weakSelf.playBtn.selected;
}
// 如果播放完成则重复播放
if (state == kFsAudioStreamPlaybackCompleted) {
[audioStream play];
}
};
[mStr appendFormat:@"%@%@", audioCell.model.audio_name, (i == playList.count - 1) ? @"" : @","]; [mStr appendFormat:@"%@%@", audioCell.model.audio_name, (i == playList.count - 1) ? @"" : @","];
[audioCell.model.audioStream playFromURL:[NSURL URLWithString:audioCell.model.audio_url]]; self.playBtn.selected = [NoisePlayerManager sharedNoisePlayerManager].isPlaying;
} }
self.audioLab.text = [mStr copy]; self.audioLab.text = [mStr copy];
} }
- (void)playStatusNoti:(NSNotification *)noti {
self.playBtn.selected = [NoisePlayerManager sharedNoisePlayerManager].isPlaying;
} }
- (void)openTimingView { - (void)openTimingView {
...@@ -116,13 +109,10 @@ ...@@ -116,13 +109,10 @@
#pragma mark - Actions #pragma mark - Actions
- (void)playNoiseAudio:(UIButton *)sender { - (void)playNoiseAudio:(UIButton *)sender {
sender.selected = !sender.selected; sender.selected = !sender.selected;
NSArray *playList = [NoiseDataManager sharedNoiseDataManager].playList;
for (int i = 0; i < playList.count; i++) {
NoiseAudioCell *audioCell = playList[i];
[audioCell.model.audioStream stop];
if (sender.selected) { if (sender.selected) {
[audioCell.model.audioStream playFromURL:[NSURL URLWithString:audioCell.model.audio_url]]; [[NoisePlayerManager sharedNoisePlayerManager] playAll];
} } else {
[[NoisePlayerManager sharedNoisePlayerManager] pauseAll];
} }
} }
...@@ -194,15 +184,7 @@ ...@@ -194,15 +184,7 @@
// 暂停播放 // 暂停播放
if (weakSelf.countTime == 0) { if (weakSelf.countTime == 0) {
[weakSelf.timer setFireDate:[NSDate distantFuture]]; [weakSelf.timer setFireDate:[NSDate distantFuture]];
NSArray *playList = [NoiseDataManager sharedNoiseDataManager].playList; [[NoisePlayerManager sharedNoisePlayerManager] pauseAll];
if (playList) {
for (NoiseAudioCell *audioCell in playList) {
FSAudioStream *audioStream = audioCell.model.audioStream;
if (audioStream.isPlaying) {
[audioStream pause];
}
}
}
weakSelf.timeLab.text = @""; weakSelf.timeLab.text = @"";
return; return;
} }
......
...@@ -22,6 +22,7 @@ NS_ASSUME_NONNULL_BEGIN ...@@ -22,6 +22,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface NoisePlayCell : UITableViewCell @interface NoisePlayCell : UITableViewCell
@property (nonatomic, weak) id<NoisePlayCellDelegate> delegate; @property (nonatomic, weak) id<NoisePlayCellDelegate> delegate;
@property (nonatomic, strong) NoiseAudioModel *model; @property (nonatomic, strong) NoiseAudioModel *model;
@property (nonatomic, assign) float volumn;
@property (nonatomic, strong) NSIndexPath *indexPath; @property (nonatomic, strong) NSIndexPath *indexPath;
@end @end
......
...@@ -72,7 +72,11 @@ ...@@ -72,7 +72,11 @@
[self.audioIcon yy_setImageWithURL:[NSURL URLWithString:model.audio_pic] placeholder:[UIImage imageNamed:@"basicPlaceholder"]]; [self.audioIcon yy_setImageWithURL:[NSURL URLWithString:model.audio_pic] placeholder:[UIImage imageNamed:@"basicPlaceholder"]];
self.nameLab.text = model.audio_name; self.nameLab.text = model.audio_name;
self.volumeSlider.value = model.audioStream.volume; }
- (void)setVolumn:(float)volumn {
_volumn = volumn;
self.volumeSlider.value = volumn;
} }
- (void)setIndexPath:(NSIndexPath *)indexPath { - (void)setIndexPath:(NSIndexPath *)indexPath {
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#import "NoisePlayView.h" #import "NoisePlayView.h"
#import "NoisePlayCell.h" #import "NoisePlayCell.h"
#import "NoiseAudioCell.h" #import "NoiseAudioCell.h"
#import "NoisePlayerManager.h"
@interface NoisePlayView () <UITableViewDelegate, UITableViewDataSource, NoisePlayCellDelegate> @interface NoisePlayView () <UITableViewDelegate, UITableViewDataSource, NoisePlayCellDelegate>
@property (nonatomic, strong) UIView *headView; @property (nonatomic, strong) UIView *headView;
...@@ -38,9 +39,9 @@ ...@@ -38,9 +39,9 @@
- (void)showNoisePlayViewWith:(BOOL)selected { - (void)showNoisePlayViewWith:(BOOL)selected {
[DSKeyWindow addSubview:self]; [DSKeyWindow addSubview:self];
NSArray *playList = [NoiseDataManager sharedNoiseDataManager].playList; NSArray *playCellList = [NoisePlayerManager sharedNoisePlayerManager].playCellList;
if (playList) { if (playCellList) {
self.audioArr = playList; self.audioArr = playCellList;
[self.playListView reloadData]; [self.playListView reloadData];
} }
...@@ -54,24 +55,25 @@ ...@@ -54,24 +55,25 @@
#pragma mark - Actions #pragma mark - Actions
- (void)oneClickAction:(UIButton *)sender { - (void)oneClickAction:(UIButton *)sender {
for (NoiseAudioCell *audioCell in self.audioArr) { // for (NoiseAudioCell *audioCell in self.audioArr) {
FSAudioStream *audioStream = audioCell.model.audioStream; // FSAudioStream *audioStream = audioCell.model.audioStream;
// if (sender.selected) {
// [audioStream stop];
// } else {
// [audioStream play];
// }
// }
sender.selected = !sender.selected;
if (sender.selected) { if (sender.selected) {
[audioStream stop]; [[NoisePlayerManager sharedNoisePlayerManager] playAll];
} else { } else {
[audioStream play]; [[NoisePlayerManager sharedNoisePlayerManager] pauseAll];
}
} }
sender.selected = !sender.selected;
} }
- (void)clearAllAction:(UIButton *)sender { - (void)clearAllAction:(UIButton *)sender {
for (NoiseAudioCell *audioCell in self.audioArr) { [[NoisePlayerManager sharedNoisePlayerManager] removeAllNoiseAudioCell];
[audioCell.model.audioStream stop];
audioCell.audioIV.selected = NO;
}
self.audioArr = @[]; self.audioArr = @[];
[NoiseDataManager sharedNoiseDataManager].playList = self.audioArr;
[self.playListView reloadData]; [self.playListView reloadData];
[self removeFromSuperview]; [self removeFromSuperview];
} }
...@@ -94,6 +96,7 @@ ...@@ -94,6 +96,7 @@
cell.delegate = self; cell.delegate = self;
NoiseAudioCell *audioCell = self.audioArr[indexPath.row]; NoiseAudioCell *audioCell = self.audioArr[indexPath.row];
cell.model = audioCell.model; cell.model = audioCell.model;
cell.volumn = audioCell.playItem.volume;
cell.indexPath = indexPath; cell.indexPath = indexPath;
return cell; return cell;
} }
...@@ -122,13 +125,12 @@ ...@@ -122,13 +125,12 @@
- (void)didRemoveItem:(NSIndexPath *)indexPath { - (void)didRemoveItem:(NSIndexPath *)indexPath {
// 将要删除的音频停止播放并且设置为非选中状态 // 将要删除的音频停止播放并且设置为非选中状态
NoiseAudioCell *audioCell = self.audioArr[indexPath.row]; NoiseAudioCell *audioCell = self.audioArr[indexPath.row];
[audioCell.model.audioStream stop];
audioCell.audioIV.selected = NO; audioCell.audioIV.selected = NO;
// 删除数据源 // 删除数据源
NSMutableArray *tmpArr = [NSMutableArray arrayWithArray:self.audioArr]; NSMutableArray *tmpArr = [NSMutableArray arrayWithArray:self.audioArr];
[tmpArr removeObjectAtIndex:indexPath.row]; [tmpArr removeObjectAtIndex:indexPath.row];
self.audioArr = [tmpArr copy]; self.audioArr = [tmpArr copy];
[NoiseDataManager sharedNoiseDataManager].playList = self.audioArr; [[NoisePlayerManager sharedNoisePlayerManager] removeNoiseAudioCell:audioCell];
// 执行删除动画 // 执行删除动画
[self.playListView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; [self.playListView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
dispatch_after(.1, dispatch_get_main_queue(), ^{ dispatch_after(.1, dispatch_get_main_queue(), ^{
...@@ -139,7 +141,7 @@ ...@@ -139,7 +141,7 @@
- (void)didSetVolume:(float)volume indexPath:(NSIndexPath *)indexPath { - (void)didSetVolume:(float)volume indexPath:(NSIndexPath *)indexPath {
NoiseAudioCell *audioCell = self.audioArr[indexPath.row]; NoiseAudioCell *audioCell = self.audioArr[indexPath.row];
[audioCell.model.audioStream setVolume:volume]; audioCell.playItem.volume = volume;
} }
#pragma mark - lazy #pragma mark - lazy
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#import "WXApi.h" #import "WXApi.h"
#import <UMCommon/UMCommon.h> #import <UMCommon/UMCommon.h>
#import <UMShare/UMShare.h> #import <UMShare/UMShare.h>
#import <AVFoundation/AVFoundation.h>
@interface AppDelegate () <WXApiDelegate> @interface AppDelegate () <WXApiDelegate>
@end @end
...@@ -47,6 +48,11 @@ ...@@ -47,6 +48,11 @@
// 全局适配:iOS15适配UITableView内容自动下移22像素BUG // 全局适配:iOS15适配UITableView内容自动下移22像素BUG
if (@available(iOS 15.0, *)) { [UITableView appearance].sectionHeaderTopPadding = 0; } if (@available(iOS 15.0, *)) { [UITableView appearance].sectionHeaderTopPadding = 0; }
// 手机在静音模式下不会影响音频播放
AVAudioSession* avSession = [AVAudioSession sharedInstance];
[avSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[avSession setActive:YES error:nil];
return YES; return YES;
} }
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!