in .h file
#import <AVFoundation/AVFoundation.h>
#import <UIKit/UIKit.h>#import <AVFoundation/AVFoundation.h>
@class AVAudioPlayer;
@interface AudioPlayer : UIViewController {
IBOutlet UIButton *playBtn;
AVAudioPlayer *audioPlayer;
}
@property (nonatomic, retain) IBOutlet UIButton *playButton;
@property (nonatomic, retain) AVAudioPlayer *audioPlayer;
-(IBAction)playBtnClicked;
@end
in .m file
- (void)viewDidLoad {
[super viewDidLoad];
// Get the file path to the song to play.
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"];
// Convert the file path to a URL.
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
//Initialize the AVAudioPlayer.
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
// Preloads the buffer and prepares the audio for playing.
[self.audioPlayer prepareToPlay];
[filePath release];
[fileURL release];
}
-(IBAction)playBtnClicked
{
audioPlayer.currentTime = 0;
[audioPlayer play];
}
No comments:
Post a Comment