2014-01-16 428 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