码迷,mamicode.com
首页 > 其他好文 > 详细

音乐播放

时间:2016-04-30 20:59:52      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property (nonatomic, strong) AVAudioPlayer *player;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 音乐: 长的声音  歌曲  戏曲
    
    // 1.1获取资源地址
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"童话.mp3" withExtension:nil];
    
    NSError *error = nil;
    
    // 1.创建音乐播放的对象
    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    
    if (error) {
        NSLog(@"创建音乐播放的对象出错:%@",error);
        return;
    }
    self.player = player;
    // 2.准备播放
    [player prepareToPlay];
  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)play:(id)sender {
    // 3.播放
    [self.player play];
}
- (IBAction)pause:(id)sender {
    // 暂停
    [self.player pause];
}
- (IBAction)stop:(id)sender {
    // 停止
    [self.player stop];
}

  

音乐播放

标签:

原文地址:http://www.cnblogs.com/yuwei0911/p/5449273.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!