worked on the connection, still buggy but I believe that is just the framework. Working on streaming content now

This commit is contained in:
Okechi 2016-03-03 12:12:50 -05:00
parent 25375c899f
commit fb39de92cc
5 changed files with 153 additions and 33 deletions

View File

@ -60,6 +60,9 @@ class CurrentlyPlayingViewController: UIViewController{
self.audioPlayer = AVPlayer(URL: self.party.currentSong.valueForProperty(MPMediaItemPropertyAssetURL) as! NSURL) self.audioPlayer = AVPlayer(URL: self.party.currentSong.valueForProperty(MPMediaItemPropertyAssetURL) as! NSURL)
// Do any additional setup after loading the view. // Do any additional setup after loading the view.
if(self.party.role == PeerType.Host_Creator){
self.songImg.image = self.party.currentSong.valueForProperty(MPMediaItemPropertyArtwork).imageWithSize(songImg.frame.size) self.songImg.image = self.party.currentSong.valueForProperty(MPMediaItemPropertyArtwork).imageWithSize(songImg.frame.size)
self.titleLabel.text = (self.party.currentSong.valueForProperty(MPMediaItemPropertyTitle) as? String!)! 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.artistAndAlbumLabel.text = (self.party.currentSong.valueForProperty(MPMediaItemPropertyArtist) as? String!)! + " - " + (self.party.currentSong.valueForProperty(MPMediaItemPropertyAlbumTitle) as? String!)!
@ -67,6 +70,14 @@ class CurrentlyPlayingViewController: UIViewController{
self.maxLabel.text = String(stringInterpolationSegment: self.audioPlayer.currentItem.duration.value) self.maxLabel.text = String(stringInterpolationSegment: self.audioPlayer.currentItem.duration.value)
self.audioPlayer.play() self.audioPlayer.play()
self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateLabels"), userInfo: nil, repeats: true) 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)
self.artistAndAlbumLabel.text = (self.party.currentSongArtistAlbum)
}
} }

View File

@ -16,6 +16,7 @@ class InvitedToPartyViewController: UIViewController , PartyServiceManagerDelega
var partyData: PartyServiceManager! var partyData: PartyServiceManager!
var inviteHandle: ((Bool, MCSession!) -> Void)! var inviteHandle: ((Bool, MCSession!) -> Void)!
private var fromPeer: MCPeerID! private var fromPeer: MCPeerID!
var shouldEndInvite = false
@IBOutlet var img: UIImageView! @IBOutlet var img: UIImageView!
@IBOutlet var inviteTxt: UITextView! @IBOutlet var inviteTxt: UITextView!
@ -63,6 +64,12 @@ class InvitedToPartyViewController: UIViewController , PartyServiceManagerDelega
} }
override func viewWillAppear(animated: Bool) {
if(self.shouldEndInvite == true){
self.dismissViewControllerAnimated(true, completion: nil)
}
}
override func didReceiveMemoryWarning() { override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning() super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated. // Dispose of any resources that can be recreated.

View File

@ -34,15 +34,22 @@ class JoiningPartyViewController: UIViewController ,PartyServiceManagerDelegate{
self.user = user self.user = user
} }
/*
// MARK: - Navigation // MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController. // Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller. // Pass the selected object to the new view controller.
if segue.identifier == "EnteringPartySegue"{
let vc: CurrentlyPlayingViewController = (segue.destinationViewController as? CurrentlyPlayingViewController)!
vc.setData(self.party, user: self.user, friends: self.friends as! [FriendData], request: self.request as! [FriendData])
} }
*/ }
} }
@ -67,6 +74,22 @@ extension JoiningPartyViewController: PartyServiceManagerDelegate {
} }
func didRecieveInstruction(dictionary: Dictionary<String, AnyObject>){ func didRecieveInstruction(dictionary: Dictionary<String, AnyObject>){
let (instruction, fromPeer) = self.party.decodeInstruction(dictionary)
if (instruction == "disconnect") {
self.party.session.disconnect()
if let vc = self.parentViewController as? InvitedToPartyViewController{
vc.shouldEndInvite = true
}
self.dismissViewControllerAnimated(true, completion: nil)
}
if (instruction == "start_party"){
self.performSegueWithIdentifier("EnteringPartySegue", sender: self)
}
} }

