#import "ViewController.h"
#import
@interface ViewController ()
/**
* 背景图片
*/
@property (weak, nonatomic) IBOutlet UIImageView *bgImageView;
/** 播放器 */
@property (nonatomic, strong) AVPlayer *player;
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];
// 1.加毛玻璃
UIToolbar *toolbar = [[UIToolbar alloc] init];
// 2. 设置frame
toolbar.frame = self.bgImageView.bounds;
// 3. 设置样式和透明度
toolbar.barStyle = UIBarStyleBlack;
toolbar.alpha = 0.98;
// 4.加到背景图片上
[self.bgImageView addSubview:toolbar];
// 5.创建播放器
/*
NSString *path = [[NSBundle mainBundle] pathForResource:@"mySong1.mp3" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
*/
// 资源的URL地址
NSURL *url = [[NSBundle mainBundle] URLForResource:@"mySong1.mp3" withExtension:nil];
// 创建播放器曲目
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url];
// 创建播放器
self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
}
/**
* 播放/暂停
*
* @param button 按钮
*/
/**
* 切换歌曲
*
* @param button 按钮
*/
(IBAction)changeMusic:(UIButton *)button {
// 歌曲的名称
NSString *musicName = nil;
switch (button.tag) {
case 1:// 上一首
musicName = @"mySong2.mp3";
break;
case 2:// 下一首
musicName = @"mySong3.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];
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章