2013-09-04 17 views
9

Mam tę listę utworów z mojej aplikacji. Chcę odtwarzać utwór z tej listy odtwarzania na innym urządzeniu (iPhone) za pomocą Bluetooth.Odtwarzanie utworu na innym urządzeniu przy użyciu bluetooth

To co mam zrobić tak dla

#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

teraz nie mam pojęcia jak proceed.Could ktoś mógłby mi pomóc ?? Wybierając utwór może być odtwarzany na inne urządzenie?

+0

Apple najprawdopodobniej odrzuci twoją aplikację, jeśli używasz GameKit, ale twoja aplikacja nie jest rzeczywistą grą. – Wain

+0

Niektóre szczegóły proszę? Ponieważ używam Gamekit. Dzięki – Machete

+0

GameKit jest dla gier. Apple nie zezwala na używanie go, jeśli twoja aplikacja nie jest grą. Nawet jeśli aplikacja ma funkcje podobne do gier, ale tak naprawdę nie jest grą, zazwyczaj ją odrzuca. – Wain

Odpowiedz

1

GameKit jest przeznaczony do gier na urządzenia. W tym celu prawdopodobnie będziesz musiał spojrzeć na CBPeripheralManager lub CBCentralManager w zależności od urządzenia, z którym współpracujesz. Jest niższy poziom, więc będziesz musiał wykonać więcej pracy, aby skonfigurować połączenie, ale jest mnóstwo tutoriali i przykładowy kod, aby ci pomóc.

+0

Co się stanie, jeśli platforma nie obsługuje technologii Bluetooth Low Energy? Nie mogę jej użyć, to prawda? – Machete

+0

Tak, działa to tylko z BLE. – Mark

+0

Więc muszę iść z Gamekit.Framework lub jakimś innym frameworkiem, ale nie mam pojęcia, jak postępować :( – Machete

0

OK pozwala dojść do punktu, w którym utknąłeś .. Możesz użyć tej samej logiki z innymi bibliotekami, ale tak właśnie powinna być. Będziesz musiał wysłać dane utworów w porcjach i zsynchronizować się z drugim urządzeniem, podczas gdy poprzedni fragment zostanie odebrany na drugim końcu. Ponieważ mamy jasność, że bluetooth nie ma szerokiej szerokości pasma, musisz dostosować szybkość transmisji do innego urządzenia. po otrzymaniu porcji na urządzenie wysłane do instancji aplikacji uruchomionej na tym urządzeniu należy ją odtworzyć .. i równolegle sondować nowsze fragmenty przychodzące z urządzenia wysyłającego. Na końcu odbierającym można po prostu użyć metody FIFO do obsługi nadchodzące kawałki danych o utworze.

+0

GameKit jest przeznaczony do gier. Apple nie zezwala na używanie go, jeśli twoja aplikacja nie jest grą. aplikacja ma funkcje podobne do gier, ale tak naprawdę nie jest grą, którą zazwyczaj odrzuca. - Wain – Machete

+0

Jakoś mi się to udało, używając tej samej logiki w tym http://www.raywenderlich.com/12865/how-to-make- a-prosta-gra-gra-z-gry-z-multiplayer-i-bluetooth-część-2 ... Ale teraz utknąłem ponownie :( – Machete

+0

Podejście jest poprawne, sprawdziłem link, o którym wspomniałeś .. ale po prostu spróbuj użyć innego dostępnego zestawu innego niż gamekit, ponieważ Apple robi filtr. –