2013-09-04 14 views
9

Ho questa playlist dei brani nella mia app.Voglio riprodurre un brano da questa playlist su un altro dispositivo (iphone) utilizzando bluetooth.riproduzione di un brano su un altro dispositivo tramite bluetooth

Questo è quello che ho fatto per

#import "BrowseStationsViewController.h" 

@interface BrowseStationsViewController(){ 
GKSession *gkSession; 
} 

@end 

@implementation BrowseStationsViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
    // Custom initialization 
} 
return self; 
} 

#pragma mark - 
    - (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
// Do any additional setup after loading the view 

    [self setupSession]; 

NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; 

// Register for notifications when the application leaves the background state 
// on its way to becoming the active application. 
[defaultCenter addObserver:self 
        selector:@selector(setupSession) 
         name:UIApplicationWillEnterForegroundNotification 
        object:nil]; 

// Register for notifications when when the application enters the background. 
[defaultCenter addObserver:self 
        selector:@selector(teardownSession) 
         name:UIApplicationDidEnterBackgroundNotification 
        object:nil]; 


     } 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
    } 



    #pragma mark - GKSession setup and teardown 

    - (void)setupSession 
{ 
gkSession = [[GKSession alloc] initWithSessionID:nil displayName:nil sessionMode:GKSessionModePeer]; 
gkSession.delegate = self; 
gkSession.disconnectTimeout = kDisconnectTimeout; 
gkSession.available = YES; 

self.title = [NSString stringWithFormat:@"GKSession: %@", gkSession.displayName]; 
    } 

- (void)teardownSession 
{ 
[gkSession disconnectFromAllPeers]; 
gkSession.available = NO; 
gkSession.delegate = nil; 
} 


#pragma mark - GKSessionDelegate protocol conformance 

- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:  (GKPeerConnectionState)state 
{ 
switch (state) 
{ 
    case GKPeerStateAvailable: 
    { 
     NSLog(@"didChangeState: peer %@ available", [session displayNameForPeer:peerID]); 

     [NSThread sleepForTimeInterval:kSleepTimeInterval]; 

     [session connectToPeer:peerID withTimeout:kConnectionTimeout]; 
     break; 
    } 

    case GKPeerStateUnavailable: 
    { 
     NSLog(@"didChangeState: peer %@ unavailable", [session displayNameForPeer:peerID]); 
     break; 
    } 

    case GKPeerStateConnected: 
    { 
     NSLog(@"didChangeState: peer %@ connected", [session displayNameForPeer:peerID]); 
     break; 
    } 

    case GKPeerStateDisconnected: 
    { 
     NSLog(@"didChangeState: peer %@ disconnected", [session displayNameForPeer:peerID]); 
     break; 
    } 

    case GKPeerStateConnecting: 
    { 
     NSLog(@"didChangeState: peer %@ connecting", [session displayNameForPeer:peerID]); 
     break; 
    } 
} 

[self.tableView reloadData]; 
    } 


- (void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID 
    { 
NSLog(@"didReceiveConnectionRequestFromPeer: %@", [session displayNameForPeer:peerID]); 

[session acceptConnectionFromPeer:peerID error:nil]; 

[self.tableView reloadData]; 
    } 

    - (void)session:(GKSession *)session connectionWithPeerFailed:(NSString *)peerID withError:(NSError *)error 
    { 
NSLog(@"connectionWithPeerFailed: peer: %@, error: %@", [session displayNameForPeer:peerID], error); 

[self.tableView reloadData]; 
    } 

- (void)session:(GKSession *)session didFailWithError:(NSError *)error 
    { 
NSLog(@"didFailWithError: error: %@", error); 

[session disconnectFromAllPeers]; 

[self.tableView reloadData]; 
    } 

#pragma mark - UITableViewDataSource protocol conformance 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 
// We have 5 sections in our grouped table view, 
// one for each GKPeerConnectionState 
return 3; 
    } 

    - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section 
    { 
NSInteger rows; 

NSInteger peerConnectionState = section; 

switch (peerConnectionState) 
{ 
    case GKPeerStateAvailable: 
    { 
     NSArray *availablePeers = [gkSession peersWithConnectionState:GKPeerStateAvailable]; 
     rows = availablePeers.count; 
     break; 
    } 

    case GKPeerStateConnected: 
    { 
     NSArray *connectedPeers = [gkSession peersWithConnectionState:GKPeerStateConnected]; 
     rows = connectedPeers.count; 
     break; 
    } 

    case GKPeerStateUnavailable: 
    { 
     NSArray *unavailablePeers = [gkSession peersWithConnectionState:GKPeerStateUnavailable]; 
     rows = unavailablePeers.count; 
     break; 
    } 
} 

// Always show at least 1 row for each GKPeerConnectionState. 
if (rows < 1) 
{ 
    rows = 1; 
} 

return rows; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
NSString *headerTitle = nil; 

NSInteger peerConnectionState = section; 

switch (peerConnectionState) 
{ 
    case GKPeerStateAvailable: 
    { 
     headerTitle = @"Available Peers"; 
     break; 
    } 


    case GKPeerStateConnected: 
    { 
     headerTitle = @"Connected Peers"; 
     break; 
    } 


    case GKPeerStateUnavailable: 
    { 
     headerTitle = @"Unavailable Peers"; 
     break; 
    } 
} 

return headerTitle; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSString * cellId = @"Cell"; 
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId]; 
if(!cell){ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId]; 
} 


