想要在通知事件发生时播放我的声音文件,然后停止
我正在制作一个闹钟,我希望当通过 Datepicker 选择的时间发生而不是仅发生通知和徽章时,我想播放我的自定义声音文件,即 jet.wav (它的时间少于 30 秒,格式为 .wav格式)。我希望一旦我的通知发生,它就应该播放声音,当我单击应用程序或警报视图时,它应该停止。那么任何人都可以帮助我吗?这就是我正在尝试的:-
代码:-
@class The420DudeViewController;
@interface The420DudeAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
The420DudeViewController *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet The420DudeViewController *viewController;
extern NSString *kRemindMeNotificationDataKey;
@implementation The420DudeAppDelegate
@synthesize window;
@synthesize viewController;
NSString *kRemindMeNotificationDataKey = @"kRemindMeNotificationDataKey";
#pragma mark -
#pragma mark === Application Delegate Methods ===
#pragma mark -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
Class cls = NSClassFromString(@"UILocalNotification");
if (cls) {
UILocalNotification *notification = [launchOptions objectForKey:
UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
NSString *reminderText = [notification.userInfo
objectForKey:kRemindMeNotificationDataKey];
[viewController showReminder:reminderText];
}
}
application.applicationIconBadgeNumber = 0;
[window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
application.applicationIconBadgeNumber = 0;
}
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
application.applicationIconBadgeNumber = 0;
NSString *reminderText = [notification.userInfo
objectForKey:kRemindMeNotificationDataKey];
[viewController showReminder:reminderText];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
@end
@interface The420DudeViewController : UIViewController {
IBOutlet UINavigationBar *titleBar;
IBOutlet UIButton *setAlarmButton;
IBOutlet UIDatePicker *selectTimePicker;
AVAudioPlayer *player;
}
@property(nonatomic, retain) IBOutlet UINavigationBar *titleBar;
@property(nonatomic, retain) IBOutlet UIButton *setAlarmButton;
@property(nonatomic, retain) IBOutlet UIDatePicker *selectTimePicker;
-(IBAction)onTapSetAlarm;
- (void)showReminder:(NSString *)text;
@end
@implementation The420DudeViewController
@synthesize titleBar,setAlarmButton,selectTimePicker;
#pragma mark -
#pragma mark === Initialization and shutdown ===
#pragma mark -
- (void)viewDidLoad {
[super viewDidLoad];
selectTimePicker.minimumDate = [NSDate date];
}
- (void)viewDidUnload {
[super viewDidUnload];
self.setAlarmButton = nil;
self.selectTimePicker = nil;
}
/*
-(void)viewWillAppear:(BOOL)animated
{
NSString *path = [[NSBundle mainBundle]pathForResource:@"Song1" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
// [player play];
}
*/
-(IBAction)onTapSetAlarm
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Class cls = NSClassFromString(@"UILocalNotification");
if (cls != nil) {
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [selectTimePicker date];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = @"Did you forget something?";
notif.alertAction = @"Show me";
notif.repeatInterval = NSDayCalendarUnit;
// notif.soundName = UILocalNotificationDefaultSoundName;
notif.soundName = [[NSBundle mainBundle]pathForResource:@"jet" ofType:@"wav"];
notif.applicationIconBadgeNumber = 1;
NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"Mayank"
forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}
/*
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm a"];
NSDate *selectedDate = [[NSDate alloc] init];
selectedDate = [selectTimePicker date];
NSString *theTime = [timeFormat stringFromDate:selectedDate];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Time selected" message:theTime delegate:nil cancelButtonTitle:@"YES" otherButtonTitles:nil];
[alert show];
[alert release];
// [timeFormat release];
// [selectedDate release];
*/
}
#pragma mark -
#pragma mark === Public Methods ===
#pragma mark -
- (void)showReminder:(NSString *)text {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Reminder"
message:@" TEXT " delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
- (void)dealloc {
[super dealloc];
[titleBar release];
[setAlarmButton release];
[selectTimePicker release];
}
@end
I am making an Alarm clock in which i want that when the selected time through Datepicker occur instead of only notification and Badge occurance , I wanted to play my custom Sound file i.e. jet.wav (it is of less than 30 sec and in .wav format).i want that as soon as my notification occurs it should play a sound and when i click on the app or alert view then it should stop. So can anyone please help me out. here is what i am trying :-
Code:-
@class The420DudeViewController;
@interface The420DudeAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
The420DudeViewController *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet The420DudeViewController *viewController;
extern NSString *kRemindMeNotificationDataKey;
@implementation The420DudeAppDelegate
@synthesize window;
@synthesize viewController;
NSString *kRemindMeNotificationDataKey = @"kRemindMeNotificationDataKey";
#pragma mark -
#pragma mark === Application Delegate Methods ===
#pragma mark -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
Class cls = NSClassFromString(@"UILocalNotification");
if (cls) {
UILocalNotification *notification = [launchOptions objectForKey:
UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
NSString *reminderText = [notification.userInfo
objectForKey:kRemindMeNotificationDataKey];
[viewController showReminder:reminderText];
}
}
application.applicationIconBadgeNumber = 0;
[window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
application.applicationIconBadgeNumber = 0;
}
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
application.applicationIconBadgeNumber = 0;
NSString *reminderText = [notification.userInfo
objectForKey:kRemindMeNotificationDataKey];
[viewController showReminder:reminderText];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
@end
@interface The420DudeViewController : UIViewController {
IBOutlet UINavigationBar *titleBar;
IBOutlet UIButton *setAlarmButton;
IBOutlet UIDatePicker *selectTimePicker;
AVAudioPlayer *player;
}
@property(nonatomic, retain) IBOutlet UINavigationBar *titleBar;
@property(nonatomic, retain) IBOutlet UIButton *setAlarmButton;
@property(nonatomic, retain) IBOutlet UIDatePicker *selectTimePicker;
-(IBAction)onTapSetAlarm;
- (void)showReminder:(NSString *)text;
@end
@implementation The420DudeViewController
@synthesize titleBar,setAlarmButton,selectTimePicker;
#pragma mark -
#pragma mark === Initialization and shutdown ===
#pragma mark -
- (void)viewDidLoad {
[super viewDidLoad];
selectTimePicker.minimumDate = [NSDate date];
}
- (void)viewDidUnload {
[super viewDidUnload];
self.setAlarmButton = nil;
self.selectTimePicker = nil;
}
/*
-(void)viewWillAppear:(BOOL)animated
{
NSString *path = [[NSBundle mainBundle]pathForResource:@"Song1" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
// [player play];
}
*/
-(IBAction)onTapSetAlarm
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Class cls = NSClassFromString(@"UILocalNotification");
if (cls != nil) {
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [selectTimePicker date];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = @"Did you forget something?";
notif.alertAction = @"Show me";
notif.repeatInterval = NSDayCalendarUnit;
// notif.soundName = UILocalNotificationDefaultSoundName;
notif.soundName = [[NSBundle mainBundle]pathForResource:@"jet" ofType:@"wav"];
notif.applicationIconBadgeNumber = 1;
NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"Mayank"
forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}
/*
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm a"];
NSDate *selectedDate = [[NSDate alloc] init];
selectedDate = [selectTimePicker date];
NSString *theTime = [timeFormat stringFromDate:selectedDate];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Time selected" message:theTime delegate:nil cancelButtonTitle:@"YES" otherButtonTitles:nil];
[alert show];
[alert release];
// [timeFormat release];
// [selectedDate release];
*/
}
#pragma mark -
#pragma mark === Public Methods ===
#pragma mark -
- (void)showReminder:(NSString *)text {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Reminder"
message:@" TEXT " delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
- (void)dealloc {
[super dealloc];
[titleBar release];
[setAlarmButton release];
[selectTimePicker release];
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据
soundName
的文档,您应该因此我建议您将
-[The420DudeViewController onTapSetAlarm]
这行更改为以下内容:
According to the documentation for
soundName
, you shouldThus I suggest you change this line of
-[The420DudeViewController onTapSetAlarm]
:to the following: