而這次程式主要是用到Gamekit API 還有Avaudioplayer
" test10ViewController.h"
//用Gamekit api前 要先import以下這行
#import <GameKit/GameKit.h>
#import <UIKit/UIKit.h>
//用GKSessionDelegate協定
@interface test10ViewController : UIViewController <GKSessionDelegate>
UILabel *display;
NSString *sessionID;
//產生GKsession 物件
GKSession *myGKSession;
NSString *peerList;
AVAudioPlayer *audioPlayer;
//存放物件的陣列
NSMutableArray *mPeers;
}
@property(nonatomic,retain)IBOutlet UILabel *display;
-(IBAction)play:(id)sender;
-(IBAction)stop:(id)sender;
-(void) setupSession;
- (void) destroySession;
-(void) disconnectCurrentCall;
-(void) sendData;
@end
" test10ViewController.m"
//在這邊先定義sessionID 兩台手機靠這個來辨識對方 所以雙方都要一樣
有點像在Java寫的UUID
#define sessionID @"connecttest"
- (void)viewDidLoad {
[super viewDidLoad];
//呼叫setupSession method
[self setupSession];
//指定音樂檔案的路徑
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audio.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
//初始化陣列
mPeers=[[NSMutableArray alloc] init];
}
-(void)setupSession
{
//GKSessionModerPeer 表示此程式可當Server or Client 詳細的說明 請看Library
//初始化Session
myGKSession = [[GKSession alloc] initWithSessionID:sessionID displayName:nil sessionMode:GKSessionModePeer];
myGKSession.delegate = self;
[myGKSession setDataReceiveHandler:self withContext:nil];
//設定為YES 將服務發佈出去
myGKSession.available = YES;
}
-(IBAction)play:(id)sender
{
audioPlayer.currentTime = 0;
[audioPlayer play];
//按下播放按鈕之後 將字串傳出去
[self sendData];
}
-(IBAction)stop:(id)sender
{
[audioPlayer stop];
}
//此處可將字串傳送給對方 我這邊是用來讓另外一台手機判斷 何時放音樂之用
-(void) sendData
{
NSString *str=@"test";
[myGKSession sendData:[str dataUsingEncoding: NSASCIIStringEncoding] toPeers:mPeers withDataMode:GKSendDataReliable error:nil];
}
//當收到data時 呼叫此method
-(void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context
{
NSString *str4 = @"test";
NSString* aStr;
aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
//判斷字串是否相同
if([str4 isEqualToString:aStr])
{
//相同就放音樂
[audioPlayer play];
}
}
- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state
{
switch (state) {
//發現服務時 會跳到此case
case GKPeerStateAvailable:
peerList=peerID;
display.text=[myGKSession displayNameForPeer:peerID];
//將peerID加到陣列中 用來傳送資料 要傳給哪個peer的紀錄之用
[mPeers addObject:peerID];
//連線到提供服務的peer
[myGKSession connectToPeer:peerID withTimeout:10.0];
break;
case GKPeerStateUnavailable:
peerList=nil;
display.text=@"No Service";
break;
case GKPeerStateDisconnected:
peerList=nil;
break;
case GKPeerStateConnecting:
break;
//連線之後 就會跳到此case
case GKPeerStateConnected:
display.text=@"Connect Sucessfully";
//切換到view2
[[NSBundle mainBundle]loadNibNamed:@"view2" owner:self options:NULL];
break;
default:
break;
}
//當有client請求連線 就會呼叫此method
- (void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID
{
//接受此client的連線要求
[myGKSession acceptConnectionFromPeer:peerID error:nil];
}
PS:另外要記得加入Gamekit framework
程式以上就大致完成了
Game Kit programming Guide
沒有留言:
張貼留言