NSInteger peerConnectionState = indexPath.section; 

NSArray *peers = nil; 

switch (peerConnectionState) 
{ 
    case GKPeerStateAvailable: 
    { 
     peers = [gkSession peersWithConnectionState:GKPeerStateAvailable]; 
     break; 
    } 

    case GKPeerStateConnected: 
    { 
     peers = [gkSession peersWithConnectionState:GKPeerStateConnected]; 
     break; 
    } 


    case GKPeerStateUnavailable: 
    { 
     peers = [gkSession peersWithConnectionState:GKPeerStateUnavailable]; 
     break; 
    } 
} 

NSInteger peerIndex = indexPath.row; 

if ((peers.count > 0) && (peerIndex < peers.count)) 
{ 
    NSString *peerID = [peers objectAtIndex:peerIndex]; 

    if (peerID) 
    { 
     cell.textLabel.text = [gkSession displayNameForPeer:peerID]; 
    } 
} 

return cell; 
} 

    @end 

please see the screen shot

ora non ho idea di come proceed.Could qualcuno please help me out ?? Selezionando una canzone può che essere riprodotto su un altro dispositivo ??

+0

Apple rifiuterà molto probabilmente la tua app se usi GameKit ma la tua app non è un gioco reale. – Wain

+0

Alcuni dettagli per favore? Perché sto usando Gamekit.Thanks – Machete

+0

GameKit è per i giochi. Apple non consente di utilizzarlo se la tua app non è un gioco. Anche se l'app ha caratteristiche simili a quelle del gioco, ma in realtà non è un gioco, di solito la respingono. – Wain

risposta

1

GameKit è pensato per giochi inter-dispositivo. Per questo, probabilmente vorrai dare un'occhiata a CBPeripheralManager o CBCentralManager a seconda del dispositivo con cui stai interagendo. È di livello inferiore, quindi dovrai fare più lavoro per impostare la connessione, ma ci sono un sacco di tutorial e codice di esempio per aiutarti.

+0

Cosa succede se una piattaforma non supporta Bluetooth Low Energy ?? Non posso usarlo allora giusto ?? – Machete

+0

Sì, funziona solo con BLE. – Mark

+0

Quindi devo andare con Gamekit.Framework o qualche altro framework ma non ho idea di come procedere :( – Machete

0

Ok consente di arrivare al punto in cui si è bloccati .. È possibile utilizzare la stessa logica con alcune altre librerie, ma questo è come dovrebbe andare. Dovrai inviare i dati del brano in blocchi e sincronizzarli all'altro dispositivo mentre il chunk precedente viene ricevuto dall'altro capo. Poiché siamo chiari che il bluetooth non ha una larghezza di banda grande, dovremo adattare in modo specifico la velocità di trasmissione all'altro dispositivo. una volta che un chunk viene ricevuto sul dispositivo inviato all'istanza dell'app in esecuzione su quel dispositivo dovrebbe riprodurlo .. e in parallelo sonda per i pezzi più recenti provenienti dal dispositivo di invio .. Sul lato ricevente si può semplicemente utilizzare il metodo FIFO per la gestione i pezzi in arrivo dei dati della tua canzone.

+0

GameKit è per i giochi.Apple non consente di usarlo se la tua app non è un gioco. l'app ha caratteristiche simili a quelle del gioco, ma in realtà non è un gioco di solito la respingono. - Wain – Machete

+0

In qualche modo ho funzionato usando la stessa logica in questo http://www.raywenderlich.com/12865/how-to-make- a-simple-playing-card-game-with-multiplayer-and-bluetooth-part-2 ... Ma ora sono di nuovo bloccato :( – Machete

+0

L'approccio è corretto, ho controllato il link che hai menzionato .. ma solo prova ad usare un altro kit disponibile diverso da un gamekit poiché Apple sta facendo un filtro .. –