1919static void *ObservationContext_Status = &ObservationContext_Status;
2020
2121
22- const NSTimeInterval timeInterval = 0.2 ;
22+ const NSTimeInterval timeInterval = 1.0 ;
2323
2424@interface PlayerEngine ()
2525{
@@ -28,23 +28,54 @@ @interface PlayerEngine ()
2828 int justSeeked;
2929}
3030
31- @property (nonatomic ,strong ) AVAudioEngine *audioEngine;
32- @property (nonatomic ,strong ) AVAudioPlayerNode *audioFilePlayer;
33- @property (nonatomic ,strong ) NSTimer *timer;
31+ @property (nonatomic ,strong ) AVAudioEngine *engine;
32+ @property (nonatomic ,strong ) AVAudioPlayerNode *playerNode;
33+ @property (nonatomic , strong ) AVAudioFile *audioFile;
34+ @property (nonatomic ,strong ) AVAudioPCMBuffer *pcmBuffer;
35+
3436@property (nonatomic , readonly ) double sampleRate;
37+
38+ @property (nonatomic ,strong ) NSTimer *timer;
3539@end
3640
3741@implementation PlayerEngine
3842
43+ -(void )setupPlayer
44+ {
45+ self.playerNode = [[AVAudioPlayerNode alloc ] init ];
46+ [self .engine attachNode: self .playerNode];
47+ }
48+
3949-(instancetype )init
4050{
4151 self = [super init ];
4252 if (self)
4353 {
4454
45- _audioEngine = [AVAudioEngine new ];
46- _audioFilePlayer = [AVAudioPlayerNode new ];
47- [_audioEngine attachNode: _audioFilePlayer];
55+ self.engine = [[AVAudioEngine alloc ]init];
56+
57+
58+ [self setupPlayer ];
59+
60+
61+ // Connect Nodes
62+ // [Player] -> [Output]
63+ AVAudioFormat *format = [[AVAudioFormat alloc ]initStandardFormatWithSampleRate:44100 channels: 2 ];
64+
65+ AVAudioMixerNode *mainMixer = self.engine .mainMixerNode ;
66+ [self .engine connect: self .playerNode to: mainMixer format: format];
67+
68+
69+
70+ // Start the engine.
71+ NSError *error;
72+ [self .engine startAndReturnError: &error];
73+ if (error) {
74+ NSLog (@" error:%@ " , error);
75+ }
76+
77+
78+
4879
4980 firstLoaded = true ;
5081 _state = playstate_stopped;
@@ -80,7 +111,14 @@ -(void)timerComing
80111 {
81112 if ( _state == playstate_playing )
82113 {
83- _progressInfo.current += timeInterval;
114+ AVAudioTime *nodeTime = self.playerNode .lastRenderTime ;
115+ AVAudioTime *playerTime = [self .playerNode playerTimeForNodeTime: nodeTime];
116+ NSTimeInterval t = playerTime.sampleTime / playerTime.sampleRate ;
117+ // NSLog(@"seek , time , %f",t);
118+
119+ _progressInfo.current = t;
120+
121+
84122 postEvent (EventID_track_progress_changed, _progressInfo);
85123 }
86124 }
@@ -95,15 +133,9 @@ -(void)needResumePlayAtBoot
95133 PlayerTrack *track = Playing ();
96134 PlayerList *list = track.list ;
97135
98- if ( doc.playState == playstate_playing )
99- {
100- playTrack ( track );
101- }
102- else
103- {
104- playTrackPauseAfterInit ( list, track , doc.playTime );
105- }
106-
136+ // if ( doc.playState == playstate_paused )
137+ playTrackPauseAfterInit ( list, track , doc.playTime );
138+
107139 }
108140}
109141
@@ -253,13 +285,13 @@ -(void)actionPlayRandom
253285-(void )playPause
254286{
255287 if (self.isPlaying ) {
256- [_audioFilePlayer pause ];
288+ [self .playerNode pause ];
257289 _state = playstate_paused ;
258290 postEvent (EventID_track_paused, nil );
259291 }
260292 else if (self.isPaused )
261293 {
262- [_audioFilePlayer play ];
294+ [self .playerNode play ];
263295 _state = playstate_playing ;
264296 postEvent (EventID_track_resumed, nil );
265297 }
@@ -274,53 +306,67 @@ -(void)seekToTime:(NSTimeInterval)time
274306 justSeeked = 2 ;
275307 _progressInfo.current = time + 2 * timeInterval;
276308
277- AVAudioTime *startTime = [AVAudioTime timeWithSampleTime: time * self .sampleRate atRate: self .sampleRate];
278- [_audioFilePlayer pause ];
279- [_audioFilePlayer playAtTime: startTime];
309+
310+ AVAudioTime *nodeTime = self.playerNode .lastRenderTime ;
311+ AVAudioTime *playerTime = [self .playerNode playerTimeForNodeTime: nodeTime];
312+ NSTimeInterval t = playerTime.sampleTime / playerTime.sampleRate ;
313+ NSLog (@" seek , time , %f " ,t);
314+
315+
316+ AVAudioFramePosition newSampleTime = (AVAudioFramePosition)(self.sampleRate * time);
317+ NSTimeInterval left = _progressInfo.total - time;
318+ AVAudioFrameCount framesLeft = self.sampleRate * left;
319+
320+
321+ // [self.playerNode pause];
322+ [self .playerNode stop ];
323+
324+
325+ if (framesLeft > 100 ) {
326+ [self .playerNode scheduleSegment: self .audioFile startingFrame: newSampleTime frameCount: framesLeft atTime: 0 completionHandler: ^{
327+ postEvent (EventID_track_stopped_playnext, nil );
328+ }];
329+ }
330+
331+ if (_state == playstate_playing) {
332+ [self .playerNode play ];
333+ }
334+
280335}
281336
282337
283338-(BOOL )playURL : (NSURL *)url pauseAfterInit : (BOOL )pauseAfterInit startTime : (NSTimeInterval )startTime
284339{
285- NSURL * fileURL = url;
286- auto audioFile = [[AVAudioFile alloc ] initForReading: fileURL error: nil ];
287- auto audioFormat = audioFile.processingFormat ;
288- uint32 audioFrameCount = (uint32)audioFile.length ;
289- auto audioFileBuffer = [[AVAudioPCMBuffer alloc ] initWithPCMFormat: audioFormat frameCapacity: audioFrameCount];
290- [audioFile readIntoBuffer: audioFileBuffer error: nil ];
340+ self.audioFile = [[AVAudioFile alloc ] initForReading: url error: nil ];
291341
292342
293- auto mainMixer = _audioEngine.mainMixerNode ;
343+ AVAudioFormat *audioFormat = self.audioFile .processingFormat ;
344+ uint32 audioFrameCount = (uint32)self.audioFile .length ;
294345
295- [_audioEngine connect: _audioFilePlayer to: mainMixer format: audioFileBuffer.format];
296- [_audioEngine startAndReturnError: nil ];
297-
298346
299- AVAudioFormat *outputFormat = [_audioFilePlayer outputFormatForBus: 0 ];
347+ self.pcmBuffer = [[AVAudioPCMBuffer alloc ] initWithPCMFormat: audioFormat frameCapacity: audioFrameCount];
348+ [self .audioFile readIntoBuffer: self .pcmBuffer error: nil ];
300349
350+
351+ AVAudioFormat *outputFormat = [self .playerNode outputFormatForBus: 0 ];
301352 _sampleRate = outputFormat.sampleRate ;
353+
302354
303- if (startTime == 0 ) {
304- [_audioFilePlayer play ];
305- }
306- else {
307- AVAudioTime *tStart = [AVAudioTime timeWithSampleTime: startTime * self .sampleRate atRate: self .sampleRate];
308- [_audioFilePlayer playAtTime: tStart];
309- }
355+ [self .playerNode scheduleBuffer: self .pcmBuffer atTime: nil options: AVAudioPlayerNodeBufferLoops completionHandler: ^{
356+ postEvent (EventID_track_stopped_playnext, nil );
357+ }];
310358
359+ [self .playerNode play ];
360+ _state = playstate_playing;
311361
312- [_audioFilePlayer scheduleBuffer: audioFileBuffer atTime: nil options: AVAudioPlayerNodeBufferLoops completionHandler: nil ];
313362
314363
315364 if (pauseAfterInit)
316365 {
317- [_audioFilePlayer pause ];
366+ [self .playerNode pause ];
318367 _state = playstate_paused;
319368 }
320- else {
321- [_audioFilePlayer play ];
322- _state = playstate_playing;
323- }
369+
324370
325371
326372 _progressInfo.current = startTime;
@@ -341,7 +387,7 @@ -(BOOL)playURL:(NSURL *)url
341387
342388-(void )stopInner
343389{
344- [_audioFilePlayer stop ];
390+ [self .playerNode stop ];
345391
346392 _state = playstate_stopped;
347393
@@ -352,7 +398,7 @@ -(void)stopInner
352398
353399-(void )stop
354400{
355- [_audioFilePlayer pause ];
401+ [self .playerNode pause ];
356402
357403 setPlaying (nil );
358404
@@ -372,12 +418,12 @@ -(PlayStateTime)close
372418
373419- (void )setVolume : (float )volume
374420{
375- _audioFilePlayer .volume = volume;
421+ self. playerNode .volume = volume;
376422}
377423
378424- (float )volume
379425{
380- return _audioFilePlayer .volume ;
426+ return self. playerNode .volume ;
381427}
382428
383429@end
0 commit comments