Skip to content

Commit

Permalink
Added downloadProgress
Browse files Browse the repository at this point in the history
  • Loading branch information
changsanjiang committed Aug 17, 2018
1 parent 2238806 commit f07ca20
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion SJMP3Player.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'SJMP3Player'
s.version = '1.1.2'
s.version = '1.1.3'
s.summary = 'mp3 player.'

# This description is used to generate tags and improve search results.
Expand Down
2 changes: 2 additions & 0 deletions SJMP3Player/Classes/SJMP3Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly) BOOL isPlaying;
@property (nonatomic) float rate;
@property (nonatomic) BOOL enableDBUG;
// current task
@property (readonly) float downloadProgress;

/// 跳转
- (BOOL)seekToTime:(NSTimeInterval)sec;
Expand Down
28 changes: 14 additions & 14 deletions SJMP3Player/Classes/SJMP3Player.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ @interface SJMP3Player()<AVAudioPlayerDelegate>
@property (nonatomic, strong, nullable) NSTimer *refreshTimeTimer;
@property (nonatomic, strong, nullable) NSTimer *tryToPlayTimer;
@property (strong, nullable) AVAudioPlayer *audioPlayer;
@property (nonatomic) NSTimeInterval duration;

#pragma mark
@property (nonatomic, strong, nullable) _SJMP3PlayerGetFileDuration *durationLoader;
Expand All @@ -78,8 +79,6 @@ @interface SJMP3Player()<AVAudioPlayerDelegate>
// current task
@property float downloadProgress;

@property (nonatomic, strong, readonly) NSLock *lock;

@end

@implementation SJMP3Player {
Expand Down Expand Up @@ -119,7 +118,7 @@ - (instancetype)init {
self = [super init];
if ( !self ) return nil;
_rate = 1;

__weak typeof(self) _self = self;
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
_pauseToken = [commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
Expand Down Expand Up @@ -172,9 +171,6 @@ - (instancetype)init {

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_audioSessionInterruptionNotification:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_activateAudioSession) name:UIApplicationDidEnterBackgroundNotification object:nil];

_lock = [NSLock new];
_lock.name = @"SJMP3Player.lock";
return self;
}

Expand Down Expand Up @@ -244,6 +240,7 @@ - (void)pause {
- (void)resume {
self.userClickedPause = NO;
[self.audioPlayer play];
self.audioPlayer.rate = self.rate;
[self _setPlayInfo];
[self _activateRefreshTimeTimer];
}
Expand Down Expand Up @@ -529,16 +526,12 @@ - (void)_playFile:(NSURL *)fileURL source:(SJMP3PlayerFileSource)origin currentT
if ( ![audioPlayer prepareToPlay] ) return;
audioPlayer.delegate = self;
audioPlayer.currentTime = currentTime;
[audioPlayer play];
audioPlayer.rate = self.rate;

self.audioPlayer = audioPlayer;
[self resume];
if ( self.enableDBUG ) {
printf("\n- SJMP3Player: 开始播放: %d - %s, 持续时间: %f 秒 - 播放地址为: %s \n", audioPlayer.isPlaying, audioPlayer.description.UTF8String, audioPlayer.duration, fileURL.description.UTF8String);
printf("\n- SJMP3Player: 开始播放: 当前时间: %f- %s, 持续时间: %f 秒 - 播放地址为: %s \n", audioPlayer.currentTime, audioPlayer.description.UTF8String, audioPlayer.duration, fileURL.description.UTF8String);
if ( @available(ios 10, *) ) printf("\n- SJMP3Player: 格式%s \n", audioPlayer.format.description.UTF8String);
}
self.audioPlayer = audioPlayer;
[self _activateRefreshTimeTimer];
[self _setPlayInfo];
}

#pragma mark - delegate
Expand Down Expand Up @@ -574,6 +567,8 @@ - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)f
printf("\n- SJMP3Player: 播放完毕, 播放地址:%s \n ", player.url.description.UTF8String);
}

[self _clearRefreshTimeTimer];

if ( [self.delegate respondsToSelector:@selector(audioPlayerDidFinishPlaying:)] ) {
__weak typeof(self) _self = self;
dispatch_async(dispatch_get_main_queue(), ^{
Expand Down Expand Up @@ -671,7 +666,12 @@ - (void)_setPlayInfo {
[mediaDict setValue:[[MPMediaItemArtwork alloc] initWithImage:info.cover]
forKey:MPMediaItemPropertyArtwork];
}
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = mediaDict;
if ( [NSThread currentThread].isMainThread ) [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = mediaDict;
else {
dispatch_async(dispatch_get_main_queue(), ^{
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = mediaDict;
});
}
}
@end

Expand Down

0 comments on commit f07ca20

Please sign in to comment.