Skip to content

Commit

Permalink
add addPeriodicTimeObserverForInterval to HysteriaPlayer, it will dir…
Browse files Browse the repository at this point in the history
…ectly call AVQueuePlayer same method, so it just bring addPeriodicTimeObserverForInterval method to HysteriaPlayer class, also add a current time label to display current time in view controller in example
  • Loading branch information
Roland Chan committed Apr 14, 2014
1 parent cef1dd5 commit 8cf6463
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
3 changes: 3 additions & 0 deletions HyseteriaSamples/HyseteriaSamples/ViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@
@interface ViewController : UIViewController <AVAudioSessionDelegate, HysteriaPlayerDelegate>
{
}

@property (weak, nonatomic) IBOutlet UILabel *currentTimeLabel;

@end
16 changes: 16 additions & 0 deletions HyseteriaSamples/HyseteriaSamples/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ @interface ViewController ()

UIBarButtonItem *mRefresh;

id mTimeObserver;

__block NSMutableArray *itunesPreviewUrls;
}
@property (nonatomic, weak) IBOutlet UIBarButtonItem *playButton;
Expand Down Expand Up @@ -64,6 +66,19 @@ - (void)viewDidLoad
// It will be called when Player is ready to play at the first time.

// If you have any UI changes related to Player, should update here.

if ( mTimeObserver == nil ) {
mTimeObserver = [hysteriaPlayer addPeriodicTimeObserverForInterval:CMTimeMake(100, 1000)
queue:NULL // main queue
usingBlock:^(CMTime time) {
float totalSecond = CMTimeGetSeconds(time);
int minute = (int)totalSecond / 60;
int second = (int)totalSecond % 60;
self.currentTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d", minute, second];
}];
}


break;

case HysteriaPlayerReadyToPlayCurrentItem:
Expand Down Expand Up @@ -91,6 +106,7 @@ - (void)viewDidLoad
}
NSLog(@"%@", [error localizedDescription]);
}];

}

