ViewController.h
//定义一个按钮button
@property(strong,nonatomic)UIButton *button;
ViewController.m
#import "ViewController.h"
//导入头文件
#import
@interface ViewController ()
//定义一个player的属性
@property (nonatomic,strong)AVAudioPlayer *player;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//获取MP3文件的全路径
NSString *musicfiledpath=[[NSBundle mainBundle] pathForResource:@"刘德华 - 冰雨" ofType:@"mp3"];
//也是获取路径
NSURL *musicURL=[[NSURL alloc] initFileURLWithPath:musicfiledpath];
//创建名叫theplay的对象
AVAudioPlayer *thePlay=[[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
//设置button的位置
self.button=[[UIButton alloc] initWithFrame:CGRectMake(150, 150, 100, 50)];