AudioPlayer


//在编码之前导入AVAudio包

//注意:AVAudioPlayer 不能释放autorelease


#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>


@interfaceViewController ()


@end


@implementation ViewController


- (void)viewDidLoad

{

   [superviewDidLoad];

UIButton *AudioButton = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

   [AudioButton setFrame:CGRectMake(10, 200, 300, 60)];

   [AudioButton setTitle:@"Play"forState:UIControlStateNormal];

   [AudioButton addTarget:selfaction:@selector(PlayAudio) forControlEvents:UIControlEventTouchUpInside];

   [self.viewaddSubview:AudioButton];

}


-(void)PlayAudio{

NSString *MusicPath = [[NSBundlemainBundle]pathForResource:@"deng"ofType:@"mp3"];


NSURL *fileurl = [[[NSURLalloc]initFileURLWithPath:MusicPath]autorelease];

//must be remember ,can't have been autorelease

AVAudioPlayer *playPath = [[AVAudioPlayeralloc]initWithContentsOfURL:fileurl error:nil];


   [playPath play];


}


- (void)didReceiveMemoryWarning

{

   [superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}


@end


你可能感兴趣的:(audioPlayer)