- (void)hysteriaPlayerCurrentItemChanged:(AVPlayerItem *)item
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="2.0" toolsVersion="4514" systemVersion="13B42" targetRuntime="iOS.CocoaTouch" variant="6xAndEarlier" propertyAccessControl="none" useAutolayout="YES" initialViewController="2">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="2.0" toolsVersion="5056" systemVersion="13C64" targetRuntime="iOS.CocoaTouch" variant="6xAndEarlier" propertyAccessControl="none" useAutolayout="YES" initialViewController="2">
<dependencies>
<deployment defaultVersion="1552" identifier="iOS"/>
<development version="4600" identifier="xcode"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3747"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -15,7 +15,7 @@
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<toolbar opaque="NO" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="bEM-8T-La3">
<rect key="frame" x="0.0" y="504" width="320" height="44"/>
<rect key="frame" x="0.0" y="504" width="321" height="44"/>
<items>
<barButtonItem style="plain" systemItem="flexibleSpace" id="1EQ-vn-Gvt"/>
<barButtonItem image="PreviousButton" style="plain" id="BKx-WL-zzn">
Expand Down Expand Up @@ -98,20 +98,28 @@
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="00:00" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="LD6-p6-Wdd">
<rect key="frame" x="139" y="448" width="43" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="0.66666666666666663" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="jFy-Y0-bxM" firstAttribute="centerX" secondItem="Bq3-gq-r9j" secondAttribute="centerX" type="default" id="5l8-PM-85V"/>
<constraint firstItem="1ky-ju-hz1" firstAttribute="trailing" secondItem="ZCt-IC-8d9" secondAttribute="trailing" type="user" id="8Ni-lO-3VQ"/>
<constraint firstItem="Bq3-gq-r9j" firstAttribute="top" secondItem="3" secondAttribute="top" constant="92" id="9Bp-sQ-HHI"/>
<constraint firstItem="jFy-Y0-bxM" firstAttribute="top" secondItem="3" secondAttribute="top" constant="66" id="ATj-Vq-gr7"/>
<constraint firstItem="1ky-ju-hz1" firstAttribute="centerX" secondItem="bEM-8T-La3" secondAttribute="centerX" type="default" id="Q8o-fG-OKk"/>
<constraint firstItem="LD6-p6-Wdd" firstAttribute="centerX" secondItem="1ky-ju-hz1" secondAttribute="centerX" type="default" id="JQE-fd-AAr"/>
<constraint firstItem="UZS-U4-a9q" firstAttribute="centerX" secondItem="ZCt-IC-8d9" secondAttribute="centerX" type="default" id="Qdn-wO-ool"/>
<constraint firstItem="UZS-U4-a9q" firstAttribute="top" secondItem="3" secondAttribute="top" constant="200" id="Qj4-P2-HvV"/>
<constraint firstItem="Bq3-gq-r9j" firstAttribute="leading" secondItem="3" secondAttribute="leading" constant="20" symbolic="YES" type="user" id="T44-nj-TSS"/>
<constraint firstItem="1ky-ju-hz1" firstAttribute="leading" secondItem="3" secondAttribute="leading" constant="20" type="user" id="T59-Nc-uD3"/>
<constraint firstItem="ZCt-IC-8d9" firstAttribute="top" secondItem="3" secondAttribute="top" constant="226" type="user" id="VKy-Jd-MS9"/>
<constraint firstItem="bEM-8T-La3" firstAttribute="bottom" secondItem="3" secondAttribute="bottom" type="default" id="VvQ-g1-c9B"/>
<constraint firstItem="LD6-p6-Wdd" firstAttribute="centerX" secondItem="bEM-8T-La3" secondAttribute="centerX" type="default" id="WWD-Ql-9Ad"/>
<constraint firstAttribute="trailing" secondItem="Bq3-gq-r9j" secondAttribute="trailing" constant="20" symbolic="YES" type="user" id="Y5r-NS-u7S"/>
<constraint firstAttribute="trailing" secondItem="1ky-ju-hz1" secondAttribute="trailing" constant="20" type="user" id="a3P-wa-XpU"/>
<constraint firstAttribute="trailing" secondItem="ZCt-IC-8d9" secondAttribute="trailing" constant="20" type="user" id="drb-Nr-U8b"/>
Expand All @@ -121,9 +129,11 @@
<constraint firstItem="ZCt-IC-8d9" firstAttribute="leading" secondItem="3" secondAttribute="leading" constant="20" type="user" id="pln-3W-1yN"/>
<constraint firstItem="bEM-8T-La3" firstAttribute="leading" secondItem="3" secondAttribute="leading" type="default" id="qCX-k2-vgH"/>
<constraint firstItem="1ky-ju-hz1" firstAttribute="leading" secondItem="ZCt-IC-8d9" secondAttribute="leading" type="user" id="sqQ-OL-Ln7"/>
<constraint firstAttribute="bottom" secondItem="LD6-p6-Wdd" secondAttribute="bottom" constant="79" id="zOf-Jh-LQE"/>
</constraints>
</view>
<connections>
<outlet property="currentTimeLabel" destination="LD6-p6-Wdd" id="5sJ-dI-1Vi"/>
<outlet property="firstButton" destination="Bq3-gq-r9j" id="gXI-5I-yo3"/>
<outlet property="nextButton" destination="IAP-4w-sZg" id="WmC-HO-9Yg"/>
<outlet property="pauseButton" destination="ist-Jl-1F9" id="ewU-OJ-MlR"/>
Expand Down Expand Up @@ -163,4 +173,4 @@
<simulatedOrientationMetrics key="orientation"/>
<simulatedScreenMetrics key="destination" type="retina4"/>
</simulatedMetricsContainer>
</document>
</document>
4 changes: 3 additions & 1 deletion HysteriaPlayer/HysteriaPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ typedef NS_ENUM(NSUInteger, HysteriaPlayerShuffleMode) {

- (float)getPlayingItemCurrentTime;
- (float)getPlayingItemDurationTime;

- (id)addPeriodicTimeObserverForInterval:(CMTime)interval
queue:(dispatch_queue_t)queue
usingBlock:(void (^)(CMTime time))block;

/*
* Disable memory cache, player will run SourceItemGetter everytime even the media has been played.
Expand Down
10 changes: 10 additions & 0 deletions HysteriaPlayer/HysteriaPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,16 @@ - (float)getPlayingItemDurationTime
return duration;
}

- (id)addPeriodicTimeObserverForInterval:(CMTime)interval
queue:(dispatch_queue_t)queue
usingBlock:(void (^)(CMTime time))block
{
id mTimeObserver = [audioPlayer addPeriodicTimeObserverForInterval:interval queue:queue usingBlock:block];
NSLog(@"audioPlayer is %@", audioPlayer);
NSLog(@"inside HysterialPlayer: mTimeObserver is : %@", mTimeObserver);
return mTimeObserver;
}

#pragma mark -
#pragma mark =========== Interruption, Route changed =========
#pragma mark -
Expand Down

0 comments on commit 8cf6463

Please sign in to comment.