網頁

2012年6月14日 星期四

AVAudioPlayer

add AVFoundation.framework

#import <AVFoundation/AVFoundation.h>


AVAudioPlayer Notification issue: delegate- audioPlayerDidFinishPlaying:successfully
 
@interface Conversation : UIViewController 
{
    
    AVAudioPlayer  *avPlayer;
}
@property (nonatomic, retain) AVAudioPlayer *avPlayer;
 
@synthesize avPlayer;


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
  
    [self playSound: indexPath];
  
}
#pragma mark - Custom methods

- (void) playSound: (NSIndexPath *)indexPath
{
    NSUInteger section = indexPath.section;
    

    each =  [mainTableDataQuestion objectAtIndex:indexPath.row];            
 
    
    NSString *fileName = [each objectForKey:@"soundID"];
    NSLog(@"fileName=%@",fileName);
    
    NSURL *url = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"] isDirectory:NO];
    
    NSError* error = nil;
    
    avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    avPlayer.delegate  = self;
    if (!url || error) {
        
    }
    avPlayer.volume = 1;
    avPlayer.numberOfLoops = 0;// -1 = repeat forever
    [avPlayer prepareToPlay];
    [avPlayer play];
    


    

}
#pragma mark -
#pragma mark AVAudioPlayer delegate

- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *)aPlayer successfully: (BOOL)flag
{
    if (flag == YES) {
        
         NSLog(@"===audioPlayerDidFinishPlaying===");
        
        //do something
        [mainTableView reloadData];
    } 
   
}

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。