您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本文实例为大家分享了iOS音乐播放器制作的具体代码,供大家参考,具体内容如下
效果图
目录结构
代码
// // ViewController.m // 播放音乐 // // Created by xubh on 2017/3/24. // Copyright © 2017年 xubh. All rights reserved. // #import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView *bgImageview; @property (strong,nonatomic) AVPlayer *player; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 背景图片和设备屏幕一样大 CGRect r = [ UIScreen mainScreen ].applicationFrame; self.bgImageview.frame = r; // Do any additional setup after loading the view, typically from a nib. // 毛玻璃效果 UIToolbar *toolbar = [[UIToolbar alloc]init]; toolbar.frame = self.bgImageview.bounds; toolbar.barStyle = UIBarStyleBlack; toolbar.alpha = 0.9; [self.bgImageview addSubview:toolbar]; // 创建播放器 // NSString *path =[[NSBundle mainBundle]pathForResource:@"mysong1.mp3" ofType:nil ]; // NSURL *url =[NSURL fileURLWithPath:path]; NSURL *url = [[NSBundle mainBundle] URLForResource:@"夜的乐章.mp3" withExtension:nil]; AVPlayerItem *playerItem = [[AVPlayerItem alloc]initWithURL:url]; self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem]; } //开始播放和暂停播放 - (IBAction)startOrPauseMusic:(UIButton *)sender { switch (sender.tag) { case 3: [self.player play]; break; case 4: [self.player pause]; break; default: break; } } //切换歌曲 - (IBAction)changeMusic:(UIButton *)sender { NSString *musicName =nil; switch (sender.tag) { case 1: musicName = @"告白气球.mp3"; break; case 2: musicName = @"周杰伦串烧.mp3"; break; default: break; } NSURL *url = [[NSBundle mainBundle] URLForResource:musicName withExtension:nil]; AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url]; [self.player replaceCurrentItemWithPlayerItem:playerItem]; // 播放音乐 [self.player play]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。