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

时间:2011-10-28 12:43:23

标签: iphone objective-c uidatepicker uilocalnotification

我正在制作一个闹钟,我希望当通过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

1 个答案:

答案 0 :(得分:1)

根据soundName的{​​{3}},你应该

  

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

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

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

以下内容:

    notif.soundName = @"jet.wav";