Re: Use WKSessionFile as Data for AVAudioPlayer?
Re: Use WKSessionFile as Data for AVAudioPlayer?
- Subject: Re: Use WKSessionFile as Data for AVAudioPlayer?
- From: "Eric E. Dolecki" <email@hidden>
- Date: Wed, 08 Feb 2017 14:55:51 +0000
UPDATE. Fixed. I bagged on sendFile and instead I am using sendMessage.
The problem with that is the iOS app needs to be open, but it plays the
message for me.
*Watch:*
let data = NSData(contentsOf: saveURL as! URL)
sendAudioFile(file: data!)
*iOS:*
func session(_ session: WCSession, didReceiveMessageData messageData: Data,
replyHandler: @escaping (Data) -> Void) {
DispatchQueue.main.async {
self.versionLabel.text = "We got an audio file: \(messageData)"
//Show bytes
self.versionLabel.textColor = UIColor.blue
// Try to play from memory buffer (NOT saving the audio)
do {
self.player = try AVAudioPlayer(data: messageData)
guard self.player != nil else { return }
self.player?.prepareToPlay()
self.player?.play()
} catch let error as NSError {
print(error.localizedDescription)
}
}
}
On Wed, Feb 8, 2017 at 8:48 AM Eric E. Dolecki <email@hidden> wrote:
> When I try the following I get a "OSStatus error 2003334207"
>
> func session(_ session: WCSession, didReceive file: WCSessionFile)
> {
> DispatchQueue.main.async
> {
> self.versionLabel.text = "We got a real audio file."
> self.versionLabel.textColor = UIColor.blue
> do {
> try self.player = AVAudioPlayer(contentsOf: file.fileURL!)
> print("It seems that we have a player. \(self.player)")
> self.player?.prepareToPlay()
> self.player?.play()
> } catch let error as NSError {
> *print(error.localizedDescription) // This is firing...*
> }
>
> file.fileURL! reports in the console as:
>
> Documents/Inbox/com.apple.watchconnectivity/711E4215-77E2-422A-80C5-6ECA8621778E/Files/AE2DCCA0-EAA0-4E4C-8E46-D1AC77101760/audioFile.wav
> --
> file:///var/mobile/Containers/Data/Application/5E72A755-D60B-4633-8283-02A0891CA8B8/
>
> file.fileURL!.path reports as:
>
>
> /var/mobile/Containers/Data/Application/26CFE356-7FB1-483B-B918-BC0A2026DE46/Documents/Inbox/com.apple.watchconnectivity/711E4215-77E2-422A-80C5-6ECA8621778E/Files/90D004CE-88F7-4C66-9B9A-5580445A0572/audioFile.wav
>
> So I tried
> try self.player = AVAudioPlayer(contentsOf: file.fileURL!.path)* //
> expecting URL and not a string so I tried:*
>
> let myURL = URL(fileURLWithPath: file.fileURL!.path)
> try self.player = AVAudioPlayer(contentsOf: myURL)
>
> And got: *The operation couldn’t be completed. (OSStatus error
> 2003334207.)*
>
> Seems kind of odd. I would think this might be easy, but for me so far
> it's not.
>
> Any help appreciated!!! I just want to play the .wav file that was sent to
> the phone.
>
>
>
> On Tue, Feb 7, 2017 at 11:25 PM Jens Alfke <email@hidden> wrote:
>
>
> On Feb 7, 2017, at 6:49 PM, Eric E. Dolecki <email@hidden> wrote:
>
> Now, I want to instantiate an AVAudioPlayer and play that sent file. I know
> that it's received (didFinish file transfer fires in the Watch Extension).
> How do I do that? I don't know how to make that file Data.
>
>
> Can’t you just create the AVAudioPlayer from the file's URL? I’m not super
> handy with Swift, but it should be something like
> `AVAudioPlayer(contentsOfURL: fileURL)`.
>
> —Jens
>
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden