运用iOS教你轻松制作音乐播放器

时间:2022-02-19 23:05:42

本文实例为大家分享了ios音乐播放器制作的具体代码,供大家参考,具体内容如下

效果图

运用iOS教你轻松制作音乐播放器

目录结构

运用iOS教你轻松制作音乐播放器

代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// 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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。