我正在制作一个蓝牙聊天应用程序,我正在使用多重连接框架,我必须使用声音通知,并且消息将显示通知 -
#import "MCManager.h"
#include <AudioToolbox/AudioToolbox.h>
#import "AppDelegate.h"
@implementation MCManager
-(id)init{
self = [super init];
if (self) {
_peerID = nil;
_session = nil;
_browser = nil;
_advertiser = nil;
}
return self;
}
#pragma mark - Public method implementation
-(void)setupPeerAndSessionWithDisplayName:(NSString *)displayName
{
_peerID = [[MCPeerID alloc] initWithDisplayName:displayName];
_session = [[MCSession alloc] initWithPeer:_peerID];
_session.delegate = self;
}
-(void)setupMCBrowser{
_browser = [[MCBrowserViewController alloc] initWithServiceType:@"chat-files" session:_session];
}
-(void)advertiseSelf:(BOOL)shouldAdvertise
{
if (shouldAdvertise)
{
_advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:@"chat-files"
discoveryInfo:nil
session:_session];
[_advertiser start];
}
else
{
[_advertiser stop];
_advertiser = nil;
}
}
#pragma mark - MCSession Delegate method implementation
-(void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state{
NSDictionary *dict = @{@"peerID": peerID,
@"state" : [NSNumber numberWithInt:state]
};
[[NSNotificationCenter defaultCenter] postNotificationName:@"MCDidChangeStateNotification"
object:nil
userInfo:dict];
}
-(void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID{
NSDictionary *dict = @{@"data": data,
@"peerID": peerID
};
[[NSNotificationCenter defaultCenter] postNotificationName:@"MCDidReceiveDataNotification"
object:nil
userInfo:dict];
SystemSoundID soundID;
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef ref = CFBundleCopyResourceURL(mainBundle, (CFStringRef)@"Voicemail.wav", NULL, NULL);
AudioServicesCreateSystemSoundID(ref, &soundID);
AudioServicesPlaySystemSound(soundID);
}
-(void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress{
NSDictionary *dict = @{@"resourceName" : resourceName,
@"peerID" : peerID,
@"progress" : progress
};
[[NSNotificationCenter defaultCenter] postNotificationName:@"MCDidStartReceivingResourceNotification"
object:nil
userInfo:dict];
dispatch_async(dispatch_get_main_queue(), ^{
[progress addObserver:self
forKeyPath:@"fractionCompleted"
options:NSKeyValueObservingOptionNew
context:nil];
});
}
-(void)session:(MCSession *)session didFinishReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID atURL:(NSURL *)localURL withError:(NSError *)error{
NSDictionary *dict = @{@"resourceName" : resourceName,
@"peerID" : peerID,
@"localURL" : localURL
};
[[NSNotificationCenter defaultCenter] postNotificationName:@"didFinishReceivingResourceNotification"
object:nil
userInfo:dict];
}
-(void)session:(MCSession *)session didReceiveStream:(NSInputStream *)stream withName:(NSString *)streamName fromPeer:(MCPeerID *)peerID
{
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"MCReceivingProgressNotification"
object:nil
userInfo:@{@"progress": (NSProgress *)object}];
}
@end
我在didReceiveData
中编写此代码,以便在接收数据时通知声音,但它无法正常工作我正在发送消息但未发出任何声音通知。
请帮助我,我在过去10天遇到了这个问题。
感谢
答案 0 :(得分:0)
试试这个:
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Voicemail" ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound (soundID);
答案 1 :(得分:0)
您必须在有效负载中传递声音文件名,否则默认通知音调为default.mp3。