0
테이블 뷰에서 테이블 뷰에 리소스 폴더의 .wav 파일을 표시합니다. 노래가 tableview에 올바르게 표시됩니다. 특정 노래의 tableview 셀에서 특정 노래를 선택하면 체크 표시가 나타나고 동시에 노래가 배경에서 재생됩니다 .i 선택한 노래를 재생할 때이 코드를 완료했습니다. 배경이 제대로 작동하지 않습니다. 무슨 문제가있을 수 있습니다. 고맙습니다.테이블 뷰 셀을 클릭 할 때 백그라운드에서 오디오를 재생하는 방법
- (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];
}
}
}
내 오디오 플레이어를 초기화하고 있지만 오디오를 재생하지 못했습니다.
내가이 코드를 수행했지만 문제가 항상 반환 soundID입니다 0 – Rani
업데이트 된 답변 확인 – Maulik
http : //www.edumo를 참조 할 수 있습니다. bile.org/iphone/ipad-development/playing-audio-file-in-ipad/ – Maulik