溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

運(yùn)用iOS教你輕松制作音樂播放器

發(fā)布時(shí)間:2020-10-13 11:04:12 來源:腳本之家 閱讀:200 作者:絢麗星空 欄目:移動開發(fā)

本文實(shí)例為大家分享了iOS音樂播放器制作的具體代碼,供大家參考,具體內(nèi)容如下

效果圖

運(yùn)用iOS教你輕松制作音樂播放器

目錄結(jié)構(gòu)

運(yùn)用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];
// 背景圖片和設(shè)備屏幕一樣大
 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];

// 創(chuàng)建播放器
// 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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI