Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"images" : [
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "20x20"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "20x20"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "29x29"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "29x29"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "40x40"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "40x40"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "60x60"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "60x60"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "20x20"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "20x20"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "29x29"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "29x29"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "40x40"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "40x40"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "76x76"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "76x76"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "83.5x83.5"
},
{
"idiom" : "ios-marketing",
"scale" : "1x",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// ContentView.swift
// SwiftAudioPlayer-SwiftUI
//
// Created by Ran on 4/21/21.
//

import SwiftUI

struct ContentView: View {
@StateObject var saUpdates = SAPlayer.SAPlayerCombine.shared
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're going to use saUpdates in multiple views, consider moving this to your App file and inject it into each view. For each view save it as @ObservedObject instead of @StateObject


var body: some View {
VStack {
Text(saUpdates.update.elapsedTime == nil ? "Hit the play button" : "playing audio at: \(saUpdates.update.elapsedTime!)")
.padding()
Button(action: {
guard let status = saUpdates.update.playingStatus else {
let url = URL(string: "https://chtbl.com/track/18338/traffic.libsyn.com/secure/acquired/Acquired_-_Rec_Room_-_Final.mp3")!
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider moving this stuff out of the view layer.

SAPlayer.shared.startRemoteAudio(withRemoteUrl: url)
SAPlayer.shared.play()
return
}

if status == .playing {
SAPlayer.shared.pause()
} else if status == .paused {
SAPlayer.shared.play()
} else {
print("you're probably still buffering, chill...")
}
}, label: {
Image(systemName: saUpdates.update.playingStatus == nil ? "play.circle.fill" : saUpdates.update.playingStatus! == .playing ? "pause.circle.fill" : "play.circle.fill")
.resizable()
.frame(width: 100, height: 100, alignment: .center)
})

}

}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<true/>
</dict>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
<key>UILaunchScreen</key>
<dict/>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// SwiftAudioPlayer_SwiftUIApp.swift
// SwiftAudioPlayer-SwiftUI
//
// Created by Ran on 4/21/21.
//

import SwiftUI

@main
struct SwiftAudioPlayer_SwiftUIApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
92 changes: 92 additions & 0 deletions Source/SAPlayerCombineUpdates.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//
// SAPlayerCombineUpdates.swift
// SwiftAudioPlayer-SwiftUI
//
// Created by Jon Mercer on 4/21/21.
//
// 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
import Combine

extension SAPlayer {
class SAPlayerCombine: ObservableObject {
static let shared = SAPlayerCombine()


struct CombineUpdate {
var url: URL?
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this won't be a nil because we should be using a PassthroughSubject. However, most new users are used to @Published so I went with that.

If you're going to heavily use this player with SwiftUI consider making a PR using PassthroughSubject, or at least let me know and I'll implement that.

var elapsedTime: Double?
var duration: Double?
var playingStatus: SAPlayingStatus?
var streamingBuffer: SAAudioAvailabilityRange?
var downloadProgress: Double?
//TODO: add queue here if people use this
}

@Published var update = CombineUpdate()


private var elapsedTimeId:UInt?
private var durationId:UInt?
private var playingStatusId:UInt?
private var streamingBufferId:UInt?
private var audioDownloadingId:UInt?
private var audioQueueId:UInt? //TODO: add this later becuase it's more complicated
deinit {
if let id = elapsedTimeId { SAPlayer.Updates.ElapsedTime.unsubscribe(id) }
if let id = durationId { SAPlayer.Updates.Duration.unsubscribe(id) }
if let id = playingStatusId { SAPlayer.Updates.PlayingStatus.unsubscribe(id) }
if let id = streamingBufferId { SAPlayer.Updates.StreamingBuffer.unsubscribe(id) }
if let id = audioDownloadingId { SAPlayer.Updates.AudioDownloading.unsubscribe(id) }
}

init() {
elapsedTimeId = SAPlayer.Updates.ElapsedTime.subscribe { [weak self] (url:URL, timePosition:Double) in
guard let self = self else { return }
self.update.url = url
self.update.elapsedTime = timePosition
}

durationId = SAPlayer.Updates.Duration.subscribe { [weak self] (url:URL, duration:Double) in
guard let self = self else { return }
self.update.url = url
self.update.duration = duration
}

playingStatusId = SAPlayer.Updates.PlayingStatus.subscribe { [weak self] (url:URL, status:SAPlayingStatus) in
guard let self = self else { return }
self.update.url = url
self.update.playingStatus = status
}

streamingBufferId = SAPlayer.Updates.StreamingBuffer.subscribe { [weak self] (url:URL, buffer:SAAudioAvailabilityRange) in
guard let self = self else { return }
self.update.url = url
self.update.streamingBuffer = buffer
}

audioDownloadingId = SAPlayer.Updates.AudioDownloading.subscribe { [weak self] (url:URL, progress:Double) in
guard let self = self else { return }
self.update.url = url
self.update.downloadProgress = progress
}
}
}
}