效果图
目录结构
代码
#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;
UIToolbar *toolbar = [[UIToolbar alloc]init];
toolbar.frame = self.bgImageview.bounds;
toolbar.barStyle = UIBarStyleBlack;
toolbar.alpha = 0.9;
[self.bgImageview addSubview:toolbar];
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];
}
@end