如何在iPhone中将歌曲从tableview设置为通知声音?

时间:2011-12-09 09:22:32

标签: iphone objective-c

我有一个应用程序,我在我的资源文件夹中添加了3个.wav文件。然后我在这个类的didselectRowatIndexPath中获取数组中的所有3首歌曲。我将数组设置为nsstring变量。在这个nsstring变量中,我选择了它的文件路径的歌曲正在存储,我正在我的类中访问这个nsstring变量,我正在调度通知,并在其中的notification.soundname属性我设置了存储歌曲文件路径的变量。

但问题是当收到通知而没有播放特定歌曲而是播放默认通知声音时。可能是什么问题?这是我的代码。

这是我选择歌曲的课程:

- (void)viewDidLoad {
    [super viewDidLoad];
    //this is used to search all the song files with wav extension in the resources folder.
    app = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];
    soundarray = [[[NSBundle mainBundle]pathsForResourcesOfType:@".wav" inDirectory:nil]retain];
    tblView.delegate = self;
    tblView.dataSource = self;



}


#pragma mark - UITableView, UITableViewDataSource delegate.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{

    return [soundarray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                       reuseIdentifier:CellIdentifier] autorelease];
    }


    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = [soundarray objectAtIndex:0]; 
            break;
        case 1:
            cell.textLabel.text = [soundarray objectAtIndex:1];
            break;
        case 2:
            cell.textLabel.text =[soundarray objectAtIndex:2];
            break;

        default:
            break;
    }
    cell.textLabel.text = [soundarray objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    app.newsong = [soundarray objectAtIndex:indexPath.row];
    [delegate selectsoundControllerReturnedsound:app.newsong forIndexPath:self.indexPathToChange];
    [self.navigationController popViewControllerAnimated:YES];
}

和我正在安排通知的类我已经加入了我的app委托字符串变量,其中存储了声音文件路径。

notification.soundName = app.newsong;

但通知会显示默认通知声音。

这是用于安排通知的代码

-(void)scheduleNotification

{
    //itemDate = [[NSDate alloc] init];
    timepicker = [[TTimePickerController alloc]init];

    double double_date = (double)[[NSDate date] timeIntervalSince1970];     

      NSDate *now= [NSDate dateWithTimeIntervalSince1970:double_date];
    selectedWeek = [[NSUserDefaults standardUserDefaults]objectForKey:@"weekday"];
    //NSDate *now = [NSDate date];
    NSMutableArray *array = [[NSUserDefaults standardUserDefaults]objectForKey:@"time"];
    //NSDate *testdate = [[NSUserDefaults standardUserDefaults]objectForKey:@"time" ];
    NSInteger repeatCount = 7;
    NSDate *today = [NSDate date];
    NSDate *date1;
    NSString *Stringdate;
    calendar = [NSCalendar currentCalendar];

    NSDateFormatter *format = [[[NSDateFormatter alloc]init]autorelease];
    format.dateFormat = @"yyyy-MM-dd hh:mm";

    NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
    dateFormat.dateFormat = @"yyyy-MM-dd";

    NSDateFormatter *timeFormat = [[[NSDateFormatter alloc]init]autorelease];
    timeFormat.dateFormat = @"hh:mm";

    for (NSDate *date in array)

    {
        if ([date isEqualToDate:now]) 
        {
            itemDate = date;
            //app.timerdate = date;
            NSLog(@"%@",itemDate);
        } 
        else if([date earlierDate:now]){
            itemDate = date;

            //app.timerdate = date;

        }
        else if([date laterDate:now])
        {
            itemDate = date;

            //app.timerdate =date;
        }

    }

        UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease]; 
    {
            BOOL idGenereated=FALSE;
        for (NSInteger i = 0; i < repeatCount; i++) 
        {

            date1 = [today dateByAddingTimeInterval:i*24*60*60];
            dateComponents  =[calendar components:NSWeekdayCalendarUnit fromDate:date1];
            if ([selectedWeek containsObject:[NSNumber numberWithInteger:dateComponents.weekday - 1]]) {
                Stringdate = [NSString stringWithFormat:@"%@ %@",[dateFormat stringFromDate:date1 ],[timeFormat stringFromDate:itemDate]];

                //this line has been commented becuase alarmid was not getting generated
                //if ([today compare:[format dateFromString:Stringdate]] ==NSOrderedAscending)
//              {
                    notification.fireDate = itemDate;
                    notification.timeZone = [NSTimeZone defaultTimeZone];
                    notification.alertBody = @"Alarm";
                    notification.alertAction = @"View";
                    //notification.soundName = UILocalNotificationDefaultSoundName;
                    //notification.soundName = @"Boat Coast Guard, sirens 1 1.wav";
                    notification.soundName = app.newsong;
                    NSLog(@"notification sound name:%@",notification.soundName);
                    notification.applicationIconBadgeNumber=1;
                    notification.repeatInterval = 0;
                    notification.alertBody = @"Alarm date";
                    AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
                    NSString *str_alarmbody = notification.alertBody; 
                    NSString *str_id = [[str_alarmbody componentsSeparatedByString:@" "] objectAtIndex:1];
                    NSLog(@"Notif userinfo:%@",str_id);
                    NSLog(@"%@",notification.alertBody);
                    //NSString *firstLetter;

                    if(!idGenereated)
                    {
                        NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
                        NSNumber *timeStampObj = [NSNumber numberWithInt:timeStamp];
                        NSLog(@"%d",timeStampObj);

                        am.AlarmID =(int)timeStampObj;
                        NSLog(@"%@",am.AlarmID);

                        idGenereated=TRUE;
                    }

                    app = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];
                    [app.newtest setObject:[NSNumber numberWithInteger:am.AlarmID]forKey:@"idtemp"];
                    [app.newtest setObject:app.timerdate forKey:@"iddate"];     

                    notification.userInfo = app.newtest;
                    NSLog(@"notif userinfo:%@",notification.userInfo);
                    [[UIApplication sharedApplication]scheduleLocalNotification:notification];
                //this line has been commented becuase alarmid was not getting generated

                //}



                }

            }
        }

    }