简体   繁体   English

使用蓝牙在另一台设备上播放歌曲

[英]playing a song on another device using bluetooth

i have this playlist of the songs in my app.I want to play a song from this playlist on anther device (iphone) using bluetooth. 我在我的应用程序中有这个歌曲的播放列表。我想使用蓝牙在这个播放列表上播放另一个设备(iphone)上的歌曲。

This is what i have done so for 这就是我所做的

#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

请看屏幕截图

Now i have no idea how to proceed.Could someone please help me out??By selecting a song can it be played on another device?? 现在我不知道如何继续。有人可以帮助我吗?通过选择一首歌可以在另一台设备上播放?

GameKit is meant for inter-device games. GameKit适用于设备间游戏。 For this, you'd probably want to look at CBPeripheralManager or CBCentralManager depending on the device you're interacting with. 为此,您可能希望查看CBPeripheralManagerCBCentralManager,具体取决于您正在与之交互的设备。 It's lower level, so you'll have to do more work to set up the connection, but there's tons of tutorials and sample code to help you out. 它的级别较低,因此您需要做更多的工作来建立连接,但是有大量的教程和示例代码可以帮助您。

Ok lets get to the point where you are stuck.. You can use the same logic with some other libraries but this is how it should go. 好吧,让我们到达你被卡住的地步..你可以使用与其他一些库相同的逻辑,但这是应该如何去做的。 You will have to send the song data in chunks and sync to the other device while the previous chunk is received on the other end. 您必须以块的形式发送歌曲数据并同步到另一个设备,而另一端收到上一个块。 Since we are clear that bluetooth does not have a great band width u will have to specifically tune your transmission rate to the other device. 由于我们很清楚蓝牙没有很好的带宽,因此您必须专门调整传输速率到其他设备。 once a chunk is received on the device sent to your app instance running on that device should play it.. and in parallel probe for the newer chunks coming in from the sending device.. On the receiving end u can simply use FIFO method for handling the incoming chunks of your song data. 一旦在设备上接收到一个块,发送到你的应用程序实例上运行该设备就应该播放它...并且并行探测来自发送设备的新块。在接收端,你只需使用FIFO方法进行处理你的歌曲数据的传入块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM