【问题标题】:how to play an audio in background when the tableview cells are clicked单击表格视图单元格时如何在后台播放音频
【发布时间】:2011-12-13 20:01:23
【问题描述】:

我有 tableview 我在 tableview 中显示我的资源文件夹的 .wav 文件。歌曲在 tableview 中正确显示。当我在特定歌曲的 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 中,我正在初始化我的音频播放器,但它不播放音频。

【问题讨论】:

    标签: iphone objective-c uitableview


    【解决方案1】:

    我在 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);
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    • 2016-07-18
    • 1970-01-01
    相关资源
    最近更新 更多