View File

@ -51,6 +51,7 @@ class LoadingPartyViewController: UIViewController, UICollectionViewDataSource,
//NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateThumbnail:", name: "peerConnected", object: nil) //NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateThumbnail:", name: "peerConnected", object: nil)
self.timerLabel.text = String(counter) self.timerLabel.text = String(counter)
self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateCounter"), userInfo: nil, repeats: true) self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateCounter"), userInfo: nil, repeats: true)
self.party.delegate = self
} }
override func didReceiveMemoryWarning() { override func didReceiveMemoryWarning() {
@ -111,6 +112,35 @@ class LoadingPartyViewController: UIViewController, UICollectionViewDataSource,
// Get the new view controller using segue.destinationViewController. // Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller. // Pass the selected object to the new view controller.
if(segue.identifier == "CurrentlyPlayingSegue"){ if(segue.identifier == "CurrentlyPlayingSegue"){
//ends invitations with outstanding peers
for invited in self.party.invitedFriends {
var joinedPeer: MCPeerID? = self.party.connectedPeersDictionary[invited.displayName] as? MCPeerID
if joinedPeer == nil {
for aPeer in self.party.foundPeers {
if aPeer.displayName == invited.displayName {
self.party.session.cancelConnectPeer(aPeer)
self.party.disconnectedPeersDictionary.setValue(aPeer, forKey: aPeer.displayName)
break
}
}
}else{
var dictionary: [String: String] = ["sender": self.party.myPeerID.displayName, "instruction": "start_Party"]
self.party.sendInstruction(dictionary, toPeer: joinedPeer! )
}
}
/*if !(self.session.sendData(dataToSend, toPeers: peersArray as [AnyObject], withMode: MCSessionSendDataMode.Reliable, error: &error)){
println(error?.localizedDescription)
return false
}*/
let vc: CurrentlyPlayingViewController = (segue.destinationViewController as? CurrentlyPlayingViewController)! let vc: CurrentlyPlayingViewController = (segue.destinationViewController as? CurrentlyPlayingViewController)!
vc.setData(self.party, user: self.usr, friends: self.frnds, request: self.requestData) vc.setData(self.party, user: self.usr, friends: self.frnds, request: self.requestData)
} }
@ -134,35 +164,48 @@ extension LoadingPartyViewController: PartyServiceManagerDelegate {
} }
func connectedWithPeer(peerID: MCPeerID) { func connectedWithPeer(peerID: MCPeerID) {
println(self.party.myPeerID.displayName + " connected to " + peerID.displayName)
var i = 0
for(index, aFriend) in enumerate(self.party.invitedFriends) {
if aFriend.displayName == peerID.displayName {
i = index
break
}
}
let cell: InvitedCollectionViewCell = self.invitedFriends.dequeueReusableCellWithReuseIdentifier("InvitedCollectionCell", forIndexPath: NSIndexPath(index: i)) as! InvitedCollectionViewCell
cell.alpha = 1.0
} }
func didRecieveInstruction(dictionary: Dictionary<String, AnyObject>){ func didRecieveInstruction(dictionary: Dictionary<String, AnyObject>){
//extract data from dictionary let (instruction, fromPeer) = self.party.decodeInstruction(dictionary)
println("mark 3")
let data = dictionary["data"] as? NSData
let fromPeer = dictionary["fromPeer"] as! MCPeerID
//convert data to dictionary object with instruction if self.party.disconnectedPeersDictionary[fromPeer.displayName] != nil {
let dataDictionary = NSKeyedUnarchiver.unarchiveObjectWithData(data!) as! Dictionary<String, String>
//check if this is an instruction being sent var dictionary: [String: String] = ["sender": self.party.myPeerID.displayName, "instruction": "disconnect"]
if let instruction = dataDictionary["instruction"] { self.party.sendInstruction(dictionary, toPeer: fromPeer)
}else{
if (instruction == "joined_party") {
println(instruction) println(instruction)
var i = 0
for(index, aFriend) in enumerate(self.party.invitedFriends) {
if aFriend.displayName == fromPeer.displayName {
i = index
break
} }
} }
self.party.connectedPeersDictionary.setValue(fromPeer, forKey: fromPeer.displayName)
//let cell: InvitedCollectionViewCell = self.invitedFriends.dequeueReusableCellWithReuseIdentifier("InvitedCollectionCell", forIndexPath: NSIndexPath(index: i)) as! InvitedCollectionViewCell
//cell.alpha = 1.0
println(instruction)
//prepares party for the guest peers
//sends music info incuding, title, artist, album, and image
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)
}
}
}
} }

