From 8c4ae2e7e7a8078ee7a38d17ca097fafe681b4d5 Mon Sep 17 00:00:00 2001 From: Okechi Date: Fri, 4 Mar 2016 00:59:56 -0500 Subject: [PATCH] Connection between peers working framework is kinda buggy, but that may just be development woes. Working on streaming now with TDAudioStreamer library --- GetHip/AppDelegate.swift | 22 +++--- GetHip/CurrentlyPlayingViewController.swift | 77 +++++++++++++++++---- GetHip/JoiningPartyViewController.swift | 1 + GetHip/LoadingPartyViewController.swift | 50 ++++++++++++- GetHip/Main.storyboard | 4 +- GetHip/PartyServiceManager.swift | 28 ++++++++ 6 files changed, 156 insertions(+), 26 deletions(-) diff --git a/GetHip/AppDelegate.swift b/GetHip/AppDelegate.swift index 15ee187..8e67637 100644 --- a/GetHip/AppDelegate.swift +++ b/GetHip/AppDelegate.swift @@ -121,17 +121,19 @@ class AppDelegate: UIResponder, UIApplicationDelegate , CLLocationManagerDelegat var query = PFQuery(className: "_User") var currentUser = PFUser.currentUser() - - query.whereKey("username", equalTo: (currentUser?.username as String!)) - - query.getFirstObjectInBackgroundWithBlock({ - (object: PFObject?, error: NSError?) -> Void in + if(currentUser != nil){ + query.whereKey("username", equalTo: (currentUser?.username as String!)) - if(error == nil){ - object?.setObject(geo, forKey: "location") - object?.saveInBackground() - } - }) + query.getFirstObjectInBackgroundWithBlock({ + (object: PFObject?, error: NSError?) -> Void in + + if(error == nil){ + object?.setObject(geo, forKey: "location") + object?.saveInBackground() + } + }) + } + } diff --git a/GetHip/CurrentlyPlayingViewController.swift b/GetHip/CurrentlyPlayingViewController.swift index 472a729..19882b8 100644 --- a/GetHip/CurrentlyPlayingViewController.swift +++ b/GetHip/CurrentlyPlayingViewController.swift @@ -9,15 +9,16 @@ import UIKit import MediaPlayer import AVFoundation +import MultipeerConnectivity -class CurrentlyPlayingViewController: UIViewController{ +class CurrentlyPlayingViewController: UIViewController, PartyServiceManagerDelegate{ //persistant data var party: PartyServiceManager! var usr: [UserParseData] = [] var frnds: [FriendData] = [] var requestData: [FriendData] = [] var audioPlayer: AVPlayer! - var playing = true + var playing = false var timer = NSTimer() //controller data @@ -57,24 +58,34 @@ class CurrentlyPlayingViewController: UIViewController{ override func viewDidLoad() { super.viewDidLoad() - self.audioPlayer = AVPlayer(URL: self.party.currentSong.valueForProperty(MPMediaItemPropertyAssetURL) as! NSURL) + // Do any additional setup after loading the view. if(self.party.role == PeerType.Host_Creator){ + self.audioPlayer = AVPlayer(URL: self.party.currentSong.valueForProperty(MPMediaItemPropertyAssetURL) as! NSURL) + self.songImg.image = self.party.currentSong.valueForProperty(MPMediaItemPropertyArtwork).imageWithSize(songImg.frame.size) self.titleLabel.text = (self.party.currentSong.valueForProperty(MPMediaItemPropertyTitle) as? String!)! self.artistAndAlbumLabel.text = (self.party.currentSong.valueForProperty(MPMediaItemPropertyArtist) as? String!)! + " - " + (self.party.currentSong.valueForProperty(MPMediaItemPropertyAlbumTitle) as? String!)! self.audioPlayer.volume = self.volCtrl.value self.maxLabel.text = String(stringInterpolationSegment: self.audioPlayer.currentItem.duration.value) - self.audioPlayer.play() - self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateLabels"), userInfo: nil, repeats: true) + self.party.delegate = self + //self.audioPlayer.play() + //self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateLabels"), userInfo: nil, repeats: true) }else if (self.party.role == PeerType.Guest_Invited){ self.songImg.image = self.party.currentSongIMG self.titleLabel.text = (self.party.currentSongTitle) + println(self.party.currentSongTitle) + println(self.titleLabel.text) self.artistAndAlbumLabel.text = (self.party.currentSongArtistAlbum) + self.party.delegate = self + + + + } @@ -82,14 +93,17 @@ class CurrentlyPlayingViewController: UIViewController{ } func updateLabels(){ - var timeLeft = self.audioPlayer.currentItem.duration.value - self.audioPlayer.currentTime().value - var interval = timeLeft - var seconds = interval%60 - println(seconds) - var minutes = (interval/60)%60 - println(minutes) - //self.maxLabel.text - //self.minLabel.text + if (self.playing == true){ + var timeLeft = self.audioPlayer.currentItem.duration.value - self.audioPlayer.currentTime().value + var interval = timeLeft + var seconds = interval%60 + println(seconds) + var minutes = (interval/60)%60 + println(minutes) + //self.maxLabel.text + //self.minLabel.text + } + } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() @@ -127,3 +141,40 @@ class CurrentlyPlayingViewController: UIViewController{ } + +extension CurrentlyPlayingViewController: PartyServiceManagerDelegate { + + func foundPeer() { + + } + + func lostPeer() { + + } + + func invitationWasRecieved(peerID: MCPeerID, invitationHandler: ((Bool, MCSession!) -> Void)!) { + + } + + func connectedWithPeer(peerID: MCPeerID) { + + + } + + func didRecieveInstruction(dictionary: Dictionary){ + let (instruction, fromPeer) = self.party.decodeInstruction(dictionary) + + if self.party.disconnectedPeersDictionary[fromPeer.displayName] != nil { + + var dictionary: [String: String] = ["sender": self.party.myPeerID.displayName, "instruction": "disconnect"] + self.party.sendInstruction(dictionary, toPeer: fromPeer) + }else{ + + } + + + + + } + +} diff --git a/GetHip/JoiningPartyViewController.swift b/GetHip/JoiningPartyViewController.swift index 26dc1bb..59f698b 100644 --- a/GetHip/JoiningPartyViewController.swift +++ b/GetHip/JoiningPartyViewController.swift @@ -87,6 +87,7 @@ extension JoiningPartyViewController: PartyServiceManagerDelegate { } if (instruction == "start_party"){ + println("mark 4") self.performSegueWithIdentifier("EnteringPartySegue", sender: self) } diff --git a/GetHip/LoadingPartyViewController.swift b/GetHip/LoadingPartyViewController.swift index 76ab9aa..69a27cc 100644 --- a/GetHip/LoadingPartyViewController.swift +++ b/GetHip/LoadingPartyViewController.swift @@ -9,6 +9,7 @@ import UIKit import MediaPlayer import MultipeerConnectivity +import AVFoundation class LoadingPartyViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, PartyServiceManagerDelegate{ //persistant data @@ -127,7 +128,7 @@ class LoadingPartyViewController: UIViewController, UICollectionViewDataSource, } } }else{ - var dictionary: [String: String] = ["sender": self.party.myPeerID.displayName, "instruction": "start_Party"] + var dictionary: [String: String] = ["sender": self.party.myPeerID.displayName, "instruction": "start_party"] self.party.sendInstruction(dictionary, toPeer: joinedPeer! ) } @@ -200,6 +201,53 @@ extension LoadingPartyViewController: PartyServiceManagerDelegate { var dictionary: [String: AnyObject] = ["sender": self.party.myPeerID.displayName, "instruction": "set_up_song", "songTitle": (self.party.currentSong.valueForProperty(MPMediaItemPropertyTitle) as? String!)!, "songArtistAndAlbum": (self.party.currentSong.valueForProperty(MPMediaItemPropertyArtist) as? String!)! + " - " + (self.party.currentSong.valueForProperty(MPMediaItemPropertyAlbumTitle) as? String!)!, "songImage": UIImagePNGRepresentation(self.party.currentSong.valueForProperty(MPMediaItemPropertyArtwork).imageWithSize(songImg.frame.size))] self.party.sendInstruction(dictionary, toPeer: fromPeer) + + //open stream with peer + let stream = self.party.outputStreamForPeer(fromPeer) + self.party.outputStreamer = TDAudioOutputStreamer(outputStream: stream) + + + /* + let asset: AVURLAsset? = AVURLAsset(URL: (self.party.currentSong.valueForProperty(MPMediaItemPropertyAssetURL) as! NSURL), options: nil) + + var error: NSError? + + let assetReader: AVAssetReader = AVAssetReader(asset: asset, error: &error) + + let assetOutput: AVAssetReaderTrackOutput = AVAssetReaderTrackOutput(track: asset!.tracks[0] as! AVAssetTrack, outputSettings: nil) + + if error != nil { + + }else{ + //for peer in self.party.session.connectedPeers { + assetReader.addOutput(assetOutput) + assetReader.startReading() + + let sampleBuffer:CMSampleBuffer! = assetOutput.copyNextSampleBuffer() + + var audioBufferList = AudioBufferList(mNumberBuffers: 1, mBuffers: AudioBuffer(mNumberChannels: 0, mDataByteSize: 0, mData: nil)) + + var blockBuffer: Unmanaged? = nil + + CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, nil, &audioBufferList, Int(sizeof(audioBufferList.dynamicType)), nil, nil, UInt32(kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment), &blockBuffer) + + let stream = self.party.outputStreamForPeer(fromPeer) //self.party.session.startStreamWithName("music", toPeer: peer as! MCPeerID, error: &error) + + if error != nil { + print("Error stream: \(error?.localizedDescription)") + } + + for (var i: UInt32 = 0; i < audioBufferList.mNumberBuffers; i++){ + var audioBuffer = AudioBuffer(mNumberChannels: audioBufferList.mBuffers.mNumberChannels, mDataByteSize: audioBufferList.mBuffers.mDataByteSize, mData: audioBufferList.mBuffers.mData) + stream.write(UnsafeMutablePointer(audioBuffer.mData), maxLength: Int(audioBuffer.mDataByteSize)) + } + //} + + //CFRelease(blockBuffer) + //CFRelease(sampleBuffer) + + }*/ + } diff --git a/GetHip/Main.storyboard b/GetHip/Main.storyboard index 8d19132..12f4467 100644 --- a/GetHip/Main.storyboard +++ b/GetHip/Main.storyboard @@ -1306,7 +1306,7 @@