欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-20 12:24:40
本文实例为大家分享了ios音乐播放器制作的具体代码,供大家参考,具体内容如下 效果图 目录结构 代码 // // viewcontroller.m...

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

效果图

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

目录结构

运用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

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

上一篇:

下一篇: