Compare commits

...

98 Commits

Author SHA1 Message Date
Tanha 80ce253f92 Release 2.8.2 2020-01-21 00:29:28 -08:00
tanhakabir fe2395066f Add equalizer example (#28)
* project configure for me

* to see log

* set project

* new UI

* fix ui

* re-fix UI

* add example of equalizer

* Revert "re-fix UI"

This reverts commit 05ed993a52.

* Revert "fix ui"

This reverts commit 0da9f6adea.

* Revert "new UI"

This reverts commit ffd6a95a2d.

* Add verbose debug mode to player

Co-authored-by: cendolinside123 <jnsbstn391@gmail.com>
2020-01-21 00:27:51 -08:00
Tanha 3e66b4b4d4 Release 2.8.1 2020-01-04 13:35:29 -08:00
Tanha 58bbc97a1b Minor fix on end of audio notification 2020-01-04 13:35:03 -08:00
Tanha 8d9e9d92f4 Release 2.8.0 2020-01-04 13:24:47 -08:00
tanhakabir 03392c21e0 fix bug in notification of end of audio in PlayingStatus (#25) 2020-01-04 13:23:50 -08:00
Tanha 924170d159 Release 2.7.0 2020-01-04 02:04:35 -08:00
tanhakabir b355eb4e09 add api for buffer progress in SAAvailabilityRange (#24) 2020-01-04 02:02:56 -08:00
Tanha 1373a816a6 Release 2.6.0 2019-12-18 21:23:07 -08:00
tanhakabir 196b04a703 Rename and deprecate initialize functions (#23) 2019-12-18 21:22:28 -08:00
Tanha ac971e65a6 Release 2.5.2 2019-12-18 16:19:57 -08:00
Tanha 2c50502b28 update documentation for live streaming audio 2019-12-18 16:19:37 -08:00
Tanha c222b5a745 Release 2.5.1 2019-12-18 00:27:49 -08:00
Tanha 2e86a6503c Clean up debug logging 2019-12-18 00:27:21 -08:00
Tanha 9ebd7fa7fe Release 2.5.0 2019-12-18 00:24:11 -08:00
tanhakabir 5197a16023 Fix live streams/servers with unpredictable size at beginning of stream being playable (#21)
* Handle case when header for stream does not contain expected content

* update documentation

* fix elapsed time updating on seek in example app
2019-12-18 00:22:31 -08:00
Tanha 159627c63e Release 2.4.0 2019-12-03 01:43:45 -08:00
tanhakabir 07230cce1a add another status to PlayingStatus for end of audio (#19) 2019-12-03 01:42:56 -08:00
tanhakabir a33aee80d1 Expose engine outside of SAPlayer (#18)
* expose engine outside of player

* add player clearing functionality
2019-12-03 01:25:58 -08:00
tanhakabir e1d3da1ddb Update README.md 2019-12-01 11:10:12 -08:00
Tanha 8c2524d990 Release 2.3.0 2019-12-01 01:17:54 -08:00
Tanha be1b7aa05f fix bug on bad network and streaming being stuck in missing data state
close #4
2019-12-01 01:16:03 -08:00
Tanha 4b57fee75c Release 2.2.0 2019-11-29 17:18:33 -08:00
tanhakabir fc9c43a23c Update accessing bytes of Data for Swift 5 (#17) 2019-11-29 17:16:53 -08:00
Tanha fd4e4e3b77 Release 2.1.0 2019-11-28 21:37:16 -08:00
tanhakabir f1200252be Update to Swift 5 (#16) 2019-11-28 21:36:30 -08:00
Jonathan Mercer 046e64b2b8 Update README.md (#15)
* Update readme

- Completely re-worded the first paragraph
- Re-worded some sentences that confused me
- Moved audio manipulation in the end. Assuming that advanced users will read it there while keeping it hidden from other users

* Update README.md
2019-11-27 16:36:24 -08:00
Tanha ad9e40ad1c Update README.md 2019-11-26 01:37:53 -08:00
Tanha f19eaf7ec9 Release 2.0.1 2019-11-26 01:04:51 -08:00
tanhakabir 012291c1c9 Merge pull request #14 from tanhakabir/open_nodes_interface
Open interface to control audio manipulation nodes
2019-11-26 01:03:11 -08:00
Tanha 70ba1c757e add controls for realtime reverb change in example app 2019-11-26 01:02:06 -08:00
Tanha 3ab47b568d nit 2019-11-26 00:53:52 -08:00
Tanha 6fd985d2ad Update documentation 2019-11-26 00:52:18 -08:00
Tanha cf028e0e36 nit rename 2019-11-26 00:52:08 -08:00
Tanha f9e6dafc2c add documentation for player functions 2019-11-26 00:00:44 -08:00
Tanha e562a259fb only show monitoring worthy errors outside of library 2019-11-25 23:48:24 -08:00
Tanha 9594b560d0 update lockscreen interval skip control on change of fields 2019-11-25 23:23:07 -08:00
Tanha bf2dae9569 add documentation for fields of SAPlayer 2019-11-25 23:19:29 -08:00
Tanha 90ac3a4336 clean up typing for rate 2019-11-25 22:23:05 -08:00
Tanha 395364b4eb test run with more modifiers 2019-11-25 22:17:55 -08:00
Tanha 6a2bb94037 fix freezing bug 2019-11-25 22:17:07 -08:00
Tanha 7d81953b83 removed usage of rate/speed within the engine 2019-11-25 22:10:57 -08:00
Tanha feb69174ae refactor to have external to library control nodes 2019-11-25 21:57:55 -08:00
Tanha 00eee68aab silenced warnings for example app 2019-11-25 21:01:53 -08:00
Tanha 6276e97c4c Release 1.3.0 2019-11-25 20:58:29 -08:00
Tanha 09142ce2d4 update to Swift 4.2 2019-11-25 20:57:54 -08:00
Tanha 90bc2262ec Release 1.2.0 2019-11-25 15:12:55 -08:00
Tanha 9594449215 nit 2019-11-25 15:05:49 -08:00
Tanha 6187c9f438 fix playing on seek 2019-11-25 14:30:30 -08:00
Tanha b28e815545 rename clean up 2019-11-25 13:49:06 -08:00
Tanha 17be73bbe8 fix fatal bug seeking 2019-11-25 13:44:54 -08:00
tanhakabir cd35f38db1 Merge pull request #13 from tanhakabir/fix_ui_seeking_bug
Fix ui when seeking to show "Loading" when not enough buffers ready
2019-11-25 10:44:28 -08:00
cendolinside123 3c752d581d Fix miniplayer on background (#12)
* 1. fix AVAudioSession configuration

* 1. fix seek bar didn't update on miniplayer on lockscreen when playing a song
2. setup miniplayer lockscreen seek bar speed (when slide  rate slider)

* setup project example to playing on the background
2019-11-25 10:44:00 -08:00
Tanha 1f20a48a20 fix example app bug 2019-11-25 10:37:11 -08:00
Tanha 3a585c1f43 fix playing status bug in disk engine 2019-11-25 10:30:06 -08:00
Tanha 5ac5b93ac4 separate enum to another file 2019-11-24 23:27:51 -08:00
Tanha b484f0bfb6 fix playing status when seeking 2019-11-24 23:24:25 -08:00
Tanha 0aeb8b0f88 change boolean playing status to enum 2019-11-21 01:46:42 -08:00
Tanha 8e7357860c shouldnot be forcing play 2019-11-21 01:24:45 -08:00
Tanha 936de8c996 minor fix 2019-11-20 23:10:03 -08:00
Tanha e986be9db5 clean up example app 2019-11-20 22:27:31 -08:00
Tanha 876d517f3d Release 1.1.1 2019-11-20 22:15:51 -08:00
Tanha 0a12c68274 Fix fatal error when seeking on streamed audio 2019-11-20 22:15:32 -08:00
cendolinside123 873e537301 fix seek bar on example app's player (#9) 2019-11-20 22:09:11 -08:00
Tanha 94c1a47641 Release 1.1.0 2019-11-20 16:36:58 -08:00
tanhakabir d0296ab012 Fix issue on streaming where it gets stuck in paused state and error of no more data to parse (#8)
* switch out audio clip for soundbite

* Fix being stuck in state of needing more data from the throttler
2019-11-20 16:35:31 -08:00
tanhakabir 2fd944d88e Fix play/pausing issue for saved audio (#7)
* update callback guards for updates for saved audio

* Fix play/pausing bug for saved audio
2019-11-20 13:38:52 -08:00
tanhakabir fc98c4c1c4 add separation of disk engine in SAPlayer, first iteration (#6) 2019-11-19 16:25:29 -08:00
Tanha 8bf6cbb56e Release 1.0.3 2019-11-18 13:46:50 -08:00
Tanha b97f97ca5e Fix fatal error on iOS 10.0
close #3
2019-11-18 13:39:06 -08:00
Tanha 0c7bcdcf90 Fix issue on example app that prevented downloaded audio being playable 2019-11-18 11:42:02 -08:00
Tanha 840122e603 remove build badge from README 2019-04-27 23:00:46 -07:00
Tanha 8518d10c6d v1.0.2 2019-04-27 22:56:57 -07:00
Tanha f214be28a9 nit 2019-04-27 22:37:17 -07:00
Tanha f219d9d1a0 nit 2019-04-27 22:36:37 -07:00
Tanha 8797c0d917 add API documentation for Downloader 2019-04-27 22:35:21 -07:00
Tanha 0121d05dff refractor deletion of downloaded files 2019-04-27 21:54:42 -07:00
Tanha 26faf62657 documentation for downloading 2019-04-27 21:42:12 -07:00
Tanha 61e79d067a ensure cancelling download also removed from queued downloads 2019-04-27 20:46:53 -07:00
Tanha 103838d1b8 add UI to see where file is saved on device 2019-04-27 19:43:43 -07:00
Tanha 47de2a5251 fix double download bug 2019-04-27 19:40:48 -07:00
Tanha d4d8f767e3 document downloading audio 2019-04-27 18:59:14 -07:00
tanhakabir c75da619cf Merge pull request #2 from tanhakabir/refractor_downloaded_audio
Refractor downloaded audio
2019-04-22 15:30:51 -07:00
Tanha aea6f5efaa add completion handler for individual entities to receive when download complete upon calling start 2019-04-22 15:30:11 -07:00
Tanha 2625b8f4db remove unused resume data in download worker 2019-04-10 14:45:56 -07:00
Tanha e6460513ea start piping for passing completion handlers for downloads 2019-02-28 14:45:50 -08:00
tanhakabir a2504f2726 Update README.md 2019-02-25 15:56:10 -08:00
Tanha 23f445ce4d seperate downloader from rest of SAPlayer implementation 2019-02-25 01:33:18 -08:00
Tanha 61fe0c6ebb nit 2019-02-24 23:24:06 -08:00
Tanha 72c4335386 nit 2019-02-24 23:23:57 -08:00
Tanha 640f0b92f0 make lockscreen artwork optional 2019-02-24 23:03:43 -08:00
Tanha c0f8db29c0 nit spelling 2019-02-24 21:36:46 -08:00
Tanha 285cd92514 nit 2019-02-24 21:35:10 -08:00
Tanha a5293a5b39 nit 2019-02-24 21:31:59 -08:00
Tanha 8430a7e8ce nit 2019-02-24 21:31:04 -08:00
Tanha 34e430713b nit 2019-02-24 21:25:10 -08:00
Tanha d23a5f8d62 Update README with Updates API 2019-02-24 21:24:04 -08:00
Tanha 9f89944bc5 nit 2019-02-24 20:40:32 -08:00
39 changed files with 1240 additions and 294 deletions
+23 -6
View File
@@ -14,6 +14,8 @@
79D8DF73FA7CDD6E266BAE71D46E035F /* Pods-SwiftAudioPlayer_Tests-umbrella.h in Headers */ = {isa = PBXBuildFile; fileRef = 50C71346CE708A211A5AFAC20BAE48CB /* Pods-SwiftAudioPlayer_Tests-umbrella.h */; settings = {ATTRIBUTES = (Public, ); }; };
831B263D357A5FA2DDC7B1AE4B374092 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5A16F4CFC63FAC439D7A04994F579A03 /* Foundation.framework */; };
8F93DB166237195ED222EE55B6404625 /* Pods-SwiftAudioPlayer_Example-umbrella.h in Headers */ = {isa = PBXBuildFile; fileRef = 3B0B76CB1439F4D361322144E5A65C3A /* Pods-SwiftAudioPlayer_Example-umbrella.h */; settings = {ATTRIBUTES = (Public, ); }; };
A40DBE292391D9CA00F86146 /* Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = A40DBE282391D9C900F86146 /* Data.swift */; };
A41AA0D2238BB9B600A467E1 /* SAPlayingStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = A41AA0D1238BB9B600A467E1 /* SAPlayingStatus.swift */; };
A4681FC6220113880018AB51 /* SAPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4681F8D2200E00E0018AB51 /* SAPlayer.swift */; };
A4681FC72201138B0018AB51 /* SAPlayerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4681F912200E1950018AB51 /* SAPlayerDelegate.swift */; };
A4681FC82201138E0018AB51 /* SAPlayerPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4681F8F2200E1450018AB51 /* SAPlayerPresenter.swift */; };
@@ -42,6 +44,7 @@
A4681FDF220113E20018AB51 /* DirectorThreadSafeClosures.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4681F872200DAD50018AB51 /* DirectorThreadSafeClosures.swift */; };
A4681FE0220113E40018AB51 /* Log.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4681F802200D0500018AB51 /* Log.swift */; };
A4681FE1220113E70018AB51 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4681F8B2200DDD50018AB51 /* Constants.swift */; };
A4B4CC122223ED2A0045554B /* SAPlayerDownloader.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4B4CC112223ED2A0045554B /* SAPlayerDownloader.swift */; };
A4FBA6B2221B538E00D5A353 /* DownloadProgressDirector.swift in Sources */ = {isa = PBXBuildFile; fileRef = A49B78C3221A78DE00BBA862 /* DownloadProgressDirector.swift */; };
A4FBA6B5221B74C900D5A353 /* SALockScreenInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4FBA6B3221B74C900D5A353 /* SALockScreenInfo.swift */; };
A4FBA6B7221BAC3D00D5A353 /* SAPlayerUpdateSubscription.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4FBA6B6221BAC3D00D5A353 /* SAPlayerUpdateSubscription.swift */; };
@@ -94,6 +97,8 @@
99925F09FC9C6EA4B9C0508F4E2D1FE2 /* Info.plist */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
A19C8F889C787C19BE4123C1896AF501 /* Pods-SwiftAudioPlayer_Example-resources.sh */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.script.sh; path = "Pods-SwiftAudioPlayer_Example-resources.sh"; sourceTree = "<group>"; };
A39F2A138CF40C1051CA9E227429A86D /* SwiftAudioPlayer.modulemap */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.module; path = SwiftAudioPlayer.modulemap; sourceTree = "<group>"; };
A40DBE282391D9C900F86146 /* Data.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Data.swift; sourceTree = "<group>"; };
A41AA0D1238BB9B600A467E1 /* SAPlayingStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SAPlayingStatus.swift; sourceTree = "<group>"; };
A4523BC8220A0B3C0079C4BC /* Credited_LICENSE */ = {isa = PBXFileReference; lastKnownFileType = text; path = Credited_LICENSE; sourceTree = "<group>"; };
A4681F802200D0500018AB51 /* Log.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Log.swift; sourceTree = "<group>"; };
A4681F822200D9150018AB51 /* AudioEngine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioEngine.swift; sourceTree = "<group>"; };
@@ -124,6 +129,7 @@
A4681FBC220100AB0018AB51 /* AudioStreamEngine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioStreamEngine.swift; sourceTree = "<group>"; };
A4681FBE22010ECF0018AB51 /* LockScreenViewProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LockScreenViewProtocol.swift; sourceTree = "<group>"; };
A49B78C3221A78DE00BBA862 /* DownloadProgressDirector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DownloadProgressDirector.swift; sourceTree = "<group>"; };
A4B4CC112223ED2A0045554B /* SAPlayerDownloader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SAPlayerDownloader.swift; sourceTree = "<group>"; };
A4FBA6B3221B74C900D5A353 /* SALockScreenInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SALockScreenInfo.swift; sourceTree = "<group>"; };
A4FBA6B6221BAC3D00D5A353 /* SAPlayerUpdateSubscription.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SAPlayerUpdateSubscription.swift; sourceTree = "<group>"; };
A4FBA6B8221BAF8700D5A353 /* SAAudioAvailabilityRange.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SAAudioAvailabilityRange.swift; sourceTree = "<group>"; };
@@ -269,6 +275,7 @@
A4681F872200DAD50018AB51 /* DirectorThreadSafeClosures.swift */,
A4681F892200DB3C0018AB51 /* Date.swift */,
A4681F962200E2E20018AB51 /* URL.swift */,
A40DBE282391D9C900F86146 /* Data.swift */,
);
path = Util;
sourceTree = "<group>";
@@ -276,6 +283,7 @@
A4681F932200E2020018AB51 /* Engine */ = {
isa = PBXGroup;
children = (
A41AA0D1238BB9B600A467E1 /* SAPlayingStatus.swift */,
A4FBA6B8221BAF8700D5A353 /* SAAudioAvailabilityRange.swift */,
A4681F822200D9150018AB51 /* AudioEngine.swift */,
A4681F942200E2220018AB51 /* AudioDiskEngine.swift */,
@@ -344,6 +352,7 @@
A4FBA6B3221B74C900D5A353 /* SALockScreenInfo.swift */,
A4681F8D2200E00E0018AB51 /* SAPlayer.swift */,
A4FBA6B6221BAC3D00D5A353 /* SAPlayerUpdateSubscription.swift */,
A4B4CC112223ED2A0045554B /* SAPlayerDownloader.swift */,
A4681F912200E1950018AB51 /* SAPlayerDelegate.swift */,
A4681F8F2200E1450018AB51 /* SAPlayerPresenter.swift */,
A4681FBE22010ECF0018AB51 /* LockScreenViewProtocol.swift */,
@@ -476,19 +485,23 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0930;
LastUpgradeCheck = 0930;
LastUpgradeCheck = 1010;
TargetAttributes = {
042ACE071BA515F4DE0E0C8007C3F0EE = {
LastSwiftMigration = 1010;
};
E50DAD13FFD3FC8036073A58BF8423D4 = {
LastSwiftMigration = 1120;
};
};
};
buildConfigurationList = 2D8E8EC45A3A1A1D94AE762CB5028504 /* Build configuration list for PBXProject "Pods" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = 7DB346D0F39D3F0E887471402A8071AB;
productRefGroup = 21D946895A4F57F51246F3EBCF330719 /* Products */;
@@ -507,12 +520,14 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
A41AA0D2238BB9B600A467E1 /* SAPlayingStatus.swift in Sources */,
A4681FDC220113D70018AB51 /* AudioDownloadWorker.swift in Sources */,
A4681FD8220113C60018AB51 /* AudioDataManager.swift in Sources */,
A4681FD1220113AF0018AB51 /* AudioParsable.swift in Sources */,
A4681FD2220113B20018AB51 /* AudioParser.swift in Sources */,
A4681FCF220113A40018AB51 /* AudioConverterListener.swift in Sources */,
A4681FE1220113E70018AB51 /* Constants.swift in Sources */,
A40DBE292391D9CA00F86146 /* Data.swift in Sources */,
A4FBA6B5221B74C900D5A353 /* SALockScreenInfo.swift in Sources */,
A4681FC6220113880018AB51 /* SAPlayer.swift in Sources */,
A4FBA6B7221BAC3D00D5A353 /* SAPlayerUpdateSubscription.swift in Sources */,
@@ -530,6 +545,7 @@
A4681FC82201138E0018AB51 /* SAPlayerPresenter.swift in Sources */,
A4681FD3220113B60018AB51 /* AudioParserPropertyListener.swift in Sources */,
A4681FCA220113940018AB51 /* AudioClockDirector.swift in Sources */,
A4B4CC122223ED2A0045554B /* SAPlayerDownloader.swift in Sources */,
A4681FD0220113A70018AB51 /* AudioConverterErrors.swift in Sources */,
A4FBA6B2221B538E00D5A353 /* DownloadProgressDirector.swift in Sources */,
A4681FD7220113C30018AB51 /* StreamProgressPTO.swift in Sources */,
@@ -630,7 +646,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
PRODUCT_NAME = "$(TARGET_NAME)";
@@ -665,7 +681,7 @@
SKIP_INSTALL = YES;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) ";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
@@ -697,7 +713,7 @@
SKIP_INSTALL = YES;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) ";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
@@ -792,10 +808,11 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
MTL_ENABLE_DEBUG_INFO = NO;
PRODUCT_NAME = "$(TARGET_NAME)";
STRIP_INSTALLED_PRODUCT = NO;
SWIFT_COMPILATION_MODE = wholemodule;
SYMROOT = "${SRCROOT}/../build";
};
name = Release;
@@ -207,25 +207,25 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0830;
LastUpgradeCheck = 0830;
LastUpgradeCheck = 1010;
ORGANIZATIONNAME = CocoaPods;
TargetAttributes = {
607FACCF1AFB9204008FA782 = {
CreatedOnToolsVersion = 6.3.1;
DevelopmentTeam = R2392A68YQ;
LastSwiftMigration = 0900;
DevelopmentTeam = H9Y26B6GZB;
LastSwiftMigration = 1120;
};
607FACE41AFB9204008FA782 = {
CreatedOnToolsVersion = 6.3.1;
DevelopmentTeam = R2392A68YQ;
LastSwiftMigration = 0900;
DevelopmentTeam = H9Y26B6GZB;
LastSwiftMigration = 1120;
TestTargetID = 607FACCF1AFB9204008FA782;
};
};
};
buildConfigurationList = 607FACCB1AFB9204008FA782 /* Build configuration list for PBXProject "SwiftAudioPlayer" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
@@ -379,12 +379,14 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
@@ -432,12 +434,14 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
@@ -471,14 +475,13 @@
baseConfigurationReference = 65A66AB4C3016E8BB53FF3E0 /* Pods-SwiftAudioPlayer_Example.debug.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = R2392A68YQ;
DEVELOPMENT_TEAM = H9Y26B6GZB;
INFOPLIST_FILE = SwiftAudioPlayer/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo-test.SwiftAudioPlayer-Example";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
@@ -487,14 +490,13 @@
baseConfigurationReference = 4B5DD2AE0B23A759D18926DC /* Pods-SwiftAudioPlayer_Example.release.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = R2392A68YQ;
DEVELOPMENT_TEAM = H9Y26B6GZB;
INFOPLIST_FILE = SwiftAudioPlayer/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo-test.SwiftAudioPlayer-Example";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Release;
};
@@ -502,7 +504,7 @@
isa = XCBuildConfiguration;
baseConfigurationReference = BBD877782CC67FBCC7BF7532 /* Pods-SwiftAudioPlayer_Tests.debug.xcconfig */;
buildSettings = {
DEVELOPMENT_TEAM = R2392A68YQ;
DEVELOPMENT_TEAM = H9Y26B6GZB;
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
"$(inherited)",
@@ -515,8 +517,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/SwiftAudioPlayer_Example.app/SwiftAudioPlayer_Example";
};
name = Debug;
@@ -525,7 +526,7 @@
isa = XCBuildConfiguration;
baseConfigurationReference = 0B7D1E6C00E83B4AF8AA1781 /* Pods-SwiftAudioPlayer_Tests.release.xcconfig */;
buildSettings = {
DEVELOPMENT_TEAM = R2392A68YQ;
DEVELOPMENT_TEAM = H9Y26B6GZB;
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
"$(inherited)",
@@ -534,8 +535,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/SwiftAudioPlayer_Example.app/SwiftAudioPlayer_Example";
};
name = Release;
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0900"
LastUpgradeVersion = "1010"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
@@ -40,7 +40,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
@@ -70,7 +69,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
+1 -1
View File
@@ -15,7 +15,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
@@ -32,7 +32,9 @@
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<color key="maximumTrackTintColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<connections>
<action selector="scrubberSeeked:" destination="vXZ-lx-hvc" eventType="valueChanged" id="jDA-wR-wxk"/>
<action selector="scrubberSeeked:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="hTi-fq-lrl"/>
<action selector="scrubberSeeked:" destination="vXZ-lx-hvc" eventType="touchUpOutside" id="mFP-SW-38w"/>
<action selector="scrubberStartedSeeking:" destination="vXZ-lx-hvc" eventType="touchDown" id="UXg-Wf-fKv"/>
</connections>
</slider>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="jUc-tP-CC5">
@@ -57,15 +59,21 @@
</connections>
</button>
<slider opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" value="1" minValue="0.10000000000000001" maxValue="32" translatesAutoresizingMaskIntoConstraints="NO" id="vfk-OJ-S3T">
<rect key="frame" x="14" y="564" width="347" height="31"/>
<rect key="frame" x="14" y="464" width="347" height="31"/>
<connections>
<action selector="rateChanged:" destination="vXZ-lx-hvc" eventType="valueChanged" id="FDJ-jA-bm8"/>
</connections>
</slider>
<slider opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" value="300" minValue="0.10000000149011612" maxValue="1000" translatesAutoresizingMaskIntoConstraints="NO" id="nsl-df-P21">
<rect key="frame" x="14" y="397" width="347" height="31"/>
<connections>
<action selector="reverbChanged:" destination="vXZ-lx-hvc" eventType="valueChanged" id="J8Q-be-35q"/>
</connections>
</slider>
<segmentedControl opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="plain" selectedSegmentIndex="0" translatesAutoresizingMaskIntoConstraints="NO" id="joK-xi-MCo">
<rect key="frame" x="16" y="80" width="343" height="29"/>
<segments>
<segment title="20k Hertz"/>
<segment title="Soundbite"/>
<segment title="Acquired"/>
<segment title="Y Combinator"/>
</segments>
@@ -87,8 +95,8 @@
<action selector="streamTouched:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="AXY-N7-87Y"/>
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="rate: 1.0" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="yUQ-mI-ozK">
<rect key="frame" x="157" y="535" width="61" height="21"/>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="rate: 1.0x" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="yUQ-mI-ozK">
<rect key="frame" x="153" y="435" width="69" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
@@ -105,9 +113,22 @@
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="remote url: " textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="1IX-z5-wWx">
<rect key="frame" x="16" y="207" width="343" height="16"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="reverb: 300.0" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="y5i-MZ-Qat">
<rect key="frame" x="136" y="368" width="103" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="nsl-df-P21" firstAttribute="top" secondItem="y5i-MZ-Qat" secondAttribute="bottom" constant="8" id="0aM-Sz-J9k"/>
<constraint firstItem="lTK-Hd-Tl2" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" constant="16" id="1wb-IW-jYz"/>
<constraint firstItem="j3w-gr-HzF" firstAttribute="leading" secondItem="lTK-Hd-Tl2" secondAttribute="leading" id="26c-ZJ-768"/>
<constraint firstItem="jUc-tP-CC5" firstAttribute="top" secondItem="KDu-ea-kF8" secondAttribute="bottom" constant="80" id="5sT-An-9vw"/>
@@ -117,23 +138,31 @@
<constraint firstItem="joK-xi-MCo" firstAttribute="trailing" secondItem="lTK-Hd-Tl2" secondAttribute="trailing" id="AH1-Uu-eLB"/>
<constraint firstItem="joK-xi-MCo" firstAttribute="top" secondItem="jyV-Pf-zRb" secondAttribute="bottom" constant="60" id="Ba7-nd-oCD"/>
<constraint firstItem="Urj-Dv-41y" firstAttribute="centerY" secondItem="j3w-gr-HzF" secondAttribute="centerY" id="Fvd-7V-Rr8"/>
<constraint firstItem="1IX-z5-wWx" firstAttribute="leading" secondItem="joK-xi-MCo" secondAttribute="leading" id="GeX-7f-jzu"/>
<constraint firstItem="0QE-3F-a4G" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="jUc-tP-CC5" secondAttribute="trailing" constant="8" symbolic="YES" id="JP5-yW-eVB"/>
<constraint firstItem="yUQ-mI-ozK" firstAttribute="top" secondItem="w2a-RA-zmI" secondAttribute="bottom" constant="200" id="K1K-8N-SpD"/>
<constraint firstItem="yUQ-mI-ozK" firstAttribute="top" secondItem="w2a-RA-zmI" secondAttribute="bottom" constant="100" id="K1K-8N-SpD"/>
<constraint firstItem="vfk-OJ-S3T" firstAttribute="leading" secondItem="lTK-Hd-Tl2" secondAttribute="leading" id="NOY-IO-NIJ"/>
<constraint firstItem="tFH-sY-Xu9" firstAttribute="centerY" secondItem="jUc-tP-CC5" secondAttribute="centerY" id="Rre-EY-kVY"/>
<constraint firstItem="KDu-ea-kF8" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" constant="78" id="SRU-sX-z5b"/>
<constraint firstItem="w2a-RA-zmI" firstAttribute="trailing" secondItem="lTK-Hd-Tl2" secondAttribute="trailing" id="Vki-IZ-AdN"/>
<constraint firstItem="lTK-Hd-Tl2" firstAttribute="top" secondItem="j3w-gr-HzF" secondAttribute="bottom" constant="8" id="Wwx-Uo-yIC"/>
<constraint firstItem="nsl-df-P21" firstAttribute="leading" secondItem="vfk-OJ-S3T" secondAttribute="leading" id="a5C-nZ-8Jc"/>
<constraint firstItem="yUQ-mI-ozK" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="a66-h4-WVf"/>
<constraint firstItem="Urj-Dv-41y" firstAttribute="trailing" secondItem="lTK-Hd-Tl2" secondAttribute="trailing" id="aKt-EV-Bwd"/>
<constraint firstItem="tFH-sY-Xu9" firstAttribute="top" secondItem="1IX-z5-wWx" secondAttribute="bottom" constant="27" id="bIq-V0-Sac"/>
<constraint firstItem="tFH-sY-Xu9" firstAttribute="leading" secondItem="kh9-bI-dsS" secondAttribute="leading" constant="62.5" id="cH6-q6-Lel"/>
<constraint firstItem="yUQ-mI-ozK" firstAttribute="top" secondItem="nsl-df-P21" secondAttribute="bottom" constant="8" id="cKV-wk-6P9"/>
<constraint firstItem="jUc-tP-CC5" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="cgM-Nj-yit"/>
<constraint firstItem="KDu-ea-kF8" firstAttribute="top" secondItem="joK-xi-MCo" secondAttribute="bottom" constant="32" id="dLw-rF-Pfb"/>
<constraint firstItem="w2a-RA-zmI" firstAttribute="leading" secondItem="lTK-Hd-Tl2" secondAttribute="leading" id="daz-b0-eCC"/>
<constraint firstItem="jUc-tP-CC5" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="tFH-sY-Xu9" secondAttribute="trailing" constant="8" symbolic="YES" id="fS9-Ce-4ph"/>
<constraint firstItem="Urj-Dv-41y" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="j3w-gr-HzF" secondAttribute="trailing" constant="8" symbolic="YES" id="fu0-ZZ-rj9"/>
<constraint firstAttribute="trailing" secondItem="lTK-Hd-Tl2" secondAttribute="trailing" constant="16" id="gdg-7Y-7la"/>
<constraint firstAttribute="trailing" secondItem="1IX-z5-wWx" secondAttribute="trailing" constant="16" id="hHM-jO-RZd"/>
<constraint firstItem="6d9-Bc-hIz" firstAttribute="top" secondItem="joK-xi-MCo" secondAttribute="bottom" constant="32" id="m9s-An-IWV"/>
<constraint firstItem="vfk-OJ-S3T" firstAttribute="top" secondItem="yUQ-mI-ozK" secondAttribute="bottom" constant="8" id="oaW-rr-UVN"/>
<constraint firstItem="nsl-df-P21" firstAttribute="trailing" secondItem="vfk-OJ-S3T" secondAttribute="trailing" id="r5e-Wq-dqV"/>
<constraint firstItem="y5i-MZ-Qat" firstAttribute="centerX" secondItem="nsl-df-P21" secondAttribute="centerX" id="reC-GA-ZgT"/>
<constraint firstAttribute="trailing" secondItem="0QE-3F-a4G" secondAttribute="trailing" constant="62.5" id="tg1-gr-hdd"/>
<constraint firstAttribute="trailing" secondItem="6d9-Bc-hIz" secondAttribute="trailing" constant="82" id="vtN-y4-iqp"/>
<constraint firstItem="0QE-3F-a4G" firstAttribute="centerY" secondItem="jUc-tP-CC5" secondAttribute="centerY" id="xDi-tj-bBF"/>
@@ -145,11 +174,14 @@
<outlet property="audioSelector" destination="joK-xi-MCo" id="GmY-Xg-be0"/>
<outlet property="bufferProgress" destination="lTK-Hd-Tl2" id="54k-by-qb2"/>
<outlet property="currentTimestampLabel" destination="j3w-gr-HzF" id="5Lh-aS-pat"/>
<outlet property="currentUrlLocationLabel" destination="1IX-z5-wWx" id="MuO-fF-ZxL"/>
<outlet property="downloadButton" destination="KDu-ea-kF8" id="5o4-1h-y06"/>
<outlet property="durationLabel" destination="Urj-Dv-41y" id="mIq-eh-int"/>
<outlet property="playPauseButton" destination="jUc-tP-CC5" id="e9C-zV-A1B"/>
<outlet property="rateLabel" destination="yUQ-mI-ozK" id="Dx4-lO-A1B"/>
<outlet property="rateSlider" destination="vfk-OJ-S3T" id="mNc-ET-aNM"/>
<outlet property="reverbLabel" destination="y5i-MZ-Qat" id="8YR-mc-GFA"/>
<outlet property="reverbSlider" destination="nsl-df-P21" id="BKt-Hb-akj"/>
<outlet property="scrubberSlider" destination="w2a-RA-zmI" id="VbI-tT-lbc"/>
<outlet property="skipBackwardButton" destination="tFH-sY-Xu9" id="LwM-2S-m6F"/>
<outlet property="skipForwardButton" destination="0QE-3F-a4G" id="cQ7-b7-pW7"/>
+6
View File
@@ -22,6 +22,12 @@
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>fetch</string>
<string>processing</string>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
+110 -30
View File
@@ -8,34 +8,35 @@
import UIKit
import SwiftAudioPlayer
import AVFoundation
class ViewController: UIViewController {
struct AudioInfo {
struct AudioInfo: Hashable {
let index: Int
var url: URL {
switch index {
case 0:
return URL(string: "https://traffic.megaphone.fm/TTH7630150098.mp3")!
return URL(string: "https://cdn.fastlearner.media/bensound-rumble.mp3")!
case 1:
return URL(string: "https://chtbl.com/track/18338/traffic.libsyn.com/secure/acquired/acquired_-_armrev_2.mp3?dest-id=376122")!
case 2:
return URL(string: "https://backtracks.fm/ycombinator/pr/0f685f72-29b1-11e9-9bcf-0ece7a7d2472/111---jake-klamka-and-kevin-hale---y-combinator.mp3?s=1&amp;sd=1&amp;u=1549423185")!
default:
return URL(string: "https://traffic.megaphone.fm/TTH7630150098.mp3")!
return URL(string: "https://cdn.fastlearner.media/bensound-rumble.mp3")!
}
}
var title: String {
switch index {
case 0:
return "Twenty Thousand Hertz"
return "Soundbite"
case 1:
return "Acquired"
case 2:
return "Y Combinator"
default:
return "Twenty Thousand Hertz"
return "Soundbite"
}
}
@@ -43,15 +44,23 @@ class ViewController: UIViewController {
let releaseDate: Int = 1550790640
}
var savedUrls: [AudioInfo: URL] = [:]
var selectedAudio: AudioInfo = AudioInfo(index: 0) {
didSet {
if SAPlayer.Downloader.isDownloaded(withRemoteUrl: selectedAudio.url) {
downloadButton.setTitle("Delete downloaded", for: .normal)
streamButton.isEnabled = false
} else {
downloadButton.setTitle("Download", for: .normal)
streamButton.isEnabled = true
}
self.currentUrlLocationLabel.text = "remote url: \(selectedAudio.url.absoluteString)"
}
}
var freq:[Int] = [0,0,0,0,0,0,0,0,0,0]
@IBOutlet weak var currentUrlLocationLabel: UILabel!
@IBOutlet weak var bufferProgress: UIProgressView!
@IBOutlet weak var scrubberSlider: UISlider!
@@ -66,11 +75,14 @@ class ViewController: UIViewController {
@IBOutlet weak var rateLabel: UILabel!
@IBOutlet weak var reverbLabel: UILabel!
@IBOutlet weak var reverbSlider: UISlider!
@IBOutlet weak var durationLabel: UILabel!
@IBOutlet weak var currentTimestampLabel: UILabel!
var isDownloading: Bool = false
var isStreaming: Bool = false
var beingSeeked: Bool = false
var duration: Double = 0.0
@@ -91,20 +103,24 @@ class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
adjustSpeed()
SAPlayer.shared.DEBUG_MODE = true
isPlayable = false
selectedAudio = AudioInfo(index: 0)
addRandomModifiers()
_ = SAPlayer.Updates.Duration.subscribe { [weak self] (url, duration) in
guard let self = self else { return }
guard url == self.selectedAudio.url else { return }
guard url == self.selectedAudio.url || url == self.savedUrls[self.selectedAudio] else { return }
self.durationLabel.text = SAPlayer.prettifyTimestamp(duration)
self.duration = duration
}
_ = SAPlayer.Updates.ElapsedTime.subscribe { [weak self] (url, position) in
guard let self = self else { return }
guard url == self.selectedAudio.url else { return }
guard url == self.selectedAudio.url || url == self.savedUrls[self.selectedAudio] else { return }
self.currentTimestampLabel.text = SAPlayer.prettifyTimestamp(position)
guard self.duration != 0 else { return }
@@ -113,26 +129,27 @@ class ViewController: UIViewController {
}
_ = SAPlayer.Updates.AudioDownloading.subscribe { [weak self] (url, progress) in
print(progress)
guard let self = self else { return }
guard url == self.selectedAudio.url else { return }
if self.isDownloading {
self.downloadButton.setTitle("Cancel \(String(format: "%02d", (progress * 100)))%", for: .normal)
DispatchQueue.main.async {
UIView.performWithoutAnimation {
self.downloadButton.setTitle("Cancel \(String(format: "%.2f", (progress * 100)))%", for: .normal)
}
}
}
}
_ = SAPlayer.Updates.StreamingBuffer.subscribe{ [weak self] (url, buffer) in
guard let self = self else { return }
guard url == self.selectedAudio.url else { return }
guard url == self.selectedAudio.url || url == self.savedUrls[self.selectedAudio] else { return }
if self.duration == 0.0 { return }
let progress = Float((buffer.totalDurationBuffered + buffer.startingBufferTimePositon) / self.duration)
self.bufferProgress.progress = Float(buffer.bufferingProgress)
self.bufferProgress.progress = progress
if progress >= 0.99 {
if buffer.bufferingProgress >= 0.99 {
self.streamButton.isEnabled = false
}
@@ -141,15 +158,44 @@ class ViewController: UIViewController {
_ = SAPlayer.Updates.PlayingStatus.subscribe { [weak self] (url, playing) in
guard let self = self else { return }
guard url == self.selectedAudio.url else { return }
guard url == self.selectedAudio.url || url == self.savedUrls[self.selectedAudio] else { return }
if playing {
switch playing {
case .playing:
self.isPlayable = true
self.playPauseButton.setTitle("Pause", for: .normal)
} else {
return
case .paused:
self.isPlayable = true
self.playPauseButton.setTitle("Play", for: .normal)
return
case .buffering:
self.isPlayable = false
self.playPauseButton.setTitle("Loading", for: .normal)
return
case .ended:
self.isPlayable = false
self.playPauseButton.setTitle("Done", for: .normal)
return
}
}
}
func addRandomModifiers() {
let node = AVAudioUnitReverb()
SAPlayer.shared.audioModifiers.append(node)
node.wetDryMix = 300
let frequency:[Int] = [60,170,310,600,1000,3000,6000,12000,14000,16000]
let node2 = AVAudioUnitEQ(numberOfBands:frequency.count)
node2.globalGain = 1
for i in 0...(node2.bands.count-1) {
node2.bands[i].frequency = Float(frequency[i])
node2.bands[i].gain = 0
node2.bands[i].bypass = false
node2.bands[i].filterType = .parametric
}
SAPlayer.shared.audioModifiers.append(node2)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
@@ -162,28 +208,55 @@ class ViewController: UIViewController {
selectedAudio = AudioInfo(index: selected)
SAPlayer.shared.mediaInfo = SALockScreenInfo(title: selectedAudio.title, artist: selectedAudio.artist, artwork: UIImage(), releaseDate: selectedAudio.releaseDate)
// if let savedUrl = savedUrls[selectedAudio] {}
}
@IBAction func scrubberStartedSeeking(_ sender: UISlider) {
beingSeeked = true
}
@IBAction func scrubberSeeked(_ sender: Any) {
SAPlayer.shared.seekTo(seconds: Double(scrubberSlider.value))
let value = Double(scrubberSlider.value) * duration
SAPlayer.shared.seekTo(seconds: value)
beingSeeked = false
}
@IBAction func rateChanged(_ sender: Any) {
adjustSpeed()
let speed = rateSlider.value
rateLabel.text = "rate: \(speed)x"
if let node = SAPlayer.shared.audioModifiers[0] as? AVAudioUnitTimePitch {
node.rate = speed
SAPlayer.shared.playbackRateOfAudioChanged(rate: speed)
}
}
@IBAction func reverbChanged(_ sender: Any) {
let reverb = reverbSlider.value
reverbLabel.text = "reverb: \(reverb)"
if let node = SAPlayer.shared.audioModifiers[1] as? AVAudioUnitReverb {
node.wetDryMix = reverb
}
}
@IBAction func downloadTouched(_ sender: Any) {
if !isDownloading {
if SAPlayer.Downloader.isDownloaded(withRemoteUrl: selectedAudio.url) {
SAPlayer.Downloader.deleteDownload(withRemoteUrl: selectedAudio.url)
if let savedUrl = SAPlayer.Downloader.getSavedUrl(forRemoteUrl: selectedAudio.url) {
SAPlayer.Downloader.deleteDownloaded(withSavedUrl: savedUrl)
downloadButton.setTitle("Download", for: .normal)
streamButton.isEnabled = true
isDownloading = false
} else {
downloadButton.setTitle("Cancel 0%", for: .normal)
isDownloading = true
SAPlayer.Downloader.downloadAudio(withRemoteUrl: selectedAudio.url)
SAPlayer.Downloader.downloadAudio(withRemoteUrl: selectedAudio.url, completion: { [weak self] url in
guard let self = self else { return }
DispatchQueue.main.async {
self.currentUrlLocationLabel.text = "saved to: \(url.lastPathComponent)"
self.savedUrls[self.selectedAudio] = url
SAPlayer.shared.startSavedAudio(withSavedUrl: url)
}
})
streamButton.isEnabled = false
}
} else {
@@ -196,7 +269,7 @@ class ViewController: UIViewController {
@IBAction func streamTouched(_ sender: Any) {
if !isStreaming {
SAPlayer.shared.initializeAudio(withRemoteUrl: selectedAudio.url)
SAPlayer.shared.startRemoteAudio(withRemoteUrl: selectedAudio.url)
streamButton.setTitle("Cancel streaming", for: .normal)
downloadButton.isEnabled = false
} else {
@@ -215,11 +288,18 @@ class ViewController: UIViewController {
@IBAction func skipForwardTouched(_ sender: Any) {
SAPlayer.shared.skipForward()
}
private func adjustSpeed() {
let speed = rateSlider.value
rateLabel.text = "rate: \(speed)x"
SAPlayer.shared.rate = Double(speed)
@IBAction func setEqualizerValue(_ sender: Any) {
if let slider = sender as? UISlider{
print("slider of index:", slider.tag, "is changed to", slider.value)
freq[slider.tag] = Int(slider.value)
print("current frequency : ",freq)
if let node = SAPlayer.shared.audioModifiers[2] as? AVAudioUnitEQ{
for i in 0...(node.bands.count - 1){
node.bands[i].gain = Float(freq[i])
}
}
}
}
}
+221 -12
View File
@@ -1,25 +1,27 @@
# SwiftAudioPlayer
[![CI Status](https://img.shields.io/travis/tanhakabir/SwiftAudioPlayer.svg?style=flat)](https://travis-ci.org/tanhakabir/SwiftAudioPlayer)
[![Version](https://img.shields.io/cocoapods/v/SwiftAudioPlayer.svg?style=flat)](https://cocoapods.org/pods/SwiftAudioPlayer)
[![License](https://img.shields.io/cocoapods/l/SwiftAudioPlayer.svg?style=flat)](https://cocoapods.org/pods/SwiftAudioPlayer)
[![Platform](https://img.shields.io/cocoapods/p/SwiftAudioPlayer.svg?style=flat)](https://cocoapods.org/pods/SwiftAudioPlayer)
Swift based audio player that is able to both stream remote audio and play locally saved audio, while performing audio manipulations in real-time. Underlying using AVAudioEngine, and you can change the rate of audio (up to 32x), change pitch, and [other audio enhancements](https://developer.apple.com/documentation/avfoundation/audio_track_engineering/audio_engine_building_blocks/audio_enhancements).
Swift-based audio player with AVAudioEngine as its base. Allows for: streaming online audio, playing local file, changing audio speed (3.5X, 4X, 32X), pitch, and real-time audio manipulation using custom [audio enhancements](https://developer.apple.com/documentation/avfoundation/audio_track_engineering/audio_engine_building_blocks/audio_enhancements).
This player was originally developed to be used in a [podcast player](https://chameleonpodcast.com/). We had originally used AVPlayer for playing audio but we wanted to manipulate audio that was being streamed. We set up AVAudioEngine at first just to play a file saved on the phone and it worked great, but AVAudioEngine on its own doesn't support streaming audio as easily as AVPlayer.
This player was built for [podcasting](https://chameleonpodcast.com/). We originally used AVPlayer for playing audio but we wanted to manipulate audio that was being streamed. We set up AVAudioEngine at first just to play a file saved on the phone and it worked great, but AVAudioEngine on its own doesn't support streaming audio as easily as AVPlayer.
Thus, using [AudioToolbox](https://developer.apple.com/documentation/audiotoolbox), we are able to stream audio and convert the downloaded data into usable data for the AVAudioEngine to play. For an overview of our solution check out our [blog post](https://medium.com/chameleon-podcast/creating-an-advanced-streaming-audio-engine-for-ios-9fbc7aef4115).
### Requirements
SwiftAudioPlayer is only available for iOS 10.0 and higher.
iOS 10.0 and higher.
## Getting Started
### Example Project
### Running the Example Project
To run the example project, clone the repo, and run `pod install` from the Example directory first.
1. Clone repo
2. CD to directory
3. Run `pod install` in terminal
4. Build and run
### Installation
@@ -32,21 +34,23 @@ pod 'SwiftAudioPlayer'
### Usage
**Important:** For app in background downloading please refer to [note](#important-step-for-background-downloads).
To play remote audio:
```
```swift
let url = URL(string: "https://randomwebsite.com/audio.mp3")!
SAPlayer.shared.initializeAudio(withRemoteUrl: url)
SAPlayer.shared.startRemoteAudio(withRemoteUrl: url)
SAPlayer.shared.play()
```
To set the display information for the lockscreen:
```
```swift
let info = SALockScreenInfo(title: "Random audio", artist: "Foo", artwork: UIImage(), releaseDate: 123456789)
SAPlayer.shared.mediaInfo = info
```
To receive streaming progress:
```
To receive streaming progress (for buffer progress %):
```swift
@IBOutlet weak var bufferProgress: UIProgressView!
override func viewDidLoad() {
@@ -64,8 +68,31 @@ override func viewDidLoad() {
}
}
```
Look at the [Updates](#SAPlayer.Updates) section to see usage details and other updates to follow.
For realtime audio manipulations, [AVAudioUnit](https://developer.apple.com/documentation/avfoundation/avaudiounit) nodes are used. For example to adjust the reverb through a slider in the UI:
```swift
@IBOutlet weak var reverbSlider: UISlider!
override func viewDidLoad() {
super.viewDidLoad()
let node = AVAudioUnitReverb()
SAPlayer.shared.audioModifiers.append(node)
node.wetDryMix = 300
}
@IBAction func reverbSliderChanged(_ sender: Any) {
if let node = SAPlayer.shared.audioModifiers[1] as? AVAudioUnitReverb {
node.wetDryMix = reverbSlider.value
}
}
```
For a more detailed explanation on usage, look at the [Realtime Audio Manipulations](#realtime-audio-manipulation) section.
For more details and specifics look at the [API documentation](#api-in-detail) below.
## Contact
### Issues
@@ -79,6 +106,188 @@ Feel free to reach out to either of us:
[tanhakabir](https://github.com/tanhakabir), tanhakabir.ca@gmail.com
[JonMercer](https://github.com/JonMercer), mercer.jon@gmail.com
## License
### License
SwiftAudioPlayer is available under the MIT license. See the LICENSE file for more info.
---
# API in detail
## SAPlayer
Access the player and all of its fields and functions through `SAPlayer.shared`.
### Playing Audio (Basic Commands)
To set up player with audio to play, use either:
* `startSavedAudio(withSavedUrl url: URL, mediaInfo: SALockScreenInfo?)` to play audio that is saved on the device.
* `startRemoteAudio(withRemoteUrl url: URL, mediaInfo: SALockScreenInfo?)` to play audio streamed from a remote location.
Both of these expect a URL of the location of the audio and an optional media information to display on the lockscreen.
For streaming remote audio, subscribe to `SAPlayer.Updates.StreamingBuffer` for updates on streaming progress.
Basic controls available:
```swift
play()
pause()
togglePlayAndPause()
seekTo(seconds: Double)
skipForward()
skipBackwards()
```
#### Important
The engine can handle audio manipulations like speed, pitch, effects, etc. To do this, nodes for effects must be finalized before initialize is called. Look at [audio manipulation documentation](#realtime-audio-manipulation) for more information.
### Lockscreen Media Player
Update and set what displays on the lockscreen's media player when the player is active.
`skipForwardSeconds` and `skipBackwardSeconds` for the intervals to skip forward and back with.
`mediaInfo` for the audio's information to display on the lockscreen. Is of type `SALockScreenInfo` which contains:
```swift
title: String
artist: String
artwork: UIImage?
releaseDate: UTC // Int
```
`playbackRateOfAudioChanged(rate: Float)` is used to update the lockscreen media player that the playback rate has changed.
## SAPlayer.Downloader
Use functionaity from Downloader to save audio files from remote locations for future offline playback.
Audio files are saved under custom naming scheme on device and are recoverable with original remote URL for file.
#### Important step for background downloads
To ensure that your app will keep downloading audio in the background be sure to add the following to `AppDelegate.swift`:
```swift
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
SAPlayer.Downloader.setBackgroundCompletionHandler(completionHandler)
}
```
### Downloading
All downloads will be paused when audio is streamed from a URL. They will automatically resume when streaming is done.
Use the following to start downloading audio in the background:
```swift
func downloadAudio(withRemoteUrl url: URL, completion: @escaping (_ savedUrl: URL) -> ())
```
It will call the completion handler you pass after successful download with the location of the downloaded file on the device.
Subscribe to `SAPlayer.Updates.AudioDownloading` for downloading progress updates.
And use the following to stop any active or prevent future downloads of the corresponding remote URL:
```swift
func cancelDownload(withRemoteUrl url: URL)
```
### Manage Downloaded
Use the following to manage downloaded audio files.
Checks if downloaded already:
```swift
func isDownloaded(withRemoteUrl url: URL) -> Bool
```
Get URL of audio file saved on device corresponding to remote location:
```swift
func getSavedUrl(forRemoteUrl url: URL) -> URL?
```
Delete downloaded audio if it exists:
```swift
func deleteDownloaded(withSavedUrl url: URL)
```
**NOTE:** You're in charge or clearing downloads when your don't need them anymore
## SAPlayer.Updates
Receive updates for changing values from the player, such as the duration, elapsed time of playing audio, download progress, and etc.
All subscription functions for updates take the form of:
```swift
func subscribe(_ closure: @escaping (_ url: URL, _ payload: <Payload>) -> ()) -> UInt
```
- `closure`: The closure that will receive the updates. It's recommended to have a weak reference to a class that uses these functions.
- `url`: The corresponding remote URL for the update. In the case there might be multiple files observed, such as downloading many files at once or switching over from playing one audio to another and the updates corresponding to the previous aren't silenced on switch-over.
- `payload`: The updated value.
- Returns: the id for the subscription in the case you would like to unsubscribe to updates for the closure.
Similarily unsubscribe takes the form of:
```swift
func unsubscribe(_ id: UInt)
```
- `id`: The closure with this id will stop receiving updates.
### ElapsedTime
Payload = `Double`
Changes in the timestamp/elapsed time of the current initialized audio. Aka, where the scrubber's pointer of the audio should be at.
Subscribe to this to update views on changes in position of which part of audio is being played.
### Duration
Payload = `Double`
Changes in the duration of the current initialized audio. Especially helpful for audio that is being streamed and can change with more data. The engine makes a best effort guess as to the duration of the audio. The guess gets better with more bytes streamed from the web.
### PlayingStatus
Payload = `SAPlayingStatus`
Changes in the playing status of the player. Can be one of the following 4: `playing`, `paused`, `buffering`, `ended` (audio ended).
### StreamingBuffer
Payload = `SAAudioAvailabilityRange`
Changes in the progress of downloading audio for streaming. Information about range of audio available and if the audio is playable. Look at SAAudioAvailabilityRange for more information.
For progress of downloading audio that saves to the phone for playback later, look at AudioDownloading instead.
### AudioDownloading
Payload = `Double`
Changes in the progress of downloading audio in the background. This does not correspond to progress in streaming downloads, look at StreamingBuffer for streaming progress.
## Audio Effects
### Realtime Audio Manipulation
All audio effects on the player is done through [AVAudioUnit](https://developer.apple.com/documentation/avfoundation/avaudiounit) nodes. These include adding reverb, changing pitch and playback rate, and adding distortion. Full list of effects available [here](https://developer.apple.com/documentation/avfoundation/audio_track_engineering/audio_engine_building_blocks/audio_enhancements).
The effects intended to use are stored in `audioModifiers` as a list of nodes. These nodes are in the order that the engine will attach them to one another.
**Note:** By default `SAPlayer` starts off with one node, an [AVAudioUnitTimePitch](https://developer.apple.com/documentation/avfoundation/avaudiounittimepitch) node, that is set to change the rate of audio without changing the pitch of the audio (intended for changing the rate of spoken word).
#### Important
All the nodes intended to be used on the playing audio must be finalized before calling `initializeSavedAudio(...)` or `initializeRemoteAudio(...)`. Any changes to list of nodes after initialize is called for a given audio file will not be reflected in playback.
Once all nodes are added to `audioModifiers` and the player has been initialized, any manipulations done with the nodes are performed in realtime. The example app shows manipulating the playback rate in realtime:
```swift
let speed = rateSlider.value
if let node = SAPlayer.shared.audioModifiers[0] as? AVAudioUnitTimePitch {
node.rate = speed
SAPlayer.shared.playbackRateOfAudioChanged(rate: speed)
}
```
**Note:** if the rate of the audio is changed, `playbackRateOfAudioChanged` should also be called to update the lockscreen's media player.
+4 -8
View File
@@ -31,7 +31,7 @@ class AudioClockDirector {
private var needleClosures: DirectorThreadSafeClosures<Needle> = DirectorThreadSafeClosures()
private var durationClosures: DirectorThreadSafeClosures<Duration> = DirectorThreadSafeClosures()
private var playingStatusClosures: DirectorThreadSafeClosures<IsPlaying> = DirectorThreadSafeClosures()
private var playingStatusClosures: DirectorThreadSafeClosures<SAPlayingStatus> = DirectorThreadSafeClosures()
private var bufferClosures: DirectorThreadSafeClosures<SAAudioAvailabilityRange> = DirectorThreadSafeClosures()
private init() {}
@@ -60,7 +60,7 @@ class AudioClockDirector {
// Playing status
func attachToChangesInPlayingStatus(closure: @escaping (Key, IsPlaying) throws -> Void) -> UInt{
func attachToChangesInPlayingStatus(closure: @escaping (Key, SAPlayingStatus) throws -> Void) -> UInt{
return playingStatusClosures.attach(closure: closure)
}
@@ -103,12 +103,8 @@ extension AudioClockDirector {
}
extension AudioClockDirector {
func audioPaused(_ key: Key) {
playingStatusClosures.broadcast(key: key, payload: false)
}
func audioPlaying(_ key: Key) {
playingStatusClosures.broadcast(key: key, payload: true)
func audioPlayingStatusWasChanged(_ key: Key, status: SAPlayingStatus) {
playingStatusClosures.broadcast(key: key, payload: status)
}
}
+2 -2
View File
@@ -64,7 +64,7 @@ class AudioDiskEngine: AudioEngine {
audioSampleRate = Float(audioFormat?.sampleRate ?? 44100)
audioLengthSeconds = Float(audioLengthSamples) / audioSampleRate
duration = Duration(audioLengthSeconds)
bufferedSeconds = SAAudioAvailabilityRange(startingNeedle: 0, durationLoadedByNetwork: duration, isPlayable: true)
bufferedSeconds = SAAudioAvailabilityRange(startingNeedle: 0, durationLoadedByNetwork: duration, predictedDurationToLoad: duration, isPlayable: true)
} else {
Log.monitor("Could not load downloaded file with url: \(url)")
}
@@ -98,7 +98,7 @@ class AudioDiskEngine: AudioEngine {
if state == .resumed {
state = .suspended
}
delegate?.didEndPlaying()
playingStatus = .ended
}
guard audioSampleRate != 0 else {
+48 -38
View File
@@ -27,10 +27,10 @@ import Foundation
import AVFoundation
protocol AudioEngineProtocol {
var engine: AVAudioEngine { get set }
func play()
func pause()
func seek(toNeedle needle: Needle)
func setSpeed(speed: Double)
func invalidate()
}
@@ -43,9 +43,8 @@ class AudioEngine: AudioEngineProtocol {
weak var delegate:AudioEngineDelegate?
let key:Key
let engine = AVAudioEngine()
var engine = AVAudioEngine()
let playerNode = AVAudioPlayerNode()
let rateNode: AVAudioUnitTimePitch
var timer: Timer?
@@ -57,12 +56,6 @@ class AudioEngine: AudioEngineProtocol {
case resumed
}
var audioSpeed: Double = 1.0 {
didSet {
rateNode.rate = Float(audioSpeed)
}
}
var needle: Needle = -1 {
didSet {
if needle >= 0 && oldValue != needle {
@@ -79,22 +72,22 @@ class AudioEngine: AudioEngineProtocol {
}
}
var isPlaying = false {
var playingStatus: SAPlayingStatus? = nil {
didSet {
guard isPlaying != oldValue else {
guard playingStatus != oldValue, let status = playingStatus else {
return
}
if isPlaying {
AudioClockDirector.shared.audioPlaying(key)
} else {
AudioClockDirector.shared.audioPaused(key)
if status == .ended {
delegate?.didEndPlaying()
}
AudioClockDirector.shared.audioPlayingStatusWasChanged(key, status: status)
}
}
var bufferedSecondsDebouncer: SAAudioAvailabilityRange = SAAudioAvailabilityRange(startingNeedle: 0.0, durationLoadedByNetwork: 0.0, isPlayable: false)
var bufferedSeconds: SAAudioAvailabilityRange = SAAudioAvailabilityRange(startingNeedle: 0.0, durationLoadedByNetwork: 0.0, isPlayable: false) {
var bufferedSecondsDebouncer: SAAudioAvailabilityRange = SAAudioAvailabilityRange(startingNeedle: 0.0, durationLoadedByNetwork: 0.0, predictedDurationToLoad: Double.greatestFiniteMagnitude, isPlayable: false)
var bufferedSeconds: SAAudioAvailabilityRange = SAAudioAvailabilityRange(startingNeedle: 0.0, durationLoadedByNetwork: 0.0, predictedDurationToLoad: Double.greatestFiniteMagnitude, isPlayable: false) {
didSet {
if bufferedSeconds.startingNeedle == 0.0 && bufferedSeconds.durationLoadedByNetwork == 0.0 {
bufferedSecondsDebouncer = bufferedSeconds
@@ -119,24 +112,35 @@ class AudioEngine: AudioEngineProtocol {
init(url: AudioURL, delegate:AudioEngineDelegate?, engineAudioFormat: AVAudioFormat) {
self.key = url.key
self.delegate = delegate
// https://forums.developer.apple.com/thread/5874
// https://forums.developer.apple.com/thread/6050
// AVAudioTimePitchAlgorithm.timeDomain (just in case we want it)
var componentDescription: AudioComponentDescription {
get {
var ret = AudioComponentDescription()
ret.componentType = kAudioUnitType_FormatConverter
ret.componentSubType = kAudioUnitSubType_AUiPodTimeOther
return ret
}
}
rateNode = AVAudioUnitTimePitch(audioComponentDescription: componentDescription)
engine.attach(playerNode)
engine.attach(rateNode)
engine.connect(playerNode, to: rateNode, format: engineAudioFormat)
engine.connect(rateNode, to: engine.mainMixerNode, format: engineAudioFormat)
for node in SAPlayer.shared.audioModifiers {
engine.attach(node)
}
if SAPlayer.shared.audioModifiers.count > 0 {
var i = 0
let node = SAPlayer.shared.audioModifiers[i]
engine.connect(playerNode, to: node, format: engineAudioFormat)
i += 1
while i < SAPlayer.shared.audioModifiers.count {
let lastNode = SAPlayer.shared.audioModifiers[i - 1]
let currNode = SAPlayer.shared.audioModifiers[i]
engine.connect(lastNode, to: currNode, format: engineAudioFormat)
i += 1
}
let finalNode = SAPlayer.shared.audioModifiers[SAPlayer.shared.audioModifiers.count - 1]
engine.connect(finalNode, to: engine.mainMixerNode, format: engineAudioFormat)
} else {
engine.connect(playerNode, to: engine.mainMixerNode, format: engineAudioFormat)
}
engine.prepare()
}
@@ -149,7 +153,17 @@ class AudioEngine: AudioEngineProtocol {
}
func updateIsPlaying() {
isPlaying = engine.isRunning && playerNode.isPlaying
if !bufferedSeconds.isPlayable {
if bufferedSeconds.bufferingProgress > 0.999 {
playingStatus = .ended
} else {
playingStatus = .buffering
}
return
}
let isPlaying = engine.isRunning && playerNode.isPlaying
playingStatus = isPlaying ? .playing : .paused
}
func play() {
@@ -184,10 +198,6 @@ class AudioEngine: AudioEngineProtocol {
fatalError("No implementation for seek inAudioEngine, should be using streaming or disk type")
}
func setSpeed(speed: Double) {
audioSpeed = speed
}
func invalidate() {
}
+9 -11
View File
@@ -8,7 +8,7 @@
// This file was modified and adapted from https://github.com/syedhali/AudioStreamer
// which was released under Apache License 2.0. Apache License 2.0 requires explicit
// documentation of modified files from source and a copy of the Apache License 2.0
// in the project which he have under the name Credited_LICENSE.
// in the project which is under the name Credited_LICENSE.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -58,6 +58,7 @@ import AVFoundation
class AudioStreamEngine: AudioEngine {
//Constants
private let MAX_POLL_BUFFER_COUNT = 300 //Having one buffer in engine at a time is choppy.
private let MIN_BUFFERS_TO_BE_PLAYABLE = 1
private let PCM_BUFFER_SIZE: AVAudioFrameCount = 8192
private let queue = DispatchQueue(label: "SwiftAudioPlayer.engine", qos: .userInitiated)
@@ -86,7 +87,9 @@ class AudioStreamEngine: AudioEngine {
didSet {
if numberOfBuffersScheduledFromPoll > MAX_POLL_BUFFER_COUNT {
shouldPollForNextBuffer = false
}
if numberOfBuffersScheduledFromPoll > MIN_BUFFERS_TO_BE_PLAYABLE {
if wasPlaying {
play()
wasPlaying = false
@@ -213,9 +216,9 @@ class AudioStreamEngine: AudioEngine {
private func updateNetworkBufferRange() { //for ui
let range = converter.pollNetworkAudioAvailabilityRange()
isPlayable = (numberOfBuffersScheduledInTotal > 0 && range.1 > 0) && predictedStreamDuration > 0
isPlayable = (numberOfBuffersScheduledInTotal >= MIN_BUFFERS_TO_BE_PLAYABLE && range.1 > 0) && predictedStreamDuration > 0
Log.debug("loaded \(range), numberOfBuffersScheduledInTotal: \(numberOfBuffersScheduledInTotal), isPlayable: \(isPlayable)")
bufferedSeconds = SAAudioAvailabilityRange(startingNeedle: range.0, durationLoadedByNetwork: range.1, isPlayable: isPlayable)
bufferedSeconds = SAAudioAvailabilityRange(startingNeedle: range.0, durationLoadedByNetwork: range.1, predictedDurationToLoad: predictedStreamDuration, isPlayable: isPlayable)
}
private func updateNeedle() {
@@ -233,12 +236,6 @@ class AudioStreamEngine: AudioEngine {
var currentTime = TimeInterval(playerTime.sampleTime) / playerTime.sampleRate
currentTime = currentTime > 0 ? currentTime : 0
if currentTime > predictedStreamDuration {
Log.info("reached end of audio")
seek(toNeedle: 0)
pause()
delegate?.didEndPlaying()
}
needle = (currentTime + currentTimeOffset)
}
@@ -262,7 +259,6 @@ class AudioStreamEngine: AudioEngine {
self.needle = needle //to tick while paused
queue.sync { [weak self] in
self?.seekHelperDispatchQueue(needle: needle)
}
@@ -290,6 +286,8 @@ class AudioStreamEngine: AudioEngine {
playerNode.stop()
shouldPollForNextBuffer = true
updateNetworkBufferRange()
}
override func invalidate() {
+48 -11
View File
@@ -36,6 +36,7 @@ protocol AudioThrottleable {
func tellAudioFormatFound()
func tellByteOffset(offset: UInt64)
func tellSeek(offset: UInt64)
func tellBytesPerAudioPacket(count: UInt64)
func pollRangeOfBytesAvailable() -> (UInt64, UInt64)
func invalidate()
}
@@ -47,6 +48,10 @@ class AudioThrottler: AudioThrottleable {
var alreadySent: Bool
var next: NetworkDataWrapper?
var byteCount: UInt {
return UInt(data.count)
}
var endOffset: UInt {
return startOffset + UInt(data.count) - 1
}
@@ -92,7 +97,17 @@ class AudioThrottler: AudioThrottleable {
private var networkData: [NetworkDataWrapper] = []
var shouldThrottle = false
var byteOffsetBecauseOfSeek: UInt = 0
var totalBytesExpected: Int64? //this got sent up twice. Once at beginning of stream and second from network seek. We honor the first send
//This will be sent once at beginning of stream and every network seek
var totalBytesExpected: Int64? {
didSet {
if let bytes = totalBytesExpected {
delegate?.didUpdate(totalBytesExpected: Int64(byteOffsetBecauseOfSeek) + bytes)
}
}
}
var largestPollingOffsetDifference: UInt64 = 1
required init(withRemoteUrl url: AudioURL, withDelegate delegate: AudioThrottleDelegate) {
self.url = url
@@ -103,9 +118,8 @@ class AudioThrottler: AudioThrottleable {
Log.debug("received stream data of size \(pto.getData().count) and progress: \(pto.getProgress())")
self.delegate?.didUpdate(networkStreamProgress: pto.getProgress())
if self.totalBytesExpected == nil, let totalBytesExpected = pto.getTotalBytesExpected() {
if let totalBytesExpected = pto.getTotalBytesExpected() {
self.totalBytesExpected = totalBytesExpected
self.delegate?.didUpdate(totalBytesExpected: totalBytesExpected)
}
let lastItem = self.networkData.last
@@ -131,25 +145,48 @@ class AudioThrottler: AudioThrottleable {
shouldThrottle = true //the above layer has enough info that we can throttle
}
func tellBytesPerAudioPacket(count: UInt64) {
if count > largestPollingOffsetDifference {
largestPollingOffsetDifference = count
}
}
func tellByteOffset(offset: UInt64) {
Log.debug("offset \(offset)")
for wrappedNetworkData in networkData {
if wrappedNetworkData.containsOffset(UInt(offset)) {
Log.debug("offset within network packet of range: \(wrappedNetworkData.startOffset) to \(wrappedNetworkData.endOffset)")
Log.debug("offset: \(offset) within network packet of range: \(wrappedNetworkData.startOffset) to \(wrappedNetworkData.endOffset) is next sent: \(wrappedNetworkData.isNextSent())")
if wrappedNetworkData.alreadySent {
if !wrappedNetworkData.isNextSent() {
if let next = wrappedNetworkData.next {
Log.debug("Sending next network packet with range: \(next.startOffset) to \(next.endOffset)")
next.alreadySent = true
delegate?.shouldProcess(networkData: next.data)
Log.debug("already sent offset: \(offset) within network packet of range: \(wrappedNetworkData.startOffset) to \(wrappedNetworkData.endOffset)")
var bytesSent: UInt = 0
var current = wrappedNetworkData
// Sometimes the next data packet is smaller than a full audio chunk size, so we need to ensure we send up enough packets for the audio chunk. This prevented Issue #4 where tsreaming would randomly get stuck in a state needing more data up the chain.
// https://github.com/tanhakabir/SwiftAudioPlayer/issues/4
while bytesSent < largestPollingOffsetDifference {
if let next = current.next {
if !next.alreadySent {
Log.info("Sending next network packet with range: \(next.startOffset) to \(next.endOffset), have sent \(bytesSent) bytes so far from \(largestPollingOffsetDifference) bytes")
next.alreadySent = true
delegate?.shouldProcess(networkData: next.data)
}
bytesSent += next.byteCount
current = next
} else {
Log.debug("next package doesn't exist, bytes sent so far: \(bytesSent)")
return
}
}
return
}
Log.debug("Found network packet to send with range: \(wrappedNetworkData.startOffset) to \(wrappedNetworkData.endOffset)")
delegate?.shouldProcess(networkData: wrappedNetworkData.data)
Log.info("Found network packet to send with range: \(wrappedNetworkData.startOffset) to \(wrappedNetworkData.endOffset)")
wrappedNetworkData.alreadySent = true
delegate?.shouldProcess(networkData: wrappedNetworkData.data)
return
}
}
+5 -1
View File
@@ -8,7 +8,7 @@
// This file was modified and adapted from https://github.com/syedhali/AudioStreamer
// which was released under Apache License 2.0. Apache License 2.0 requires explicit
// documentation of modified files from source and a copy of the Apache License 2.0
// in the project which he have under the name Credited_LICENSE.
// in the project which is under the name Credited_LICENSE.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -174,6 +174,10 @@ class AudioConverter: AudioConvertable {
}
private func getPacketIndex(forNeedle needle: Needle) -> AVAudioPacketCount? {
guard needle >= 0 else {
Log.error("needle should never be a negative number! needle received: \(needle)")
return nil
}
guard let frame = frameOffset(forTime: TimeInterval(needle)) else { return nil }
guard let framesPerPacket = parser.fileAudioFormat?.streamDescription.pointee.mFramesPerPacket else { return nil }
return AVAudioPacketCount(frame) / AVAudioPacketCount(framesPerPacket)
@@ -8,7 +8,7 @@
// This file was modified and adapted from https://github.com/syedhali/AudioStreamer
// which was released under Apache License 2.0. Apache License 2.0 requires explicit
// documentation of modified files from source and a copy of the Apache License 2.0
// in the project which he have under the name Credited_LICENSE.
// in the project which is under the name Credited_LICENSE.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -57,28 +57,39 @@ public enum ConverterError: LocalizedError {
public var errorDescription: String? {
switch self {
case .cannotLockQueue:
Log.warn("Failed to lock queue")
return "Failed to lock queue"
case .converterFailed(let status):
Log.warn(localizedDescriptionFromConverterError(status))
return localizedDescriptionFromConverterError(status)
case .failedToCreateDestinationFormat:
Log.warn("Failed to create a destination (processing) format")
return "Failed to create a destination (processing) format"
case .failedToCreatePCMBuffer:
Log.warn("Failed to create PCM buffer for reading data")
return "Failed to create PCM buffer for reading data"
case .notEnoughData:
Log.warn("Not enough data for read-conversion operation")
return "Not enough data for read-conversion operation"
case .parserMissingDataFormat:
Log.warn("Parser is missing a valid data format")
return "Parser is missing a valid data format"
case .reachedEndOfFile:
Log.warn("Reached the end of the file")
return "Reached the end of the file"
case .unableToCreateConverter(let status):
return localizedDescriptionFromConverterError(status)
case .superConcerningShouldNeverHappen:
Log.warn("Weird unexpected reader error. Should not have happened")
return "Weird unexpected reader error. Should not have happened"
case .cannotCreatePCMBufferWithoutConverter:
Log.warn("Could not create a PCM Buffer because reader does not have a converter yet")
return "Could not create a PCM Buffer because reader does not have a converter yet"
case .throttleParsingBuffersForEngine:
Log.warn("Preventing the reader from creating more PCM buffers since the player has more than 60 seconds of audio already to play")
return "Preventing the reader from creating more PCM buffers since the player has more than 60 seconds of audio already to play"
case .failedToCreateParser:
Log.warn("Could not create a parser")
return "Could not create a parser"
}
}
@@ -8,7 +8,7 @@
// This file was modified and adapted from https://github.com/syedhali/AudioStreamer
// which was released under Apache License 2.0. Apache License 2.0 requires explicit
// documentation of modified files from source and a copy of the Apache License 2.0
// in the project which he have under the name Credited_LICENSE.
// in the project which is under the name Credited_LICENSE.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -70,7 +70,7 @@ func ConverterListener(_ converter: AudioConverterRef, _ packetCount: UnsafeMuta
let packetByteCount = packet.count //this is not the count of an array
ioData.pointee.mNumberBuffers = 1
ioData.pointee.mBuffers.mData = UnsafeMutableRawPointer.allocate(byteCount: packetByteCount, alignment: 0)
_ = packet.withUnsafeMutableBytes({ (bytes: UnsafeMutablePointer<UInt8>) in
_ = packet.accessMutableBytes({ (bytes: UnsafeMutablePointer<UInt8>) in
memcpy((ioData.pointee.mBuffers.mData?.assumingMemoryBound(to: UInt8.self))!, bytes, packetByteCount)
})
ioData.pointee.mBuffers.mDataByteSize = UInt32(packetByteCount)
+1 -1
View File
@@ -8,7 +8,7 @@
// This file was modified and adapted from https://github.com/syedhali/AudioStreamer
// which was released under Apache License 2.0. Apache License 2.0 requires explicit
// documentation of modified files from source and a copy of the Apache License 2.0
// in the project which he have under the name Credited_LICENSE.
// in the project which is under the name Credited_LICENSE.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
+14 -5
View File
@@ -8,7 +8,7 @@
// This file was modified and adapted from https://github.com/syedhali/AudioStreamer
// which was released under Apache License 2.0. Apache License 2.0 requires explicit
// documentation of modified files from source and a copy of the Apache License 2.0
// in the project which he have under the name Credited_LICENSE.
// in the project which is under the name Credited_LICENSE.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -85,7 +85,9 @@ class AudioParser: AudioParsable {
return max(AVAudioPacketCount(parsedAudioHeaderPacketCount), AVAudioPacketCount(audioPackets.count))
}
guard let sizeOfFileInBytes = expectedFileSizeInBytes, let bytesPerPacket = averageBytesPerPacket else {
let sizeOfFileInBytes: UInt64 = expectedFileSizeInBytes != nil ? expectedFileSizeInBytes! : 0
guard let bytesPerPacket = averageBytesPerPacket else {
return AVAudioPacketCount(0)
}
@@ -98,7 +100,14 @@ class AudioParser: AudioParsable {
return predictedCount
}
var sumOfParsedAudioBytes:UInt32 = 0
var sumOfParsedAudioBytes:UInt32 = 0 {
didSet {
if let byteCount = averageBytesPerPacket {
throttler.tellBytesPerAudioPacket(count: UInt64(byteCount))
}
}
}
var numberOfPacketsParsed:UInt32 = 0
var audioPackets: [(AudioStreamPacketDescription?,Data)] = [] {
didSet {
@@ -163,7 +172,7 @@ class AudioParser: AudioParsable {
if isParsingComplete {
throw ParserError.readerAskingBeyondEndOfFile
} else {
Log.debug("Tried to pull packet at index: \(packetIndex) when only have: \(audioPackets.count)")
Log.debug("Tried to pull packet at index: \(packetIndex) when only have: \(audioPackets.count), we predict \(totalPredictedPacketCount) in total")
throw ParserError.notEnoughDataForReader
}
}
@@ -288,7 +297,7 @@ extension AudioParser: AudioThrottleDelegate {
let sID = self.streamID!
let dataSize = data.count
let _ = try data.withUnsafeBytes({ (bytes:UnsafePointer<UInt8>) in
_ = try data.accessBytes({ (bytes: UnsafePointer<UInt8>) in
let result:OSStatus = AudioFileStreamParseBytes(sID, UInt32(dataSize), bytes, [])
guard result == noErr else {
Log.monitor(ParserError.failedToParseBytes(result).errorDescription as Any)
+1 -1
View File
@@ -8,7 +8,7 @@
// This file was modified and adapted from https://github.com/syedhali/AudioStreamer
// which was released under Apache License 2.0. Apache License 2.0 requires explicit
// documentation of modified files from source and a copy of the Apache License 2.0
// in the project which he have under the name Credited_LICENSE.
// in the project which is under the name Credited_LICENSE.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -8,7 +8,7 @@
// This file was modified and adapted from https://github.com/syedhali/AudioStreamer
// which was released under Apache License 2.0. Apache License 2.0 requires explicit
// documentation of modified files from source and a copy of the Apache License 2.0
// in the project which he have under the name Credited_LICENSE.
// in the project which is under the name Credited_LICENSE.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -8,7 +8,7 @@
// This file was modified and adapted from https://github.com/syedhali/AudioStreamer
// which was released under Apache License 2.0. Apache License 2.0 requires explicit
// documentation of modified files from source and a copy of the Apache License 2.0
// in the project which he have under the name Credited_LICENSE.
// in the project which is under the name Credited_LICENSE.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -29,8 +29,15 @@ import Foundation
public struct SAAudioAvailabilityRange {
let startingNeedle: Needle
let durationLoadedByNetwork: Duration
let predictedDurationToLoad: Duration
let isPlayable: Bool
public var bufferingProgress: Double {
get {
return (startingNeedle + durationLoadedByNetwork) / predictedDurationToLoad
}
}
public var startingBufferTimePositon: Double {
get {
return startingNeedle
@@ -52,4 +59,8 @@ public struct SAAudioAvailabilityRange {
public func contains(_ needle: Double) -> Bool {
return needle >= startingNeedle && (needle - startingNeedle) < durationLoadedByNetwork
}
public func isCompletelyBuffered() -> Bool {
return startingNeedle + durationLoadedByNetwork >= predictedDurationToLoad
}
}
+34
View File
@@ -0,0 +1,34 @@
//
// SAPlayingStatus.swift
// SwiftAudioPlayer
//
// Created by Tanha Kabir on 2019-11-24.
// Copyright © 2019 Tanha Kabir, Jon Mercer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Foundation
public enum SAPlayingStatus {
case playing
case paused
case buffering
case ended
}
+39 -5
View File
@@ -35,10 +35,19 @@ protocol LockScreenViewProtocol {
}
extension LockScreenViewProtocol {
func clearLockScreenInfo() {
MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]
}
@available(iOS 10.0, *)
func setLockScreenInfo(withMediaInfo info: SALockScreenInfo, duration: Duration) {
func setLockScreenInfo(withMediaInfo info: SALockScreenInfo?, duration: Duration) {
var nowPlayingInfo:[String : Any] = [:]
guard let info = info else {
MPNowPlayingInfoCenter.default().nowPlayingInfo = [:]
return
}
let title = info.title
let artist = info.artist
let releaseDate = info.releaseDate
@@ -56,12 +65,18 @@ extension LockScreenViewProtocol {
nowPlayingInfo[MPMediaItemPropertyPodcastTitle] = title
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = 0.0 //because default is 1.0. If we pause audio then it keeps ticking
nowPlayingInfo[MPMediaItemPropertyReleaseDate] = Date(timeIntervalSince1970: TimeInterval(releaseDate))
nowPlayingInfo[MPMediaItemPropertyArtwork] =
MPMediaItemArtwork(boundsSize: info.artwork.size) { size in
return info.artwork
if let artwork = info.artwork {
nowPlayingInfo[MPMediaItemPropertyArtwork] =
MPMediaItemArtwork(boundsSize: artwork.size) { size in
return artwork
}
} else {
nowPlayingInfo[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: UIImage().size) { size in
return UIImage()
}
}
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
}
@@ -137,4 +152,23 @@ extension LockScreenViewProtocol {
func updateLockscreenPlaybackDuration(duration: Duration) {
MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPMediaItemPropertyPlaybackDuration] = NSNumber(value: duration)
}
func updateLockscreenPaused(){
MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0.0
}
func updateLockscreenPlaying(){
MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 1.0
}
func updateLockscreenChangePlaybackRate(speed: Float){
if speed > 0.0{
MPNowPlayingInfoCenter.default().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = speed
}
}
func updateLockscreenSkipIntervals() {
MPRemoteCommandCenter.shared().skipBackwardCommand.preferredIntervals = [skipBackwardSeconds] as [NSNumber]
MPRemoteCommandCenter.shared().skipForwardCommand.preferredIntervals = [skipForwardSeconds] as [NSNumber]
}
}
+12 -6
View File
@@ -45,8 +45,9 @@ protocol AudioDataManagable {
func deleteStream(withRemoteURL url: AudioURL)
func getPersistedUrl(withRemoteURL url: AudioURL) -> URL?
func startDownload(withRemoteURL url: AudioURL)
func deleteDownload(withRemoteURL url: AudioURL)
func startDownload(withRemoteURL url: AudioURL, completion: @escaping (URL) -> ())
func cancelDownload(withRemoteURL url: AudioURL)
func deleteDownload(withLocalURL url: URL)
}
class AudioDataManager: AudioDataManagable {
@@ -152,11 +153,12 @@ extension AudioDataManager {
return FileStorage.Audio.locate(url.key)
}
func startDownload(withRemoteURL url: AudioURL) {
func startDownload(withRemoteURL url: AudioURL, completion: @escaping (URL) -> ()) {
let key = url.key
if FileStorage.Audio.isStored(key) {
if let savedUrl = FileStorage.Audio.locate(key), FileStorage.Audio.isStored(key) {
globalDownloadProgressCallback(key, 1.0)
completion(savedUrl)
return
}
@@ -171,13 +173,17 @@ extension AudioDataManager {
return
}
downloadWorker.start(withID: key, withRemoteUrl: url, withResumeData: nil)
downloadWorker.start(withID: key, withRemoteUrl: url, completion: completion)
}
func deleteDownload(withRemoteURL url: AudioURL) {
func cancelDownload(withRemoteURL url: AudioURL) {
downloadWorker.stop(withID: url.key, callback: nil)
FileStorage.Audio.delete(url.key)
}
func deleteDownload(withLocalURL url: URL) {
FileStorage.delete(url)
}
}
// MARK:- Listeners
@@ -33,7 +33,7 @@ protocol AudioDataDownloadable: AnyObject {
func getProgressOfDownload(withID id: ID) -> Double?
func start(withID id: ID, withRemoteUrl remoteUrl: URL, withResumeData data: Data?)
func start(withID id: ID, withRemoteUrl remoteUrl: URL, completion: @escaping (URL) -> ())
func stop(withID id: ID, callback: ((_ dataSoFar: Data?, _ totalBytesExpected: Int64?) -> ())?)
func pauseAllActive() //Because of streaming
func resumeAllActive() //Because of streaming
@@ -85,31 +85,36 @@ class AudioDownloadWorker: NSObject, AudioDataDownloadable {
return activeDownloads.filter { $0.info.id == id }.first?.progress
}
func start(withID id: ID, withRemoteUrl remoteUrl: URL, withResumeData data: Data? = nil) {
Log.info("paramID: \(id) activeDownloadIDs: \((activeDownloads.map { $0.info.id } ).toLog)")
func start(withID id: ID, withRemoteUrl remoteUrl: URL, completion: @escaping (URL) -> ()) {
Log.info("startExternal paramID: \(id) activeDownloadIDs: \((activeDownloads.map { $0.info.id } ).toLog)")
let temp = activeDownloads.filter { $0.info.id == id }.count
guard temp == 0 else {
return
}
let rank = Date.getUTC()
let info = queuedDownloads.updatePreservingOldCompletionHandlers(withID: id, withRemoteUrl: remoteUrl, completion: completion)
guard numberOfActive < MAX_CONCURRENT_DOWNLOADS else {
queuedDownloads.update(with: DownloadInfo(id: id, remoteUrl: remoteUrl, rank: rank))
start(withInfo: info)
}
fileprivate func start(withInfo info: DownloadInfo) {
Log.info("paramID: \(info.id) activeDownloadIDs: \((activeDownloads.map { $0.info.id } ).toLog)")
let temp = activeDownloads.filter { $0.info.id == info.id }.count
guard temp == 0 else {
return
}
var task: URLSessionDownloadTask
if let resumeData = data {
task = session.downloadTask(withResumeData: resumeData)
} else {
task = session.downloadTask(with: remoteUrl)
guard numberOfActive < MAX_CONCURRENT_DOWNLOADS else {
_ = queuedDownloads.updatePreservingOldCompletionHandlers(withID: info.id, withRemoteUrl: info.remoteUrl)
return
}
task.taskDescription = id
queuedDownloads.remove(info)
let activeTask = ActiveDownload(info: DownloadInfo(id: id, remoteUrl: remoteUrl, rank: rank), task: task)
let task: URLSessionDownloadTask = session.downloadTask(with: info.remoteUrl)
task.taskDescription = info.id
let activeTask = ActiveDownload(info: info, task: task)
activeDownloads.append(activeTask)
activeTask.task.resume()
@@ -145,6 +150,7 @@ class AudioDownloadWorker: NSObject, AudioDataDownloadable {
}
}
queuedDownloads.remove(withMatchingId: id)
callback?(nil, nil)
}
}
@@ -189,10 +195,15 @@ extension AudioDownloadWorker: URLSessionDownloadDelegate {
}
completionHandler(task.info.id, nil)
for handler in task.info.completionHandlers {
handler(destinationUrl)
}
activeDownloads = activeDownloads.filter { $0 != task }
if let queued = queuedDownloads.popHighestRanked() {
start(withID: queued.id, withRemoteUrl: queued.remoteUrl)
start(withInfo: queued)
}
}
@@ -256,16 +267,22 @@ extension AudioDownloadWorker {
// MARK:- Helper Classes
extension AudioDownloadWorker {
fileprivate struct DownloadInfo: Hashable {
static func == (lhs: AudioDownloadWorker.DownloadInfo, rhs: AudioDownloadWorker.DownloadInfo) -> Bool {
return lhs.id == rhs.id && lhs.remoteUrl == rhs.remoteUrl
}
let id: ID
let remoteUrl: URL
let rank: Int
var completionHandlers: [(URL) -> ()]
func hash(into hasher: inout Hasher) {
hasher.combine(id)
hasher.combine(remoteUrl)
}
}
private class ActiveDownload: Hashable {
var hashValue: Int {
return info.id.hashValue ^ task.hashValue
}
static func == (lhs: AudioDownloadWorker.ActiveDownload, rhs: AudioDownloadWorker.ActiveDownload) -> Bool {
return lhs.info.id == rhs.info.id
}
@@ -279,6 +296,11 @@ extension AudioDownloadWorker {
self.info = info
self.task = task
}
func hash(into hasher: inout Hasher) {
hasher.combine(info.id)
hasher.combine(task)
}
}
}
@@ -298,6 +320,47 @@ extension Set where Element == AudioDownloadWorker.DownloadInfo {
return ret
}
mutating func updatePreservingOldCompletionHandlers(withID id: ID, withRemoteUrl remoteUrl: URL, completion: ((URL) -> ())? = nil) -> AudioDownloadWorker.DownloadInfo {
let rank = Date.getUTC()
let tempHandlers: [(URL) -> ()] = completion != nil ? [completion!] : []
var newInfo = AudioDownloadWorker.DownloadInfo.init(id: id, remoteUrl: remoteUrl, rank: rank, completionHandlers: tempHandlers)
if let previous = self.update(with: newInfo) {
let prevHandlers = previous.completionHandlers
let newHandlers = prevHandlers + tempHandlers
newInfo = AudioDownloadWorker.DownloadInfo.init(id: id, remoteUrl: remoteUrl, rank: rank, completionHandlers: newHandlers)
self.update(with: newInfo)
}
return newInfo
}
mutating func remove(withMatchingId id: ID) {
var toRemove: AudioDownloadWorker.DownloadInfo? = nil
var matchCount = 0
for item in self.enumerated() {
if item.element.id == id {
toRemove = item.element
matchCount += 1
}
}
guard matchCount <= 1 else {
Log.error("Found \(matchCount) matches of queued info with the same id of: \(id), this should have never happened.")
return
}
if let removeInfo = toRemove {
self.remove(removeInfo)
}
}
}
extension String {
+3 -3
View File
@@ -36,19 +36,19 @@ struct FileStorage {
Note: It is not guaranteed that the file actually exists.
*/
private static func getUrl(givenAName name: NameFile, inDirectory dir: FileManager.SearchPathDirectory) -> URL {
static func getUrl(givenAName name: NameFile, inDirectory dir: FileManager.SearchPathDirectory) -> URL {
let directoryPath = NSSearchPathForDirectoriesInDomains(dir, .userDomainMask, true)[0] as String
let url = URL(fileURLWithPath: directoryPath)
return url.appendingPathComponent(name)
}
private static func isStored(_ url: URL) -> Bool{
static func isStored(_ url: URL) -> Bool{
// https://stackoverflow.com/questions/42897844/swift-3-0-filemanager-fileexistsatpath-always-return-false
// When determining if a file exists, we must use .path not .absolute string!
return FileManager.default.fileExists(atPath: url.path)
}
private static func delete(_ url: URL) {
static func delete(_ url: URL) {
if !isStored(url) {
return
}
@@ -247,11 +247,15 @@ extension AudioStreamWorker: URLSessionDataDelegate {
return
}
guard let totalBytesExpected = totalBytesExpectedForCurrentStream, totalBytesExpected > 0 else {
guard var totalBytesExpected = totalBytesExpectedForCurrentStream else {
Log.monitor("should not be called 223r2")
return
}
if totalBytesExpected <= 0 {
totalBytesExpected = totalBytesReceived
}
totalBytesReceived = totalBytesReceived + Int64(data.count)
let progress = Double(totalBytesReceived)/Double(totalBytesExpected)
+8 -2
View File
@@ -26,15 +26,21 @@
import Foundation
import UIKit
/**
UTC corresponds to epoch time (number of seconds that have elapsed since January 1, 1970, midnight UTC/GMT). https://www.epochconverter.com/ is a useful site to convert to human readable format.
*/
public typealias UTC = Int
/**
Use to set what will be displayed in the lockscreen.
*/
public struct SALockScreenInfo {
var title: String
var artist: String
var artwork: UIImage
var artwork: UIImage?
var releaseDate: UTC
public init(title: String, artist: String, artwork: UIImage, releaseDate: UTC) {
public init(title: String, artist: String, artwork: UIImage?, releaseDate: UTC) {
self.title = title
self.artist = artist
self.artwork = artwork
+229 -52
View File
@@ -27,55 +27,166 @@ import Foundation
import AVFoundation
public class SAPlayer {
public var DEBUG_MODE: Bool = false {
didSet {
if(DEBUG_MODE) {
logLevel = LogLevel.EXTERNAL_DEBUG
} else {
logLevel = LogLevel.MONITOR
}
}
}
/**
Access to the player.
*/
public static let shared: SAPlayer = SAPlayer()
private var presenter: SAPlayerPresenter!
private var player: AudioEngine?
public var skipForwardSeconds: Double = 30
public var skipBackwardSeconds: Double = 15
/**
Access the engine of the player. Engine is nil if player has not been initialized with audio.
- Important: Changes to the engine are not safe guarded, thus unknown behaviour can arise from changing the engine. Just be wary and read [documentation of AVAudioEngine](https://developer.apple.com/documentation/avfoundation/avaudioengine) well when modifying,
*/
public var engine: AVAudioEngine? {
get {
return player?.engine
}
}
public var rate: Double = 1.0 {
/**
Corresponding to the overall volume of the player. Volume's default value is 1.0 and the range of valid values is 0.0 to 1.0. Volume is nil if no audio has been initialized yet.
*/
public var volume: Float? {
get {
return player?.engine.mainMixerNode.volume
}
set {
guard let value = newValue else { return }
guard value >= 0.0 && value <= 1.0 else { return }
player?.engine.mainMixerNode.volume = value
}
}
/**
Corresponding to the skipping forward button on the media player on the lockscreen. Default is set to 30 seconds.
*/
public var skipForwardSeconds: Double = 30 {
didSet {
presenter.handleSetSpeed(withMultiple: rate)
presenter.handleScrubbingIntervalsChanged()
}
}
public var duration: Double {
/**
Corresponding to the skipping backwards button on the media player on the lockscreen. Default is set to 15 seconds.
*/
public var skipBackwardSeconds: Double = 15 {
didSet {
presenter.handleScrubbingIntervalsChanged()
}
}
/**
List of [AVAudioUnit](https://developer.apple.com/documentation/avfoundation/audio_track_engineering/audio_engine_building_blocks/audio_enhancements) audio modifiers to pass to the engine on initialization.
- Important: To have the intended effects, the list of modifiers must be finalized before initializing the audio to be played. The modifers are added to the engine in order of the list.
- Note: The default list already has an AVAudioUnitTimePitch node first in the list. This node is specifically set to change the rate of audio without changing the pitch of the audio (intended for changing the rate of spoken word).
The component description of this node is:
````
var componentDescription: AudioComponentDescription {
get {
var ret = AudioComponentDescription()
ret.componentType = kAudioUnitType_FormatConverter
ret.componentSubType = kAudioUnitSubType_AUiPodTimeOther
return ret
}
}
````
Please look at [forums.developer.apple.com/thread/5874](https://forums.developer.apple.com/thread/5874) and [forums.developer.apple.com/thread/6050](https://forums.developer.apple.com/thread/6050) for more details.
*/
public var audioModifiers: [AVAudioUnit] = []
/**
Total duration of current audio initialized. Returns nil if no audio is initialized in player.
- Note: If you are streaming from a source that does not have an expected size at the beginning of a stream, such as live streams, this value will be constantly updating to best known value at the time.
*/
public var duration: Double? {
get {
return presenter.duration ?? 0.0
return presenter.duration
}
}
public var prettyDuration: String {
/**
A textual representation of the duration of the current audio initialized. Returns nil if no audio is initialized in player.
*/
public var prettyDuration: String? {
get {
return SAPlayer.prettifyTimestamp(duration)
guard let d = duration else { return nil }
return SAPlayer.prettifyTimestamp(d)
}
}
public var elapsedTime: Double {
/**
Elapsed playback time of the current audio initialized. Returns nil if no audio is initialized in player.
*/
public var elapsedTime: Double? {
get {
return presenter.needle ?? 0
return presenter.needle
}
}
public var prettyElapsedTime: String {
/**
A textual representation of the elapsed playback time of the current audio initialized. Returns nil if no audio is initialized in player.
*/
public var prettyElapsedTime: String? {
get {
return SAPlayer.prettifyTimestamp(elapsedTime)
guard let e = elapsedTime else { return nil }
return SAPlayer.prettifyTimestamp(e)
}
}
/**
Corresponding to the media info to display on the lockscreen for the current audio.
- Note: Setting this to nil clears the information displayed on the lockscreen media player.
*/
public var mediaInfo: SALockScreenInfo? = nil {
didSet {
if let info = mediaInfo {
presenter.handleLockscreenInfo(info: info)
}
presenter.handleLockscreenInfo(info: mediaInfo)
}
}
private init() {
presenter = SAPlayerPresenter(delegate: self)
// https://forums.developer.apple.com/thread/5874
// https://forums.developer.apple.com/thread/6050
// AVAudioTimePitchAlgorithm.timeDomain (just in case we want it)
var componentDescription: AudioComponentDescription {
get {
var ret = AudioComponentDescription()
ret.componentType = kAudioUnitType_FormatConverter
ret.componentSubType = kAudioUnitSubType_AUiPodTimeOther
return ret
}
}
audioModifiers.append(AVAudioUnitTimePitch(audioComponentDescription: componentDescription))
}
/**
Formats a textual representation of a given timestamp for display in hh:MM:SS format, that is hours:minutes:seconds.
- Parameter timestamp: The timestamp to format.
- Returns: A textual representation of the given timestamp
*/
public static func prettifyTimestamp(_ timestamp: Double) -> String {
let hours = Int(timestamp / 60 / 60)
let minutes = Int((timestamp - Double(hours * 60)) / 60)
@@ -94,63 +205,124 @@ public class SAPlayer {
}
}
//MARK: - Player Controls
//MARK: - External Player Controls
extension SAPlayer {
/**
Toggles between the play and pause state of the player. If nothing is playable (aka still in buffering state or no audio is initialized) no action will be taken. Please call `startSavedAudio` or `startRemoteAudio` to set up the player with audio before this.
- Note: If you are streaming, wait till the status from `SAPlayer.Updates.PlayingStatus` is not `.buffering`.
*/
public func togglePlayAndPause() {
presenter.handleTogglePlayingAndPausing()
}
/**
Attempts to play the player. If nothing is playable (aka still in buffering state or no audio is initialized) no action will be taken. Please call `startSavedAudio` or `startRemoteAudio` to set up the player with audio before this.
- Note: If you are streaming, wait till the status from `SAPlayer.Updates.PlayingStatus` is not `.buffering`.
*/
public func play() {
presenter.handlePlay()
}
/**
Attempts to pause the player. If nothing is playable (aka still in buffering state or no audio is initialized) no action will be taken. Please call `startSavedAudio` or `startRemoteAudio` to set up the player with audio before this.
- Note:If you are streaming, wait till the status from `SAPlayer.Updates.PlayingStatus` is not `.buffering`.
*/
public func pause() {
presenter.handlePause()
}
public func skipBackwards() {
presenter.handleSkipBackward()
}
/**
Attempts to skip forward in audio even if nothing playable is loaded (aka still in buffering state or no audio is initialized). The interval to which to skip forward is defined by `SAPlayer.shared.skipForwardSeconds`.
- Note: The skipping is limited to the duration of the audio, if the intended skip is past the duration of the current audio, the skip will just go to the end.
*/
public func skipForward() {
presenter.handleSkipForward()
}
/**
Attempts to skip backwards in audio even if nothing playable is loaded (aka still in buffering state or no audio is initialized). The interval to which to skip backwards is defined by `SAPlayer.shared.skipBackwardSeconds`.
- Note: The skipping is limited to the playable timestamps, if the intended skip is below 0 seconds, the skip will just go to 0 seconds.
*/
public func skipBackwards() {
presenter.handleSkipBackward()
}
/**
Attempts to seek/scrub through the audio even if nothing playable is loaded (aka still in buffering state or no audio is initialized).
- Parameter seconds: The intended seconds within the audio to seek to.
- Note: The seeking is limited to the playable timestamps, if the intended seek is below 0 seconds, the skip will just go to 0 seconds. If the intended seek is past the curation of the current audio, the seek will just go to the end.
*/
public func seekTo(seconds: Double) {
presenter.handleSeek(toNeedle: seconds)
}
public func initializeAudio(withRemoteUrl url: URL, mediaInfo: SALockScreenInfo? = nil) {
/**
If using an AVAudioUnitTimePitch, it's important to notify the player that the rate at which the audio playing has changed to keep the media player in the lockscreen up to date. This is only important for playback rate changes.
- Parameter rate: The current rate at which the audio is playing.
*/
public func playbackRateOfAudioChanged(rate: Float) {
presenter.handleAudioRateChanged(rate: rate)
}
/**
Sets up player to play audio that has been saved on the device.
- Important: If intending to use [AVAudioUnit](https://developer.apple.com/documentation/avfoundation/audio_track_engineering/audio_engine_building_blocks/audio_enhancements) audio modifiers during playback, the list of audio modifiers under `SAPlayer.shared.audioModifiers` must be finalized before calling this function. After all realtime audio manipulations within the this will be effective.
- Parameter withSavedUrl: The URL of the audio saved on the device.
- Parameter mediaInfo: The media information of the audio to show on the lockscreen media player (optional).
*/
public func startSavedAudio(withSavedUrl url: URL, mediaInfo: SALockScreenInfo? = nil) {
self.mediaInfo = mediaInfo
presenter.handlePlayAudio(withRemoteUrl: url)
presenter.handlePlaySavedAudio(withSavedUrl: url)
}
@available(*, deprecated, renamed: "startSavedAudio")
public func initializeSavedAudio(withSavedUrl url: URL, mediaInfo: SALockScreenInfo? = nil) {
self.mediaInfo = mediaInfo
presenter.handlePlaySavedAudio(withSavedUrl: url)
}
/**
Sets up player to play audio that will be streamed from a remote location. After this is called, it will connect to the server and start to receive and process data. The player is not playable the SAAudioAvailabilityRange notifies that player is ready for playing (you can subscribe to these updates through `SAPlayer.Updates.StreamingBuffer`). You can alternatively see when the player is available to play by subscribing to `SAPlayer.Updates.PlayingStatus` and waiting for a status that isn't `.buffering`.
- Important: If intending to use [AVAudioUnit](https://developer.apple.com/documentation/avfoundation/audio_track_engineering/audio_engine_building_blocks/audio_enhancements) audio modifiers during playback, the list of audio modifiers under `SAPlayer.shared.audioModifiers` must be finalized before calling this function. After all realtime audio manipulations within the this will be effective.
- Note: Subscribe to `SAPlayer.Updates.StreamingBuffer` to see updates in streaming progress.
- Parameter withRemoteUrl: The URL of the remote audio.
- Parameter mediaInfo: The media information of the audio to show on the lockscreen media player (optional).
*/
public func startRemoteAudio(withRemoteUrl url: URL, mediaInfo: SALockScreenInfo? = nil) {
self.mediaInfo = mediaInfo
presenter.handlePlayStreamedAudio(withRemoteUrl: url)
}
@available(*, deprecated, renamed: "startRemoteAudio")
public func initializeRemoteAudio(withRemoteUrl url: URL, mediaInfo: SALockScreenInfo? = nil) {
self.mediaInfo = mediaInfo
presenter.handlePlayStreamedAudio(withRemoteUrl: url)
}
/**
Resets the player to the state before initializing audio and setting media info.
*/
public func clear() {
player = nil
presenter.handleClear()
}
}
extension SAPlayer {
public struct Downloader {
public static func downloadAudio(withRemoteUrl url: URL) {
SAPlayer.shared.addUrlToMapping(url: url)
AudioDataManager.shared.startDownload(withRemoteURL: url)
}
public static func cancelDownload(withRemoteUrl url: URL) {
AudioDataManager.shared.deleteDownload(withRemoteURL: url)
}
public static func deleteDownload(withRemoteUrl url: URL) {
AudioDataManager.shared.deleteDownload(withRemoteURL: url)
}
public static func isDownloaded(withRemoteUrl url: URL) -> Bool {
return AudioDataManager.shared.getPersistedUrl(withRemoteURL: url) != nil
}
public static func setBackgroundCompletionHandler(_ completionHandler: @escaping () -> ()) {
AudioDataManager.shared.setBackgroundCompletionHandler(completionHandler)
}
}
}
//MARK: - Internal implementation of delegate
extension SAPlayer: SAPlayerDelegate {
func startAudioDownloaded(withSavedUrl url: AudioURL) {
player?.pause()
@@ -177,7 +349,9 @@ extension SAPlayer: SAPlayerDelegate {
} else {
// Fallback on earlier versions
}
try AVAudioSession.sharedInstance().setActive(true, with: .notifyOthersOnDeactivation)
try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: AVAudioSession.Mode(rawValue: convertFromAVAudioSessionMode(AVAudioSession.Mode.default)), options: .allowAirPlay)
try AVAudioSession.sharedInstance().setActive(true, options: .notifyOthersOnDeactivation)
} catch {
Log.monitor("Problem setting up AVAudioSession to play in:: \(error.localizedDescription)")
}
@@ -188,11 +362,14 @@ extension SAPlayer: SAPlayerDelegate {
}
func seekEngine(toNeedle needle: Needle) {
player?.seek(toNeedle: needle)
}
func setSpeedEngine(withMultiple multiple: Double) {
player?.setSpeed(speed: multiple)
var seekToNeedle = needle < 0 ? 0 : needle
seekToNeedle = needle > Needle(duration ?? 0) ? Needle(duration ?? 0) : needle
player?.seek(toNeedle: seekToNeedle)
}
}
// Helper function inserted by Swift 4.2 migrator.
fileprivate func convertFromAVAudioSessionMode(_ input: AVAudioSession.Mode) -> String {
return input.rawValue
}
-1
View File
@@ -35,5 +35,4 @@ protocol SAPlayerDelegate: AnyObject, LockScreenViewProtocol {
func playEngine()
func pauseEngine()
func seekEngine(toNeedle needle: Needle) //TODO ensure that engine cleans up out of bounds
func setSpeedEngine(withMultiple multiple: Double)
}
+104
View File
@@ -0,0 +1,104 @@
//
// SAPlayerDownloader.swift
// SwiftAudioPlayer
//
// Created by Tanha Kabir on 2019-02-25.
// Copyright © 2019 Tanha Kabir, Jon Mercer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Foundation
extension SAPlayer {
/**
Actions relating to downloading remote audio to the device for offline playback.
- Note: All saved urls generated from downloaded audio corresponds to a specific remote url. Thus, can be queryed if original remote url is known.
- Important: Please ensure that you have passed in the background download completion handler in the AppDelegate with `setBackgroundCompletionHandler` to allow for downloading audio while app is in the background.
*/
public struct Downloader {
/**
Download audio from a remote url. Will save the audio on the device for playback later.
Save the saved url of the downloaded audio for future playback or query for the saved url with the same remote url in the future.
- Note: It's recommended to have a weak reference to a class that uses this function
- Note: Subscribe to `SAPlayer.Updates.AudioDownloading` to see updates in downloading progress.
- Parameter url: The remote url to download audio from.
- Parameter completion: Completion handler that will return once the download is successful and complete.
- Parameter savedUrl: The url of where the audio was saved locally on the device. Will receive once download has completed.
*/
public static func downloadAudio(withRemoteUrl url: URL, completion: @escaping (_ savedUrl: URL) -> ()) {
SAPlayer.shared.addUrlToMapping(url: url)
AudioDataManager.shared.startDownload(withRemoteURL: url, completion: completion)
}
/**
Cancel downloading audio from a specific remote url if actively downloading. If download has not started yet, it will remove from the list of future downloads queued.
- Parameter url: The remote url corresponding to the active download you want to cancel.
*/
public static func cancelDownload(withRemoteUrl url: URL) {
AudioDataManager.shared.cancelDownload(withRemoteURL: url)
}
/**
Delete downloaded audio file from device at url.
- Note: This will delete any file saved on device at the local url. This, however, is intended to use for audio files.
- Parameter url: The url of the audio to delete from the device.
*/
public static func deleteDownloaded(withSavedUrl url: URL) {
AudioDataManager.shared.deleteDownload(withLocalURL: url)
}
/**
Check if audio at remote url is downloaded on device.
- Parameter url: The remote url corresponding to the audio file you want to see if downloaded.
- Returns: Whether of not file at remote url is downloaded on device.
*/
public static func isDownloaded(withRemoteUrl url: URL) -> Bool {
return AudioDataManager.shared.getPersistedUrl(withRemoteURL: url) != nil
}
/**
Get url of audio file downloaded from remote url onto on device if it exists.
- Parameter url: The remote url corresponding to the audio file you want the device url of.
- Returns: Url of audio file on device if it exists.
*/
public static func getSavedUrl(forRemoteUrl url: URL) -> URL? {
return AudioDataManager.shared.getPersistedUrl(withRemoteURL: url)
}
/**
Pass along the completion handler from `AppDelegate` to ensure downloading continues while app is in background.
- Parameter completionHandler: The completion hander from `AppDelegate` to use for app in the background downloads.
*/
public static func setBackgroundCompletionHandler(_ completionHandler: @escaping () -> ()) {
AudioDataManager.shared.setBackgroundCompletionHandler(completionHandler)
}
}
}
+38 -20
View File
@@ -35,7 +35,7 @@ class SAPlayerPresenter {
var duration: Duration?
private var key: String?
private var isPlaying = false
private var isPlaying: SAPlayingStatus = .buffering
private var mediaInfo: SALockScreenInfo?
private var urlKeyMap: [Key: URL] = [:]
@@ -60,21 +60,35 @@ class SAPlayerPresenter {
urlKeyMap[url.key] = url
}
func handlePlayAudio(withRemoteUrl url: URL) {
func handleClear() {
needle = nil
duration = nil
key = nil
mediaInfo = nil
delegate?.clearLockScreenInfo()
AudioClockDirector.shared.detachFromChangesInDuration(withID: durationRef)
AudioClockDirector.shared.detachFromChangesInNeedle(withID: needleRef)
AudioClockDirector.shared.detachFromChangesInPlayingStatus(withID: playingStatusRef)
}
func handlePlaySavedAudio(withSavedUrl url: URL) {
attachForUpdates(url: url)
delegate?.startAudioDownloaded(withSavedUrl: url)
}
func handlePlayStreamedAudio(withRemoteUrl url: URL) {
attachForUpdates(url: url)
delegate?.startAudioStreamed(withRemoteUrl: url)
}
private func attachForUpdates(url: URL) {
AudioClockDirector.shared.detachFromChangesInDuration(withID: durationRef)
AudioClockDirector.shared.detachFromChangesInNeedle(withID: needleRef)
AudioClockDirector.shared.detachFromChangesInPlayingStatus(withID: playingStatusRef)
self.key = url.key
if let savedUrl = AudioDataManager.shared.getPersistedUrl(withRemoteURL: url) {
self.key = savedUrl.key
urlKeyMap[savedUrl.key] = url
delegate?.startAudioDownloaded(withSavedUrl: savedUrl)
} else {
urlKeyMap[url.key] = url
delegate?.startAudioStreamed(withRemoteUrl: url)
}
urlKeyMap[url.key] = url
durationRef = AudioClockDirector.shared.attachToChangesInDuration(closure: { [weak self] (key, duration) in
guard let self = self else { throw DirectorError.closureIsDead }
@@ -86,9 +100,7 @@ class SAPlayerPresenter {
self.delegate?.updateLockscreenPlaybackDuration(duration: duration)
self.duration = duration
if let info = self.mediaInfo {
self.delegate?.setLockScreenInfo(withMediaInfo: info, duration: duration)
}
self.delegate?.setLockScreenInfo(withMediaInfo: self.mediaInfo, duration: duration)
})
needleRef = AudioClockDirector.shared.attachToChangesInNeedle(closure: { [weak self] (key, needle) in
@@ -114,7 +126,7 @@ class SAPlayerPresenter {
}
@available(iOS 10.0, *)
func handleLockscreenInfo(info: SALockScreenInfo) {
func handleLockscreenInfo(info: SALockScreenInfo?) {
self.mediaInfo = info
}
}
@@ -124,16 +136,18 @@ class SAPlayerPresenter {
extension SAPlayerPresenter {
func handlePause() {
delegate?.pauseEngine()
self.delegate?.updateLockscreenPaused()
}
func handlePlay() {
delegate?.playEngine()
self.delegate?.updateLockscreenPlaying()
}
func handleTogglePlayingAndPausing() {
if isPlaying {
if isPlaying == .playing {
handlePause()
} else {
} else if isPlaying == .paused {
handlePlay()
}
}
@@ -152,15 +166,19 @@ extension SAPlayerPresenter {
delegate?.seekEngine(toNeedle: needle)
}
func handleSetSpeed(withMultiple: Double) {
delegate?.setSpeedEngine(withMultiple: withMultiple)
func handleAudioRateChanged(rate: Float) {
delegate?.updateLockscreenChangePlaybackRate(speed: rate)
}
func handleScrubbingIntervalsChanged() {
delegate?.updateLockscreenSkipIntervals()
}
}
//MARK:- For lock screen
extension SAPlayerPresenter {
func getIsPlaying() -> Bool {
return isPlaying
return isPlaying == .playing
}
}
+13 -7
View File
@@ -40,7 +40,7 @@ extension SAPlayer {
/**
Subscribe to updates in elapsed time of the playing audio. Aka, the current timestamp of the audio.
Note: It's recommended to have a weak reference to a class that uses this fuction
- Note: It's recommended to have a weak reference to a class that uses this function
- Parameter closure: The closure that will receive the updates of the changes in time.
- Parameter url: The corresponding remote URL for the updated playing time.
@@ -66,13 +66,17 @@ extension SAPlayer {
/**
Updates to changes in the duration of the current initialized audio. Especially helpful for audio that is being streamed and can change with more data.
- Note: If you are streaming from a source that does not have an expected size at the beginning of a stream, such as live streams, duration will be constantly updating to best known value at the time (which is the seconds buffered currently and not necessarily the actual total duration of audio).
*/
public struct Duration {
/**
Subscribe to updates to changes in duration of the current audio initialized.
Note: It's recommended to have a weak reference to a class that uses this fuction
- Note: If you are streaming from a source that does not have an expected size at the beginning of a stream, such as live streams, duration will be constantly updating to best known value at the time (which is the seconds buffered currently and not necessarily the actual total duration of audio).
- Note: It's recommended to have a weak reference to a class that uses this function
- Parameter closure: The closure that will receive the updates of the changes in duration.
- Parameter url: The corresponding remote URL for the updated duration.
@@ -104,14 +108,14 @@ extension SAPlayer {
/**
Subscribe to updates to changes in the playing/paused status of audio.
Note: It's recommended to have a weak reference to a class that uses this fuction
- Note: It's recommended to have a weak reference to a class that uses this function
- Parameter closure: The closure that will receive the updates of the changes in duration.
- Parameter url: The corresponding remote URL for the updated duration.
- Parameter playingStatus: Whether the player is playing audio or paused.
- Returns: the id for the subscription in the case you would like to unsubscribe to updates for the closure.
*/
public static func subscribe(_ closure: @escaping (_ url: URL, _ playingStatus: Bool) -> ()) -> UInt {
public static func subscribe(_ closure: @escaping (_ url: URL, _ playingStatus: SAPlayingStatus) -> ()) -> UInt {
return AudioClockDirector.shared.attachToChangesInPlayingStatus(closure: { (key, isPlaying) in
guard let url = SAPlayer.shared.getUrl(forKey: key) else { return }
closure(url, isPlaying)
@@ -129,14 +133,16 @@ extension SAPlayer {
}
/**
Updates to changes in the progress of downloading audio for streaming. Information about range of audio available and if the audio is playable. Look at SAAudioAvailabilityRange for more information.
Updates to changes in the progress of downloading audio for streaming. Information about range of audio available and if the audio is playable. Look at `SAAudioAvailabilityRange` for more information.
*/
public struct StreamingBuffer {
/**
Subscribe to updates to changes in the progress of downloading audio for streaming. Information about range of audio available and if the audio is playable. Look at SAAudioAvailabilityRange for more information. For progress of downloading audio that saves to the phone for playback later, look at AudioDownloading instead.
Note: It's recommended to have a weak reference to a class that uses this fuction
- Note: For live streams that don't have an expected audio length from the beginning of the stream; the duration is constantly changing and equal to the total seconds buffered from the SAAudioAvailabilityRange.
- Note: It's recommended to have a weak reference to a class that uses this function
- Parameter closure: The closure that will receive the updates of the changes in duration.
- Parameter url: The corresponding remote URL for the updated streaming progress.
@@ -168,7 +174,7 @@ extension SAPlayer {
/**
Subscribe to updates to changes in the progress of downloading audio. This does not correspond to progress in streaming downloads, look at StreamingBuffer for streaming progress.
Note: It's recommended to have a weak reference to a class that uses this fuction
- Note: It's recommended to have a weak reference to a class that uses this function
- Parameter closure: The closure that will receive the updates of the changes in duration.
- Parameter url: The corresponding remote URL for the updated download progress.
+54
View File
@@ -0,0 +1,54 @@
//
// Data.swift
// SwiftAudioPlayer
//
// Created by Tanha Kabir on 2019-11-29.
// Copyright © 2019 Tanha Kabir, Jon Mercer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Foundation
extension Data {
// Introduced in Swift 5, withUnsafeBytes using UnsafePointers is deprecated
// https://mjtsai.com/blog/2019/03/27/swift-5-released/
func accessBytes<R>(_ body: (UnsafePointer<UInt8>) throws -> R) rethrows -> R {
return try withUnsafeBytes { (rawBufferPointer: UnsafeRawBufferPointer) -> R in
let unsafeBufferPointer = rawBufferPointer.bindMemory(to: UInt8.self)
guard let unsafePointer = unsafeBufferPointer.baseAddress else {
Log.error("")
var int: UInt8 = 0
return try body(&int)
}
return try body(unsafePointer)
}
}
mutating func accessMutableBytes<R>(_ body: (UnsafeMutablePointer<UInt8>) throws -> R) rethrows -> R {
return try withUnsafeMutableBytes { (rawBufferPointer: UnsafeMutableRawBufferPointer) -> R in
let unsafeMutableBufferPointer = rawBufferPointer.bindMemory(to: UInt8.self)
guard let unsafeMutablePointer = unsafeMutableBufferPointer.baseAddress else {
Log.error("")
var int: UInt8 = 0
return try body(&int)
}
return try body(unsafeMutablePointer)
}
}
}
+7 -1
View File
@@ -33,7 +33,13 @@ extension Date {
*/
static func getUTC64() -> UInt {
//"On 32-bit platforms, UInt is the same size as UInt32, and on 64-bit platforms, UInt is the same size as UInt64."
return UInt(Date().timeIntervalSince1970.bitPattern)
if #available(iOS 11.0, *) {
return UInt(Date().timeIntervalSince1970.bitPattern)
} else {
let time = Date().timeIntervalSince1970.bitPattern & 0xFFFFFFFF;
return UInt(time)
}
}
/**
@@ -38,6 +38,12 @@ class DirectorThreadSafeClosures<P> {
private var closures: [UInt: TypeClosure] = [:]
private var cache: [Key: P] = [:]
var count: Int {
get {
return closures.count
}
}
func broadcast(key: Key, payload: P) {
queue.sync {
self.cache[key] = payload
+25 -14
View File
@@ -9,22 +9,23 @@
import Foundation
import os.log
// Possible levels of log messages to log
enum LogLevel: Int {
case DEBUG = 1
case INFO = 2
case WARN = 3
case ERROR = 4
case EXTERNAL_DEBUG = 5
case MONITOR = 6
case TEST = 7
}
// Specify which types of log messages to display. Default level is set to WARN, which means Log will print any log messages of type only WARN, ERROR, MONITOR, and TEST. To print DEBUG and INFO logs, set the level to a lower value.
var logLevel: LogLevel = LogLevel.MONITOR
class Log {
private init() {}
// Possible levels of log messages to log
public enum LogLevel: Int {
case DEBUG = 1
case INFO = 2
case WARN = 3
case ERROR = 4
case MONITOR = 5
case TEST = 6
}
// Specify which types of log messages to display. Default level is set to WARN, which means Log will print any log messages of type only WARN, ERROR, MONITOR, and TEST. To print DEBUG and INFO logs, set the level to a lower value.
public static var logLevel: LogLevel = LogLevel.ERROR
// Used for OSLog
private static let SUBSYSTEM: String = "com.SwiftAudioPlayer"
@@ -68,6 +69,11 @@ class Log {
let log = OSLog(subsystem: SUBSYSTEM, category: "ERROR 🛑🛑🛑🛑")
os_log("%@:%@:%d:: %@", log: log, fileName, functionName, lineNumber, "\(logMessage)")
}
if logLevel.rawValue <= LogLevel.EXTERNAL_DEBUG.rawValue {
let log = OSLog(subsystem: SUBSYSTEM, category: "WARNING")
os_log("%@:%@:%d:: %@", log: log, fileName, functionName, lineNumber, "\(logMessage)")
}
}
/**
@@ -86,7 +92,7 @@ class Log {
public static func monitor(_ logMessage: Any, classPath: String = #file, functionName: String = #function, lineNumber: Int = #line) {
let fileName = URLUtil.getNameFromStringPath(classPath)
if logLevel.rawValue <= LogLevel.ERROR.rawValue {
let log = OSLog(subsystem: SUBSYSTEM, category: "MONITOR 🔥🔥🔥🔥")
let log = OSLog(subsystem: SUBSYSTEM, category: "ERROR 🔥🔥🔥🔥")
os_log("%@:%@:%d:: %@", log: log, fileName, functionName, lineNumber, "\(logMessage)")
}
}
@@ -110,6 +116,11 @@ class Log {
let log = OSLog(subsystem: SUBSYSTEM, category: "WARN ⚠️⚠️⚠️⚠️")
os_log("%@:%@:%d:: %@", log: log, fileName, functionName, lineNumber, "\(logMessage)")
}
if logLevel.rawValue <= LogLevel.EXTERNAL_DEBUG.rawValue {
let log = OSLog(subsystem: SUBSYSTEM, category: "DEBUG")
os_log("%@:%@:%d:: %@", log: log, fileName, functionName, lineNumber, "\(logMessage)")
}
}
/**
+2 -2
View File
@@ -8,7 +8,7 @@
Pod::Spec.new do |s|
s.name = 'SwiftAudioPlayer'
s.version = '0.1.0'
s.version = '2.8.2'
s.summary = 'SwiftAudioPlayer is a Swift based audio player that can handle streaming from a remote location and audio manipulation.'
# This description is used to generate tags and improve search results.
@@ -31,7 +31,7 @@ SwiftAudioPlayer is a Swift based audio player that can handle streaming from a
s.ios.deployment_target = '10.0'
s.source_files = 'Source/**/*'
s.swift_version = '4.0'
s.swift_version = '5.0'
# s.resource_bundles = {
# 'SwiftAudioPlayer' => ['SwiftAudioPlayer/Assets/*.png']