iOS開發-音樂播放


現在的各種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.
    self.musicArr=@[@"潮濕的心.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.
    self.musicArr=@[@"潮濕的心.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的模擬器播放的效果還是非常出色的~

 由於是播放音樂,無法模擬效果,大概試驗一下,應該沒有什么問題~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM