显示 UIAlertView 时播放本地通知默认声音?

发布于 2024-09-10 06:25:46 字数 730 浏览 4 评论 0原文

我正在为 iPhone 编写一个提醒应用程序,它使用本地通知显示提醒。

如果在应用程序运行时发出提醒,则不会显示本地通知。相反,在我的应用程序委托中调用 didReceiveLocalNotification 方法,并通过显示带有提醒文本的 UIAlertView 来模拟本地通知对话框。

当本地通知显示在应用程序外部时,设备会振动并发出 UILocalNotificationDefaultSoundName 已播放。同样,我想在显示 UIAlertView 时在应用程序中模仿这一点。

我可以通过调用 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) 来振动设备,但我不知道如何播放本地通知默认声音。没有等效的 SystemSoundID 常量,并且我不确定路径是什么。

tl;dr 我想在显示 UIAlertView 时播放本地通知默认声音。有什么想法吗?

I'm writing a reminders app for iPhone that displays reminders using local notifications.

If a reminder goes off while the application is running, the local notification isn't displayed. Instead, the didReceiveLocalNotification method is called in my app delegate, and I mimic the local notification dialog by displaying a UIAlertView with the reminder text.

When local notifications are displayed outside of the app, the device vibrates and the sound specified by UILocalNotificationDefaultSoundName is played. Again, I'd like to mimic this in the app when displaying the UIAlertView.

I can vibrate the device by calling AudioServicesPlaySystemSound(kSystemSoundID_Vibrate), but I can't figure out how to play the local notification default sound. There's no equivalent SystemSoundID constant, and I'm not sure what the path would be.

tl;dr I'd like to play the local notification default sound when displaying a UIAlertView. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜扑 2024-09-17 06:25:46

好问题。理想情况下,有一种使用音频服务选择系统声音的方法。然而,Apple 的“系统声音服务参考”中的以下声明却表明了相反的情况:

在 Mac OS X 中,当用户
将系统偏好设置配置为闪存
警报屏幕或声音
无法渲染,调用此
函数将导致屏幕
闪烁。在 Mac OS X 中,传递
持续的
kSystemSoundID_UserPreferredAlert 至
播放由选择的警报声音
用户在系统偏好设置中。 在 iOS 中
没有首选用户提醒
声音。

由于 SDK 似乎没有什么可提供的,您可能希望使用自己的 wav 文件来模仿系统声音。以下链接中有一个不错的库,也许它会提供您正在寻找的声音:http://sites.google.com/site/iphonesounds/iPhoneOriginalSystemSounds_WAV.zip

祝你好运!

Good question. Ideally, there would be a way of selecting a system sound using AudioServices. However, the following statement from Apple's "System Sound Services Reference" suggests otherwise:

In Mac OS X, when a user has
configured System Preferences to flash
the screen for alerts, or if sound
cannot be rendered, calling this
function will result in the screen
flashing. In Mac OS X, pass the
constant
kSystemSoundID_UserPreferredAlert to
play the alert sound selected by the
user in System Preferences. In iOS
there is no preferred user alert
sound.

Since it seems like the SDK has little to offer, you might wish to mimick the system sounds by using your own wav file. There is a nice library at the following link, perhaps it will have the sound you're looking for: http://sites.google.com/site/iphonesounds/iPhoneOriginalSystemSounds_WAV.zip

Good luck!

吃素的狼 2024-09-17 06:25:46

您可以通过以下方式播放默认通知声音:

AudioServicesPlaySystemSound(1315);

在这里您将找到您可以将 ids 用作 AudioServicesPlaySystemSound(id) 的参数。

良好的编码!

You can play the default notification sound in this way:

AudioServicesPlaySystemSound(1315);

Here you'll find the list of the ids you can use as parameter of AudioServicesPlaySystemSound(id).

Good coding!

在你怀里撒娇 2024-09-17 06:25:46

在.h文件中设置委托:

@interface ViewController : UIViewController <UIAlertViewDelegate>
{
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

@end

以及上面声明的设置方法。

在 .m 文件中执行以下操作:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/ma.mp3", [[NSBundle mainBundle] resourcePath]]];

    NSError *error;

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 

    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = -1;


    [audioPlayer play];

    [alert show]; 
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0) {
        [audioPlayer stop];

    }
    NSLog(@"U HAVE CLICKED BUTTON");
}

Set delegate in .h file:

@interface ViewController : UIViewController <UIAlertViewDelegate>
{
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

@end

And set method that above declared.

And in .m file do this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/ma.mp3", [[NSBundle mainBundle] resourcePath]]];

    NSError *error;

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 

    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = -1;


    [audioPlayer play];

    [alert show]; 
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0) {
        [audioPlayer stop];

    }
    NSLog(@"U HAVE CLICKED BUTTON");
}
梦忆晨望 2024-09-17 06:25:46

这是对安德鲁·利特尔答案的补充。

为了更好地模仿通知声音,您还应该配置音频会话:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryAmbient withOptions:AVAudioSessionCategoryOptionDuckOthers error:nil];

这在您听音乐并在此期间获取通知时至关重要。

上面的会话参数看起来与后台应用程序触发通知时播放的声音相同:

  • 音乐在通知声音之前静音并在通知声音之后恢复。
  • 设备静音开关处理正确 - 仅当开关打开时才播放声音 音量
  • 相同(请注意,如果您在后台应用程序和来自 Andrew Little 答案的文件时使用系统声音,您可能会获得不同的音量)

This is addition to Andrew Little answer.

To better mimic notification sound, you also should to configure audio session:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryAmbient withOptions:AVAudioSessionCategoryOptionDuckOthers error:nil];

This is essential when e.g. you listen to the music and obtain notification during this.

Parameters of session above looks the same as for sound played when notification fires with application in background:

  • music is silenced before and resumed after notification sound.
  • device silence switch is handled correctly - sound is played only when switch is on
  • volume is the same (note you may obtain different volume if you use system sound when application in background and files from Andrew Little answer)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文