想要在通知事件发生时播放我的声音文件,然后停止

发布于 2024-12-12 18:06:00 字数 5429 浏览 0 评论 0原文

我正在制作一个闹钟,我希望当通过 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 技术交流群。

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

发布评论

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

评论(1

一腔孤↑勇 2024-12-19 18:06:00

根据 soundName 的文档,您应该

指定应用程序主包中声音资源的文件名(包括扩展名)

因此我建议您将 -[The420DudeViewController onTapSetAlarm] 这行更改

    notif.soundName = [[NSBundle mainBundle]pathForResource:@"jet" ofType:@"wav"];

为以下内容:

    notif.soundName = @"jet.wav";

According to the documentation for soundName, you should

specify the filename (including extension) of a sound resource in the application’s main bundle

Thus I suggest you change this line of -[The420DudeViewController onTapSetAlarm]:

    notif.soundName = [[NSBundle mainBundle]pathForResource:@"jet" ofType:@"wav"];

to the following:

    notif.soundName = @"jet.wav";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文