2014-01-16 427 views
1

我试图对象didSelectedIndexUITableViewControllerIPhone,音频MP3或M4A等音频文件播放没有声音

音频文件与扩展m4a文件内播放声音的方法,但我也试图与MP3文件和结果不会改变。

下面我发布,我在TableViewController的方法已经实现了代码:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

    NSString *soundFilePath = [NSString stringWithFormat:@"%@/testaudio.m4a", [[NSBundle mainBundle] resourcePath]]; 
    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 
    NSLog(@"soundFileURL %@",soundFileURL); 
    NSError *error; 
    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error]; 

    if(!player){ 
     NSLog(@"ERRORE: %@",error); 
    } 
    [player setDelegate:self]; 
    [player prepareToPlay]; 
    [player play]; 

} 

我也试过直接在设备上,它仍然不能播放任何声音。

我还添加了类AVSession如下:

AVAudioSession *session = [AVAudioSession sharedInstance]; 
[session setActive:YES error:nil]; 

结果并没有改变,没有人知道是什么问题?

回答

1

试一下这个(测试):

中设置一个财产AVAudioPlayer对象的.m

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *soundFilePath; 

    if ((soundFilePath = [[NSBundle mainBundle] pathForResource:@"testaudio" ofType:@"m4a"])) 
    { 
     NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; 
     NSError *error; 
     self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error]; 

     if(self.player) 
     { 
      [self.player setDelegate:self]; 
      [self.player prepareToPlay]; 
      [self.player play]; 
     } 
     else 
     { 
      NSLog(@"ERRORE: %@", error); 
     } 
    } 
} 

你不”:

@property(strong, nonatomic) AVAudioPlayer *player; 

然后在您添加此你需要AVAudioSession

+1

很好...现在播放声音 – macuser