Skip to content

Commit

Permalink
Merge pull request #115 from analogcode/dev
Browse files Browse the repository at this point in the history
Support AirPlay by @geraldnolan
  • Loading branch information
fethica authored Apr 9, 2019
2 parents 5adde24 + 4c26301 commit f20d9f5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
4 changes: 3 additions & 1 deletion SwiftRadio/Libraries/FRadioPlayer/FRadioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ open class FRadioPlayer: NSObject {

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

// Notifications
setupNotifications()
Expand Down Expand Up @@ -304,6 +304,8 @@ open class FRadioPlayer: NSObject {
private func setupPlayer(with asset: AVAsset) {
if player == nil {
player = AVPlayer()
//Removes black screen when connecting to appleTV
player?.allowsExternalPlayback = false
}

playerItem = AVPlayerItem(asset: asset)
Expand Down
31 changes: 21 additions & 10 deletions SwiftRadio/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<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">
<device id="retina5_5" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand Down Expand Up @@ -40,16 +40,16 @@
</constraints>
</imageView>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="g0b-Y4-SBe" userLabel="Labels Stack View">
<rect key="frame" x="136" y="22.333333333333332" width="860" height="46.333333333333343"/>
<rect key="frame" x="136" y="22.333333333333332" width="250" height="46.333333333333343"/>
<subviews>
<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">
<rect key="frame" x="0.0" y="0.0" width="860" height="20.333333333333332"/>
<rect key="frame" x="0.0" y="0.0" width="250" height="20.333333333333332"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleHeadline"/>
<color key="textColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<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">
<rect key="frame" x="0.0" y="28.333333333333329" width="860" height="18"/>
<rect key="frame" x="0.0" y="28.333333333333329" width="250" height="18"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleSubhead"/>
<color key="textColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
Expand Down Expand Up @@ -411,13 +411,13 @@
<rect key="frame" x="12" y="534" width="390" height="63.333333333333371"/>
<subviews>
<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">
<rect key="frame" x="129.66666666666663" y="0.0" width="130.99999999999997" height="31.333333333333332"/>
<rect key="frame" x="129.66666666666666" y="0.0" width="130.99999999999997" height="31.333333333333332"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleTitle1"/>
<color key="textColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<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">
<rect key="frame" x="143.66666666666663" y="39.333333333333371" width="102.66666666666666" height="24"/>
<rect key="frame" x="143.66666666666666" y="39.333333333333371" width="102.66666666666666" height="24"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleTitle3"/>
<color key="textColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
Expand Down Expand Up @@ -481,9 +481,18 @@
</button>
</subviews>
</stackView>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="djB-ds-tW4" userLabel="AirPlay View" customClass="AirPlayUIView">
<rect key="frame" x="186" y="688" width="42" height="45"/>
<color key="backgroundColor" white="0.66666666666666663" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="height" constant="45" id="fg4-Nd-K1s"/>
<constraint firstAttribute="width" constant="42" id="yR1-Zk-l1q"/>
</constraints>
</view>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="djB-ds-tW4" firstAttribute="bottom" secondItem="Ub6-am-LP5" secondAttribute="bottom" constant="10" id="0Po-4M-k2q"/>
<constraint firstItem="Ub6-am-LP5" firstAttribute="centerY" secondItem="GIj-4c-kRh" secondAttribute="centerY" id="2Ym-J6-vxU"/>
<constraint firstItem="obn-8m-awZ" firstAttribute="leading" secondItem="RA9-OX-Xr1" secondAttribute="leading" id="4LO-kb-G6v"/>
<constraint firstItem="RA9-OX-Xr1" firstAttribute="top" secondItem="obn-8m-awZ" secondAttribute="bottom" constant="-32" id="5fc-ws-2hz"/>
Expand All @@ -492,6 +501,7 @@
<constraint firstItem="obn-8m-awZ" firstAttribute="leading" secondItem="aXI-gd-81R" secondAttribute="leading" constant="70" id="Ad0-X2-0Fe"/>
<constraint firstItem="GIj-4c-kRh" firstAttribute="leading" secondItem="aXI-gd-81R" secondAttribute="leading" constant="12" id="Hts-LO-0mm"/>
<constraint firstItem="8xJ-4F-WCE" firstAttribute="top" secondItem="a3Z-1t-AAA" secondAttribute="bottom" constant="12" id="Rgp-Rr-5X1"/>
<constraint firstItem="djB-ds-tW4" firstAttribute="centerX" secondItem="8xJ-4F-WCE" secondAttribute="centerX" id="YUB-SR-Omd"/>
<constraint firstItem="aXI-gd-81R" firstAttribute="trailing" secondItem="8xJ-4F-WCE" secondAttribute="trailing" constant="12" id="a0R-gd-CTo"/>
<constraint firstItem="a3Z-1t-AAA" firstAttribute="top" secondItem="WmY-ug-Nhz" secondAttribute="bottom" constant="12" id="epF-5G-BUS"/>
<constraint firstItem="uXf-Wr-UiK" firstAttribute="top" secondItem="kh9-bI-dsS" secondAttribute="top" id="hPm-1B-yKf"/>
Expand All @@ -512,6 +522,7 @@
<navigationItem key="navigationItem" title="SubPop Records" id="M1Z-0o-hNV" userLabel="Station Title"/>
<nil key="simulatedBottomBarMetrics"/>
<connections>
<outlet property="airPlayView" destination="djB-ds-tW4" id="JAG-TE-Fcg"/>
<outlet property="albumHeightConstraint" destination="hCD-tK-vVP" id="VlU-n5-VPe"/>
<outlet property="albumImageView" destination="obn-8m-awZ" id="13r-WO-VAs"/>
<outlet property="artistLabel" destination="v3l-q6-g0h" id="s0B-6Y-MWu"/>
Expand All @@ -526,7 +537,7 @@
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1164" y="385.45727136431788"/>
<point key="canvasLocation" x="1163.768115942029" y="384.78260869565219"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="cxZ-Cy-z6Q">
Expand Down Expand Up @@ -794,14 +805,14 @@
<image name="btn-previous" width="44" height="44"/>
<image name="btn-stop" width="44" height="44"/>
<image name="icon-hamburger" width="22" height="16"/>
<image name="logo" width="180" height="71"/>
<image name="logo" width="60" height="23.666666030883789"/>
<image name="share" width="32" height="32"/>
<image name="stationImage" width="120" height="75"/>
<image name="swift-radio-black" width="180" height="70"/>
<image name="vol-max" width="16.666666030883789" height="14"/>
<image name="vol-min" width="11" height="12.666666984558105"/>
</resources>
<inferredMetricsTieBreakers>
<segue reference="2md-Am-VN1"/>
<segue reference="Z22-bU-Ytg"/>
</inferredMetricsTieBreakers>
</document>
26 changes: 26 additions & 0 deletions SwiftRadio/NowPlayingViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import UIKit
import MediaPlayer
import AVKit


//*****************************************************************
// NowPlayingViewControllerDelegate
Expand Down Expand Up @@ -39,6 +41,7 @@ class NowPlayingViewController: UIViewController {
@IBOutlet weak var volumeParentView: UIView!
@IBOutlet weak var previousButton: UIButton!
@IBOutlet weak var nextButton: UIButton!
@IBOutlet weak var airPlayView: UIView!

// MARK: - Properties

Expand Down Expand Up @@ -78,6 +81,9 @@ class NowPlayingViewController: UIViewController {
// Setup volumeSlider
setupVolumeSlider()

// Setup AirPlayButton
setupAirPlayButton()

// Hide / Show Next/Previous buttons
previousButton.isHidden = hideNextPreviousButtons
nextButton.isHidden = hideNextPreviousButtons
Expand Down Expand Up @@ -107,6 +113,26 @@ class NowPlayingViewController: UIViewController {
mpVolumeSlider.setThumbImage(#imageLiteral(resourceName: "slider-ball"), for: .normal)
}

func setupAirPlayButton() {
guard !hideAirPlayButton else {
airPlayView.isHidden = true
return
}

if #available(iOS 11.0, *) {
let airPlayButton = AVRoutePickerView(frame: airPlayView.bounds)
airPlayButton.activeTintColor = globalTintColor
airPlayButton.tintColor = .gray
airPlayView.backgroundColor = .clear
airPlayView.addSubview(airPlayButton)
} else {
let airPlayButton = MPVolumeView(frame: airPlayView.bounds)
airPlayButton.showsVolumeSlider = false
airPlayView.backgroundColor = .clear
airPlayView.addSubview(airPlayButton)
}
}

func stationDidChange() {
radioPlayer.radioURL = URL(string: currentStation.streamURL)
albumImageView.image = currentTrack.artworkImage
Expand Down
11 changes: 11 additions & 0 deletions SwiftRadio/SwiftRadio-Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import UIKit

//**************************************
// GENERAL SETTINGS
Expand All @@ -15,6 +16,9 @@ import Foundation
// Display Comments
let kDebugLog = true

// AirPlayButton Color
let globalTintColor = UIColor(red: 0, green: 189/255, blue: 233/255, alpha: 1);

//**************************************
// STATION JSON
//**************************************
Expand All @@ -38,3 +42,10 @@ let searchable = false

// Set this to "false" to show the next/previous player buttons
let hideNextPreviousButtons = true

//**************************************
// AirPlay BUTTON
//**************************************

// Set this to "true" to hide the AirPlay button
let hideAirPlayButton = false

0 comments on commit f20d9f5

Please sign in to comment.