使用多点连接收到消息时如何发出声音通知?
Posted
技术标签:
【中文标题】使用多点连接收到消息时如何发出声音通知?【英文标题】:How to make sound notification when message receives using multipeer connectivity? 【发布时间】:2016-03-09 06:56:44 【问题描述】:我正在制作一个蓝牙聊天应用程序,其中我正在使用多点连接框架,我必须使用声音通知,并且消息将显示通知 -
#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 天开始就陷入了这个问题。
谢谢
【问题讨论】:
【参考方案1】:试试这个:
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Voicemail" ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound (soundID);
【讨论】:
@AayushKatiyar 如果可行,您可以检查我的答案是否有效吗?【参考方案2】:您必须在有效负载中传递您的声音文件名,否则 default.mp3 为默认通知音。
【讨论】:
以上是关于使用多点连接收到消息时如何发出声音通知?的主要内容,如果未能解决你的问题,请参考以下文章
使用 django 频道发生 onmessage 事件时如何触发通知声音?