Iphone 想在通知事件发生时播放我的声音文件,然后停止
我正在制作一个闹钟,我希望当通过Datepicker选择的时间发生时,而不仅仅是通知和徽章发生时,我希望播放我的自定义声音文件,即jet.wav(它的时间小于30秒,格式为.wav)。我希望在收到通知后立即播放声音,当我单击应用程序或警报视图时,它应该停止。谁能帮帮我吗。以下是我正在尝试的:- 代码:-Iphone 想在通知事件发生时播放我的声音文件,然后停止,iphone,objective-c,uidatepicker,uilocalnotification,Iphone,Objective C,Uidatepicker,Uilocalnotification,我正在制作一个闹钟,我希望当通过Datepicker选择的时间发生时,而不仅仅是通知和徽章发生时,我希望播放我的自定义声音文件,即jet.wav(它的时间小于30秒,格式为.wav)。我希望在收到通知后立即播放声音,当我单击应用程序或警报视图时,它应该停止。谁能帮帮我吗。以下是我正在尝试的:- 代码:- @class The420DudeViewController; @interface The420DudeAppDelegate : NSObject <UIApplicationDe
@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
@class视图控制器;
@接口应用程序委托:NSObject{
UIWindow*窗口;
视图控制器*视图控制器;
}
@属性(非原子,保留)IBUIWindow*window;
@属性(非原子,保留)IBOutlet,即viewController*viewController;
extern NSString*kRemindMeNotificationDataKey;
@应用程序委托的实现
@合成窗口;
@综合视图控制器;
NSString*kRemindMeNotificationDataKey=@“kRemindMeNotificationDataKey”;
#布拉格标记-
#pragma标记===应用程序委托方法===
#布拉格标记-
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项{
类cls=NSClassFromString(@“UILocalNotification”);
如果(cls){
UILocalNotification*通知=[launchOptions objectForKey:
UIApplicationLaunchActionSlocalNotificationKey];
如果(通知){
NSString*提醒文本=[notification.userInfo
objectForKey:kRemindMeNotificationDataKey];
[查看控制器显示提醒:提醒文字];
}
}
application.applicationBadgeNumber=0;
[窗口添加子视图:viewController.view];
[self.window makeKeyAndVisible];
返回YES;
}
-(无效)应用程序将进入前台:(UIApplication*)应用程序{
application.applicationBadgeNumber=0;
}
-(无效)申请:(UIApplication*)申请
didReceiveLocalNotification:(UILocalNotification*)通知{
application.applicationBadgeNumber=0;
NSString*提醒文本=[notification.userInfo
objectForKey:kRemindMeNotificationDataKey];
[查看控制器显示提醒:提醒文字];
}
-(无效)解除锁定{
[视图控制器释放];
[窗口释放];
[super dealoc];
}
@结束
@与ViewController的接口:UIViewController{
IBUINAVIGATIONBAR*标题栏;
IBUIButton*设置报警按钮;
IBUIDatePicker*选择TimePicker;
AVAudioPlayer*播放器;
}
@不动产(非原子,保留)IBUINAVIGATIONBAR*标题栏;
@属性(非原子,保留)IBUIButton*setAlarmButton;
@属性(非原子,保留)IBUIDatePicker*selectTimePicker;
-(IBAction)onTapSetAlarm;
-(void)showAlerter:(NSString*)文本;
@结束
@视图控制器的实现
@合成标题栏,设置报警按钮,选择时间选择器;
#布拉格标记-
#pragma标记===初始化和关闭===
#布拉格标记-
-(无效)viewDidLoad{
[超级视图下载];
选择TimePicker.minimumDate=[NSDate日期];
}
-(无效)视图卸载{
[超级视频下载];
self.setAlarmButton=nil;
self.selectTimePicker=nil;
}
/*
-(无效)视图将显示:(BOOL)动画
{
NSString*path=[[NSBundle mainBundle]pathForResource:@“mp3”类型的@“Song1”;
NSURL*url=[NSURL fileURLWithPath:path];
player=[[AVAudioPlayer alloc]initWithContentsOfURL:url错误:nil];
//[玩家游戏];
}
*/
-(IBAction)onTapSetAlarm
{
[[UIApplication sharedApplication]取消所有本地通知];
类cls=NSClassFromString(@“UILocalNotification”);
如果(cls!=零){
UILocalNotification*notif=[[cls alloc]init];
notif.fireDate=[selectTimePicker date];
notif.timeZone=[NSTimeZone defaultTimeZone];
notif.alertBody=@“你忘了什么吗?”;
notif.alertAction=@“向我展示”;
notif.repeatInterval=NSDayCalendarUnit;
//notif.soundName=UILocalNotificationDefaultSoundName;
notif.soundName=[[NSBundle mainBundle]路径资源:@“jet”类型:@“wav”];
notif.applicationBadgeNumber=1;
NSDictionary*userDict=[NSDictionary Dictionary WithObject:@“Mayank”
forKey:kRemindMeNotificationDataKey];
notif.userInfo=userDict;
[[UIApplication sharedApplication]scheduleLocalNotification:notif];
[不释放];
}
/*
NSDateFormatter*timeFormat=[[NSDateFormatter alloc]init];
[时间格式setDateFormat:@“HH:mm a”];
NSDate*selectedDate=[[NSDate alloc]init];
selectedDate=[selectTimePicker日期];
NSString*时间=[timeFormat stringFromDate:selectedDate];
UIAlertView*警报=[[UIAlertView alloc]
initWithTitle:@“Time selected”消息:时间委托:nil CancelButtonTile:@“YES”其他按钮:nil];
[警报显示];
[警报发布];
//[时间表发布];
//[选择日期释放];
*/
}
#布拉格标记-
#pragma标记===公共方法===
#布拉格标记-
-(无效)显示提醒:(NSString*)文本{
UIAlertView*alertView=[[UIAlertView alloc]initWithTitle:@“提醒”
消息:@“文本”代表:无
取消按钮:@“确定”
其他按钮:无];
[警报视图显示];
[警报视图发布];
}
-(无效)解除锁定{
[super dealoc];
[标题栏释放];
[设置报警按钮释放];
[选择时间选择器释放];
}
@结束
根据forsoundName
,您应该
指定应用程序主捆绑包中声音资源的文件名(包括扩展名)
因此,我建议您更改这一行的-[The420DudeViewController ontapsetalum]
:
notif.soundName = [[NSBundle mainBundle]pathForResource:@"jet" ofType:@"wav"];
对下列事项:
notif.soundName = @"jet.wav";
非常感谢,我真是个笨蛋