Skip to content

Commit f20d9f5

Browse files
authored
Merge pull request #115 from analogcode/dev
Support AirPlay by @geraldnolan
2 parents 5adde24 + 4c26301 commit f20d9f5

File tree

4 files changed

+61
-11
lines changed

4 files changed

+61
-11
lines changed

SwiftRadio/Libraries/FRadioPlayer/FRadioPlayer.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ open class FRadioPlayer: NSObject {
224224

225225
// Enable bluetooth playback
226226
let audioSession = AVAudioSession.sharedInstance()
227-
try? audioSession.setCategory(AVAudioSession.Category.playback, mode: AVAudioSession.Mode.default, options: [.defaultToSpeaker, .allowBluetooth])
227+
try? audioSession.setCategory(AVAudioSession.Category.playback, mode: AVAudioSession.Mode.default, options: [.defaultToSpeaker, .allowBluetooth, .allowAirPlay])
228228

229229
// Notifications
230230
setupNotifications()
@@ -304,6 +304,8 @@ open class FRadioPlayer: NSObject {
304304
private func setupPlayer(with asset: AVAsset) {
305305
if player == nil {
306306
player = AVPlayer()
307+
//Removes black screen when connecting to appleTV
308+
player?.allowsExternalPlayback = false
307309
}
308310

309311
playerItem = AVPlayerItem(asset: asset)

SwiftRadio/Main.storyboard

+21-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="kdl-ej-Zz5">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="kdl-ej-Zz5">
33
<device id="retina5_5" orientation="portrait">
44
<adaptation id="fullscreen"/>
55
</device>
66
<dependencies>
77
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
99
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
1010
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1111
</dependencies>
@@ -40,16 +40,16 @@
4040
</constraints>
4141
</imageView>
4242
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="g0b-Y4-SBe" userLabel="Labels Stack View">
43-
<rect key="frame" x="136" y="22.333333333333332" width="860" height="46.333333333333343"/>
43+
<rect key="frame" x="136" y="22.333333333333332" width="250" height="46.333333333333343"/>
4444
<subviews>
4545
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Station Name" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumScaleFactor="0.5" translatesAutoresizingMaskIntoConstraints="NO" id="hQQ-6f-sYl">
46-
<rect key="frame" x="0.0" y="0.0" width="860" height="20.333333333333332"/>
46+
<rect key="frame" x="0.0" y="0.0" width="250" height="20.333333333333332"/>
4747
<fontDescription key="fontDescription" style="UICTFontTextStyleHeadline"/>
4848
<color key="textColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
4949
<nil key="highlightedColor"/>
5050
</label>
5151
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Desc" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Ptr-mN-34N">
52-
<rect key="frame" x="0.0" y="28.333333333333329" width="860" height="18"/>
52+
<rect key="frame" x="0.0" y="28.333333333333329" width="250" height="18"/>
5353
<fontDescription key="fontDescription" style="UICTFontTextStyleSubhead"/>
5454
<color key="textColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
5555
<nil key="highlightedColor"/>
@@ -411,13 +411,13 @@
411411
<rect key="frame" x="12" y="534" width="390" height="63.333333333333371"/>
412412
<subviews>
413413
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Song Name" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumScaleFactor="0.5" translatesAutoresizingMaskIntoConstraints="NO" id="KC8-ZG-rx5" userLabel="Song Label" customClass="SpringLabel" customModule="SwiftRadio" customModuleProvider="target">
414-
<rect key="frame" x="129.66666666666663" y="0.0" width="130.99999999999997" height="31.333333333333332"/>
414+
<rect key="frame" x="129.66666666666666" y="0.0" width="130.99999999999997" height="31.333333333333332"/>
415415
<fontDescription key="fontDescription" style="UICTFontTextStyleTitle1"/>
416416
<color key="textColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
417417
<nil key="highlightedColor"/>
418418
</label>
419419
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Artist Name" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumScaleFactor="0.5" translatesAutoresizingMaskIntoConstraints="NO" id="v3l-q6-g0h" userLabel="Artist Label">
420-
<rect key="frame" x="143.66666666666663" y="39.333333333333371" width="102.66666666666666" height="24"/>
420+
<rect key="frame" x="143.66666666666666" y="39.333333333333371" width="102.66666666666666" height="24"/>
421421
<fontDescription key="fontDescription" style="UICTFontTextStyleTitle3"/>
422422
<color key="textColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
423423
<nil key="highlightedColor"/>
@@ -481,9 +481,18 @@
481481
</button>
482482
</subviews>
483483
</stackView>
484+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="djB-ds-tW4" userLabel="AirPlay View" customClass="AirPlayUIView">
485+
<rect key="frame" x="186" y="688" width="42" height="45"/>
486+
<color key="backgroundColor" white="0.66666666666666663" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
487+
<constraints>
488+
<constraint firstAttribute="height" constant="45" id="fg4-Nd-K1s"/>
489+
<constraint firstAttribute="width" constant="42" id="yR1-Zk-l1q"/>
490+
</constraints>
491+
</view>
484492
</subviews>
485493
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
486494
<constraints>
495+
<constraint firstItem="djB-ds-tW4" firstAttribute="bottom" secondItem="Ub6-am-LP5" secondAttribute="bottom" constant="10" id="0Po-4M-k2q"/>
487496
<constraint firstItem="Ub6-am-LP5" firstAttribute="centerY" secondItem="GIj-4c-kRh" secondAttribute="centerY" id="2Ym-J6-vxU"/>
488497
<constraint firstItem="obn-8m-awZ" firstAttribute="leading" secondItem="RA9-OX-Xr1" secondAttribute="leading" id="4LO-kb-G6v"/>
489498
<constraint firstItem="RA9-OX-Xr1" firstAttribute="top" secondItem="obn-8m-awZ" secondAttribute="bottom" constant="-32" id="5fc-ws-2hz"/>
@@ -492,6 +501,7 @@
492501
<constraint firstItem="obn-8m-awZ" firstAttribute="leading" secondItem="aXI-gd-81R" secondAttribute="leading" constant="70" id="Ad0-X2-0Fe"/>
493502
<constraint firstItem="GIj-4c-kRh" firstAttribute="leading" secondItem="aXI-gd-81R" secondAttribute="leading" constant="12" id="Hts-LO-0mm"/>
494503
<constraint firstItem="8xJ-4F-WCE" firstAttribute="top" secondItem="a3Z-1t-AAA" secondAttribute="bottom" constant="12" id="Rgp-Rr-5X1"/>
504+
<constraint firstItem="djB-ds-tW4" firstAttribute="centerX" secondItem="8xJ-4F-WCE" secondAttribute="centerX" id="YUB-SR-Omd"/>
495505
<constraint firstItem="aXI-gd-81R" firstAttribute="trailing" secondItem="8xJ-4F-WCE" secondAttribute="trailing" constant="12" id="a0R-gd-CTo"/>
496506
<constraint firstItem="a3Z-1t-AAA" firstAttribute="top" secondItem="WmY-ug-Nhz" secondAttribute="bottom" constant="12" id="epF-5G-BUS"/>
497507
<constraint firstItem="uXf-Wr-UiK" firstAttribute="top" secondItem="kh9-bI-dsS" secondAttribute="top" id="hPm-1B-yKf"/>
@@ -512,6 +522,7 @@
512522
<navigationItem key="navigationItem" title="SubPop Records" id="M1Z-0o-hNV" userLabel="Station Title"/>
513523
<nil key="simulatedBottomBarMetrics"/>
514524
<connections>
525+
<outlet property="airPlayView" destination="djB-ds-tW4" id="JAG-TE-Fcg"/>
515526
<outlet property="albumHeightConstraint" destination="hCD-tK-vVP" id="VlU-n5-VPe"/>
516527
<outlet property="albumImageView" destination="obn-8m-awZ" id="13r-WO-VAs"/>
517528
<outlet property="artistLabel" destination="v3l-q6-g0h" id="s0B-6Y-MWu"/>
@@ -526,7 +537,7 @@
526537
</viewController>
527538
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
528539
</objects>
529-
<point key="canvasLocation" x="1164" y="385.45727136431788"/>
540+
<point key="canvasLocation" x="1163.768115942029" y="384.78260869565219"/>
530541
</scene>
531542
<!--Navigation Controller-->
532543
<scene sceneID="cxZ-Cy-z6Q">
@@ -794,14 +805,14 @@
794805
<image name="btn-previous" width="44" height="44"/>
795806
<image name="btn-stop" width="44" height="44"/>
796807
<image name="icon-hamburger" width="22" height="16"/>
797-
<image name="logo" width="180" height="71"/>
808+
<image name="logo" width="60" height="23.666666030883789"/>
798809
<image name="share" width="32" height="32"/>
799810
<image name="stationImage" width="120" height="75"/>
800811
<image name="swift-radio-black" width="180" height="70"/>
801812
<image name="vol-max" width="16.666666030883789" height="14"/>
802813
<image name="vol-min" width="11" height="12.666666984558105"/>
803814
</resources>
804815
<inferredMetricsTieBreakers>
805-
<segue reference="2md-Am-VN1"/>
816+
<segue reference="Z22-bU-Ytg"/>
806817
</inferredMetricsTieBreakers>
807818
</document>

SwiftRadio/NowPlayingViewController.swift

+26
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import UIKit
1010
import MediaPlayer
11+
import AVKit
12+
1113

1214
//*****************************************************************
1315
// NowPlayingViewControllerDelegate
@@ -39,6 +41,7 @@ class NowPlayingViewController: UIViewController {
3941
@IBOutlet weak var volumeParentView: UIView!
4042
@IBOutlet weak var previousButton: UIButton!
4143
@IBOutlet weak var nextButton: UIButton!
44+
@IBOutlet weak var airPlayView: UIView!
4245

4346
// MARK: - Properties
4447

@@ -78,6 +81,9 @@ class NowPlayingViewController: UIViewController {
7881
// Setup volumeSlider
7982
setupVolumeSlider()
8083

84+
// Setup AirPlayButton
85+
setupAirPlayButton()
86+
8187
// Hide / Show Next/Previous buttons
8288
previousButton.isHidden = hideNextPreviousButtons
8389
nextButton.isHidden = hideNextPreviousButtons
@@ -107,6 +113,26 @@ class NowPlayingViewController: UIViewController {
107113
mpVolumeSlider.setThumbImage(#imageLiteral(resourceName: "slider-ball"), for: .normal)
108114
}
109115

116+
func setupAirPlayButton() {
117+
guard !hideAirPlayButton else {
118+
airPlayView.isHidden = true
119+
return
120+
}
121+
122+
if #available(iOS 11.0, *) {
123+
let airPlayButton = AVRoutePickerView(frame: airPlayView.bounds)
124+
airPlayButton.activeTintColor = globalTintColor
125+
airPlayButton.tintColor = .gray
126+
airPlayView.backgroundColor = .clear
127+
airPlayView.addSubview(airPlayButton)
128+
} else {
129+
let airPlayButton = MPVolumeView(frame: airPlayView.bounds)
130+
airPlayButton.showsVolumeSlider = false
131+
airPlayView.backgroundColor = .clear
132+
airPlayView.addSubview(airPlayButton)
133+
}
134+
}
135+
110136
func stationDidChange() {
111137
radioPlayer.radioURL = URL(string: currentStation.streamURL)
112138
albumImageView.image = currentTrack.artworkImage

SwiftRadio/SwiftRadio-Settings.swift

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
import Foundation
10+
import UIKit
1011

1112
//**************************************
1213
// GENERAL SETTINGS
@@ -15,6 +16,9 @@ import Foundation
1516
// Display Comments
1617
let kDebugLog = true
1718

19+
// AirPlayButton Color
20+
let globalTintColor = UIColor(red: 0, green: 189/255, blue: 233/255, alpha: 1);
21+
1822
//**************************************
1923
// STATION JSON
2024
//**************************************
@@ -38,3 +42,10 @@ let searchable = false
3842

3943
// Set this to "false" to show the next/previous player buttons
4044
let hideNextPreviousButtons = true
45+
46+
//**************************************
47+
// AirPlay BUTTON
48+
//**************************************
49+
50+
// Set this to "true" to hide the AirPlay button
51+
let hideAirPlayButton = false

0 commit comments

Comments
 (0)