单击tableview单元格时如何在后台播放音频

时间:2011-12-13 11:12:57

标签: iphone objective-c uitableview

我有tableview,我在tableview中显示资源文件夹的.wav文件。这些歌曲在桌面视图中正确显示。当我在该特定歌曲的tableview单元格中选择一首特定歌曲时,会显示一个复选标记,同时该歌曲应该在背景上播放。我已经完成了此代码以播放特定的所选歌曲背景,但它不能正常工作。可能是什么问题。谢谢。

- (void)viewDidLoad {
    [super viewDidLoad];

    app = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];

    app.newcheckarray = [[[NSBundle mainBundle]pathsForResourcesOfType:@".wav" inDirectory:nil]retain];
    tblView.delegate = self;
    tblView.dataSource = self;


}



- (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:
            soundstring = [app.newcheckarray objectAtIndex:0];
            cell.textLabel.text = [soundstring lastPathComponent];

            break;
        case 1:
            soundstring  =[app.newcheckarray objectAtIndex:1];
            cell.textLabel.text = [soundstring  lastPathComponent];
            break;


        default:
            break;
    }
    cell.accessoryType = ([indexPath isEqual:rowselection]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;

    return cell;
}



- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.rowselection = indexPath;

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [tableView reloadData];

    for (int i= 0;i <[[self.navigationController viewControllers] count];++i)
    {
        UIViewController *aController = [[self.navigationController viewControllers]objectAtIndex:i];
        if ([aController isKindOfClass:[TAddAlarmController class]])
        {
            NSString *newsong1 = [app.newcheckarray objectAtIndex:indexPath.row];
            app.newsong = [newsong1 lastPathComponent];
            NSURL *pewPewURL1 = [NSURL fileURLWithPath:app.newsong];
            AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:pewPewURL1 error:nil];

            [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
            [[AVAudioSession sharedInstance] setActive: YES error: nil];
            [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

            [audioPlayer play];         
            [delegate selectsoundControllerReturnedsound:app.newsong forIndexPath:self.indexPathToChange];

        }


    }


}

在didselect中我正在初始化我的音频播放器,但它不播放音频。

1 个答案:

答案 0 :(得分:0)

我在UIAlertView上使用了以下代码。你可以在

中试试
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

方法

您需要添加AudioToolbox.framework

#import <AudioToolbox/AudioServices.h>

代码:

    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Voicemail" ofType:@"wav"];       
    SystemSoundID soundID;      
    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);       
    AudioServicesPlaySystemSound (soundID);