2013-07-29 4 views
3

실시간으로 알람을 설정하거나 해제하고 단일 톤 오디오를 재생할 수있는 간단한 알람 알림 앱을 만들었습니다. 하지만 클래스 VOID으로 시작해야하는 사운드를 재생해야합니다. 나는 radiosound 재생을 시작하기 위해 알람을 설정할 수있는 방법iOS 알람 시계

- (void)playPause { 

    RADAppDelegate *appDelegate = (RADAppDelegate *)[[UIApplication sharedApplication] delegate]; 

    if (appDelegate.radiosound == 0){ 

     [appDelegate.radiosound play]; 

    } else { 

     [appDelegate.radiosound pause]; 

    } 

} 

:

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    dateTimerPicker.date = [NSDate date]; 

} 

- (void)presentMessage:(NSString *)message { 

    UIAlertView *alert = [[UIAlertView alloc] 
          initWithTitle:@"Hello!" 
          message:message 
          delegate:nil 
          cancelButtonTitle:@"OK" 
          otherButtonTitles: nil]; 
    [alert show]; 

} 

- (void)scheduleLocalNotificationWithDate:(NSDate *)fireDate { 

    UILocalNotification *notification = [[UILocalNotification alloc]init]; 
    notification.fireDate = fireDate; 
    notification.alertBody = @"Time to wake up!!"; 
    notification.soundName = @"PhoneOld.mp3"; 
    [self playPause]; 

    [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 

} 

- (IBAction)alarmSetOn:(id)sender{ 

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    dateFormatter.timeZone = [NSTimeZone defaultTimeZone]; 
    dateFormatter.timeStyle = NSDateFormatterShortStyle; 
    dateFormatter.dateStyle = NSDateFormatterShortStyle; 

    NSString *dateTimeString = [dateFormatter stringFromDate:dateTimerPicker.date]; 
    NSLog(@"Alarm Set: %@", dateTimeString); 


    [self scheduleLocalNotificationWithDate:dateTimerPicker.date]; 
    [self presentMessage:@"Alarm ON!"]; 


} 

- (IBAction)alarmSetOff:(id)sender { 
    NSLog(@"Alarm Off"); 

    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 
    [self presentMessage:@"Alarm OFF!"]; 
} 

이 내 VOID입니다 :

얻을 및 경보 알림을 시작하려면 : 아래

코드입니다 0과 같은 0 인 경우 :

notification.soundName = [self playPause]; 

그러나 이것은 NSString이라는 것을 알고 있습니다.

+0

IMO는 예약 알림을 취소하고 'notification.soundName = radioSound;'(으)로 새 알림 일정을 변경합니다. – Bala

+1

무엇이 질문입니까? 알림에서 스트리밍 사운드를 재생하는 방법 (할 수 없음), 앱에 알림 호출을하는 방법 (사용자가 알림을 허용하지 않는 한 알림을 보내지 않음), 알림에서 소리를 재생하는 방법 Aniket은 아래 답변), 또는 다른 뭔가? – AndrewS

답변

2

예약 알림에 사운드 이름을 지정할 필요가 없습니다. 아래 그림과 같이 playPause 메소드를 호출하고 알림에서 사운드 파일 이름을 가져온 다음 NSString에 할당하고 속성을 appDelegate로 설정하면됩니다. 그리고 액세스하여 해당 파일을 재생하십시오.

AppDelegate.h

@property (nonatomic,strong) NSString *nsStr_soundFile; 

AppDelegate.m이

@synthesize nsStr_soundFile; 

- (void)application:(UIApplication *)application 
didReceiveLocalNotification:(UILocalNotification *)notification { 


//Give call to play sound method. 

self.nsStr_soundFile=notification.soundName; 
    VOID *obj=[VOID alloc]init]; 
     [obj playPause]; 

} 
+0

사운드 파일이 아니고 스트리밍 오디오 인 경우, notification.soundName이 호출되면 마지막 스트리밍 설정으로 재생을 시작하려고 시도합니다. – BlackSheep

+1

알람 알림은 매우 제한된 것입니다. 스트리밍 리소스를 마술처럼 재생할 수는 없습니다. 문서를보십시오. 응용 프로그램의 기본 번들 또는 기본 사운드에서 사운드 리소스를 재생할 수 있습니다. – AndrewS

+0

괜찮습니까? 모든 기능을 아무 문제없이 재 작성 할 수는 있지만, 처음으로 알람 시계를 만들려고 시도하기 때문에 예제가 필요합니다. – BlackSheep

0

당신은 앱에서 설정하여 아이폰 OS의 멀티 태스킹의 선택을 해제와 트릭을 할 수 .plist 파일이 키 UIApplicationExitsOnSuspend to 쓰여진대로 here

앱이 선택 해제되면 운영 중지, 비활성 및 활성 상태 사이를 순환하고 백그라운드 나 일시 중지 상태로 들어 가지 않습니다.

사전 멀티 태스킹 호환성 모드로 실행되는 앱은 앱이 전경에 인 동안 사용자가 기기를 잠그면 이 계속 실행됩니다. 모든 앱은 알람 시간을 기다리고 의 사용자 지정 코드를 실행해야합니다.