iOS開發-音樂播放

來源:互聯網
上載者:User

標籤:

現在的各種App大行其道,其實常用也就是圍繞著吃喝玩樂基本的需求,視頻,音樂在智能手機出現之前更是必不可少的功能,每個手機都會有一個內建的音樂播放器,當然公眾也有自己的需求所以也就造就了各種音樂播放軟體,自己下午閑來無事簡單的寫了一個全部隨機播放音樂的Demo,iOS中有三種播放音訊方式AVAudioPlayer、音頻服務、音頻隊列。另外兩種暫時沒有用到,就簡單的練手了一下AVAudioPlayer,還是開始正題吧;

1.建立項目或者在原有項目重新弄一個頁面,先看頁面:

 

2.匯入幾首自己喜歡的歌曲:

3.匯入AVFoundation/AVFoundation.h,對四個按鈕進行事件操作,一個AVAudioPlayer只能對應一個URL,因此播放其他歌曲的時候需要情況一下;

定義兩個成員變數,並且初始化成員變數:

@interface MusicViewController ()@property (nonatomic,strong)AVAudioPlayer *player;@property (nonatomic,strong)NSArray *musicArr;@end

 viewDidLoad執行個體化數組:

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    [email protected][@"潮濕的心.mp3",@"愛拼才會贏.mp3",@"給我一個理由忘記.mp3"];    [self prepareMusic:self.musicArr[1]];}- (void)prepareMusic:(NSString *)path{    //1.音頻檔案的url路徑    NSURL *url=[[NSBundle mainBundle]URLForResource:path withExtension:Nil];        //2.執行個體化播放器    _player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];        //3.緩衝    [_player prepareToPlay];}

4.四個對應事件的代碼:

隨機:

- (IBAction)random:(id)sender {    [self prepareMusic:self.musicArr[arc4random()%3]];    [_player play];}

播放:

- (IBAction)play:(id)sender {    //播放    [_player play];}

暫停:

- (IBAction)pause:(id)sender {    //暫停    [_player pause];}

停止:

- (IBAction)stop:(id)sender {    //停止    [_player stop];}

5.設定迴圈次數,開始播放時間,設定音量

  //設定音量    [_player setVolume:0.6];    //設定當前播放事件    [_player setCurrentTime:60];    //設定迴圈次數    [_player setNumberOfLoops:2];

MusicViewController.m中的代碼:

////  MusicViewController.m//  MyPicture////  Created by keso on 15/1/17.//  Copyright (c) 2015年 keso. All rights reserved.//#import "MusicViewController.h"#import <AVFoundation/AVFoundation.h>@interface MusicViewController ()@property (nonatomic,strong)AVAudioPlayer *player;@property (nonatomic,strong)NSArray *musicArr;@end@implementation MusicViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.    [email protected][@"潮濕的心.mp3",@"愛拼才會贏.mp3",@"給我一個理由忘記.mp3"];    [self prepareMusic:self.musicArr[1]];}- (void)prepareMusic:(NSString *)path{    //1.音頻檔案的url路徑    NSURL *url=[[NSBundle mainBundle]URLForResource:path withExtension:Nil];        //2.執行個體化播放器    _player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];        //3.緩衝    [_player prepareToPlay];    //設定音量    [_player setVolume:0.6];    //設定當前播放事件    [_player setCurrentTime:60];    //設定迴圈次數    [_player setNumberOfLoops:2];}- (IBAction)random:(id)sender {    [self prepareMusic:self.musicArr[arc4random()%3]];    [_player play];}- (IBAction)play:(id)sender {    //播放    [_player play];}- (IBAction)stop:(id)sender {    //停止    [_player stop];}- (IBAction)pause:(id)sender {    //暫停    [_player pause];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    // Get the new view controller using [segue destinationViewController].    // Pass the selected object to the new view controller.}*/@end

其實需要設定還有很多,播放出現異常,或者被更進階別的系統任務打斷,可以通過設定相應的委託處理對應的的情形,Demo很小,iOS很多東西都是這樣,概念很多,調用的時候根本都不需要寫幾行代碼,iOS的模擬器播放的效果還是非常出色的~

 由於是播放音樂,無法類比效果,大概實驗一下,應該沒有什麼問題~

iOS開發-音樂播放

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.