iOS音頻功能開發(一)整理

來源:互聯網
上載者:User

iOS音頻功能開發(一)整理
要給工程中添加音頻,首先要匯入音訊架構AVFoundation.framework

然後建立一個類繼承於UIViewController, 我這裡就叫FirstVC.
首先在AppDelegate.m中初始化根視圖

 1 // 2 //  AppDelegate.m 3 //  YinPinShiPin 4 // 5 //  Created by chulijian on 14-11-8. 6 //  Copyright (c) 2014年 company. All rights reserved. 7 // 8  9 #import "AppDelegate.h"10 #import "FirstVC.h"11 @implementation AppDelegate12 13 18 19 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions20 {21     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];22     // Override point for customization after application launch.23     24     FirstVC *firstVC = [[FirstVC alloc] init];25     self.window.rootViewController = firstVC;27     28     self.window.backgroundColor = [UIColor whiteColor];29     [self.window makeKeyAndVisible];30     return YES;31 }

然後在FirstVC.h中匯入AVFoundation架構

 1 // 2 //  FirstVC.h 3 //  YinPinShiPin 4 // 5 //  Created by chulijian on 14-11-8.
6 // Copyright (c) 2014年 company. All rights reserved.
7 //
 8 //
 9 //
     #import 
 10
 11 //要想使用封裝好的音頻類,匯入框,匯入類標頭檔,缺一不可;
 12  #import 
 13
 14  @interface FirstVC : UIViewController
 15 {
 16     AVAudioPlayer *avAudioPlayer;  //播放器player17    
UIProgressView *progressV;      //播放進度18    
UISlider *volumeSlider;         //聲音控制19    
NSTimer *timer;                 //監控音頻播放進度20
 }
 21
 22 @end

然後在FirstVC.m裡的viewDidLoad方法裡填寫代碼 你需要匯入一個音頻才可以播放 像添加圖片一樣,直接拖到工程裡就可以了

 1 - (void)viewDidLoad 2 { 3     [super viewDidLoad]; 4     // Do any additional setup after loading the view. 5     //初始化三個button 6     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 7     [button setFrame:CGRectMake(100, 100, 60, 40)]; 8     [button setTitle:@"Play" forState:UIControlStateNormal]; 9     [button addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchUpInside];10     [self.view addSubview:button];11     12     UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];13     [button1 setFrame:CGRectMake(100, 150, 60, 40)];14     [button1 setTitle:@"pause" forState:UIControlStateNormal];15     [button1 addTarget:self action:@selector(pause) forControlEvents:UIControlEventTouchUpInside];16     [self.view addSubview:button1];17     18     UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];19     [button2 setFrame:CGRectMake(100, 200, 60, 40)];20     [button2 setTitle:@"stop" forState:UIControlStateNormal];21     [button2 addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];22     [self.view addSubview:button2];23     24     //從budle路徑下讀取音頻檔案  輕音樂 - 薩克斯回家 這個檔案名稱是你的歌曲名字,mp3是你的音頻格式25     NSString *string = [[NSBundle mainBundle] pathForResource:@"輕音樂 - 薩克斯回家" ofType:@"mp3"];26     //把音頻檔案轉換成url格式27     NSURL *url = [NSURL fileURLWithPath:string];28     //初始化音頻類 並且添加播放檔案29     avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];30     //設定代理31     avAudioPlayer.delegate = self;32     33     //設定初始音量大小34    // avAudioPlayer.volume = 1;35     36     //設定音樂播放次數  -1為一直迴圈37     avAudioPlayer.numberOfLoops = -1;38     39     //預播放40     [avAudioPlayer prepareToPlay];41     42     //初始化一個播放進度條43     progressV = [[UIProgressView alloc] initWithFrame:CGRectMake(20, 50, 200, 20)];44     [self.view addSubview:progressV];46     47     //用NSTimer來監控音頻播放進度48     timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self49                                                      selector:@selector(playProgress)50                                                      userInfo:nil repeats:YES];51     //初始化音量控制52     volumeSlider = [[UISlider alloc] initWithFrame:CGRectMake(20, 70, 200, 20)];53     [volumeSlider addTarget:self action:@selector(volumeChange)54                        forControlEvents:UIControlEventValueChanged];55     //設定最小音量56     volumeSlider.minimumValue = 0.0f;57     //設定最大音量58     volumeSlider.maximumValue = 10.0f;59     //初始化音量為多少60     volumeSlider.value = 5.0f;61     62     [self.view addSubview:volumeSlider];64 65     //聲音開關控制項(靜音)66     UISwitch *swith = [[UISwitch alloc] initWithFrame:CGRectMake(100, 20, 60, 40)];67     [swith addTarget:self action:@selector(onOrOff:) forControlEvents:UIControlEventValueChanged];68     //預設狀態為開啟69     swith.on = YES;70     [self.view addSubview:swith];72 }

相應的自訂方法代碼如下

 1     //播放 2 - (void)play 3 {   4     [avAudioPlayer play]; 5 } 6     //暫停 7 - (void)pause 8 { 9     [avAudioPlayer pause];10 }11     //停止12 - (void)stop13 {14     avAudioPlayer.currentTime = 0;  //當前播放時間設定為015     [avAudioPlayer stop];16 }17 //播放進度條18 - (void)playProgress19 {20     //通過音頻播放時間長度的百分比,給progressview進行賦值;21     progressV.progress = avAudioPlayer.currentTime/avAudioPlayer.duration;22 }23 //聲音開關(是否靜音)24 - (void)onOrOff:(UISwitch *)sender25 {26     avAudioPlayer.volume = sender.on;27 }28 29 //播放音量控制30 - (void)volumeChange31 {32    avAudioPlayer.volume = volumeSlider.value;33 }34 35 //播放完成時調用的方法  (代理裡的方法),需要設定代理才可以調用36 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag37 {38     [timer invalidate]; //NSTimer暫停   invalidate  使...無效;39 }

demo稍後會整理上傳

當然,你也可以再定義一個UISlider來控制播放進度.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.