View File

@ -57,9 +57,14 @@ class PartyServiceManager: NSObject {
var connectedPeersDictionary = NSMutableDictionary() var connectedPeersDictionary = NSMutableDictionary()
var disconnectedPeersDictionary = NSMutableDictionary() var disconnectedPeersDictionary = NSMutableDictionary()
//party variables //party-creator variables
var currentSong: MPMediaItem! = nil var currentSong: MPMediaItem! = nil
//party-guest variables
var currentSongTitle: String!
var currentSongArtistAlbum: String!
var currentSongIMG: UIImage!
//Peer Initializer //Peer Initializer
@ -94,7 +99,7 @@ class PartyServiceManager: NSObject {
} }
//Party Instruction Sender //Party Instruction Sender
func sendInstruction(dictionary: Dictionary<String, String>, toPeer: MCPeerID) -> Bool { func sendInstruction(dictionary: Dictionary<String, AnyObject>, toPeer: MCPeerID) -> Bool {
let dataToSend = NSKeyedArchiver.archivedDataWithRootObject(dictionary) let dataToSend = NSKeyedArchiver.archivedDataWithRootObject(dictionary)
let peersArray = NSArray(object: toPeer) let peersArray = NSArray(object: toPeer)
@ -108,6 +113,32 @@ class PartyServiceManager: NSObject {
return true return true
} }
func decodeInstruction(dictionary: Dictionary<String, AnyObject>) -> (String, MCPeerID) {
//extract data from dictionary
println("mark 3")
let data = dictionary["data"] as? NSData
let fromPeer = dictionary["fromPeer"] as! MCPeerID
//convert data to dictionary object with instruction
let dataDictionary = NSKeyedUnarchiver.unarchiveObjectWithData(data!) as! Dictionary<String, AnyObject>
//check if this is an instruction being sent
var instruction: String! = dataDictionary["instruction"] as! String
if instruction != nil {
println(instruction)
if(instruction == "set_up_song"){
self.currentSongTitle = dataDictionary["songTitle"] as! String
self.currentSongArtistAlbum = dataDictionary["songArtistAndAlbum"] as! String
self.currentSongIMG = UIImage(data: (dataDictionary["songImage"] as! NSData))
}
return (instruction!, fromPeer)
}else{
return ("not_an_instruction", fromPeer)
}
}
//Listening methods //Listening methods
func setAdvertiser(){ func setAdvertiser(){
self.serviceAdvertiser = MCNearbyServiceAdvertiser(peer: self.myPeerID, discoveryInfo: nil, serviceType: self.PartyServiceType) self.serviceAdvertiser = MCNearbyServiceAdvertiser(peer: self.myPeerID, discoveryInfo: nil, serviceType: self.PartyServiceType)
@ -273,6 +304,10 @@ extension PartyServiceManager: MCSessionDelegate{
self.delegate?.connectedWithPeer(peerID) self.delegate?.connectedWithPeer(peerID)
} }
if(state == MCSessionState.NotConnected){
NSLog("%@", "peer \(peerID) didChangeState: \(state.stringValue())")
}
} }
func session(session: MCSession!, didReceiveData data: NSData!, fromPeer peerID: MCPeerID!) { func session(session: MCSession!, didReceiveData data: NSData!, fromPeer peerID: MCPeerID!) {
@ -281,6 +316,7 @@ extension PartyServiceManager: MCSessionDelegate{
let dictionary: [String: AnyObject] = ["data": data, "fromPeer": peerID] let dictionary: [String: AnyObject] = ["data": data, "fromPeer": peerID]
self.delegate?.didRecieveInstruction(dictionary) self.delegate?.didRecieveInstruction(dictionary)
} }
func session(session: MCSession!, didReceiveStream stream: NSInputStream!, withName streamName: String!, fromPeer peerID: MCPeerID!) { func session(session: MCSession!, didReceiveStream stream: NSInputStream!, withName streamName: String!, fromPeer peerID: MCPeerID!) {