如何在桌面单元格点击时在背景中播放音频
问题描述:
我有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我正在初始化我的audioplayer,但它不播放音频。
答
我已经使用下面的代码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);
我已经做到了这一点代码,但问题是soundID总回报0 – Rani
检查更新回答 – Maulik
你可以参考http://www.edumo bile.org/iphone/ipad-development/playing-audio-file-in-ipad/ – Maulik