我正在制作一个蓝牙聊天应用程序,我在其中使用了多点连接框架,我必须使用声音通知,并且消息将与通知一起显示-
#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 天里一直陷入这个问题。
谢谢
最佳答案
试试这个:
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Voicemail" ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound (soundID);
关于ios - 使用多点连接接收消息时如何发出声音通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35884954/