要給工程中添加音頻,起首要導入音頻的框架 AVFoundation.framework
然後新建一個類繼續於UIViewController, 我這裡就叫FirstVC.
起首在 AppDelegate.m中初始化根視圖
#import "AppDelegate.h"
#import "FirstVC.h"
@implementation AppDelegate
- (void)dealloc
{
[_Window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.Window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
FirstVC *firstVC = [[FirstVC alloc] init];
self.window.rootViewController = firstVC;
[firstVC release];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
然後在FirstVC.h中導入AVFoundation框架
#import <UIKit/UIKit.h>
//要想應用封裝好的音頻類,導入框,導入類頭文件,缺一弗成;
#import <AVFoundation/AVFoundation.h>
@interface FirstVC : UIViewController<AVAudioPlayerDelegate>
{
AVAudioPlayer *avAudioPlayer; //播放器player
UIProgressView *progressV; //播放進度
UISlider *volumeSlider; //聲響掌握
NSTimer *timer; //監控音頻播放進度
}
@end
然後在FirstVC.m裡的viewDidLoad辦法裡填寫代碼 你須要導入一個音頻才可以播放 像添加圖片一樣,直接拖到工程裡便可以了
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//初始化三個button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(100, 100, 60, 40)];
[button setTitle:@"Play" forState:UIControlStateNormal];
[button addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setFrame:CGRectMake(100, 150, 60, 40)];
[button1 setTitle:@"pause" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(pause) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setFrame:CGRectMake(100, 200, 60, 40)];
[button2 setTitle:@"stop" forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
//從budle途徑下讀取音頻文件 輕音樂 - 薩克斯回家 這個文件名是你的歌曲名字,mp3是你的音頻格局
NSString *string = [[NSBundle mainBundle] pathForResource:@"輕音樂 - 薩克斯回家" ofType:@"mp3"];
//把音頻文件轉換成url格局
NSURL *url = [NSURL fileURLWithPath:string];
//初始化音頻類 而且添加播放文件
avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
//設置署理
avAudioPlayer.delegate = self;
//設置初始音量年夜小
// avAudioPlayer.volume = 1;
//設置音樂播放次數 -1為一向輪回
avAudioPlayer.numberOfLoops = -1;
//預播放
[avAudioPlayer prepareToPlay];
//初始化一個播放進度條
progressV = [[UIProgressView alloc] initWithFrame:CGRectMake(20, 50, 200, 20)];
[self.view addSubview:progressV];
[progressV release];
//用NSTimer來監控音頻播放進度
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self
selector:@selector(playProgress)
userInfo:nil repeats:YES];
//初始化音量掌握
volumeSlider = [[UISlider alloc] initWithFrame:CGRectMake(20, 70, 200, 20)];
[volumeSlider addTarget:self action:@selector(volumeChange)
forControlEvents:UIControlEventValueChanged];
//設置最小音量
volumeSlider.minimumValue = 0.0f;
//設置最年夜音量
volumeSlider.maximumValue = 10.0f;
//初始化音量為若干
volumeSlider.value = 5.0f;
[self.view addSubview:volumeSlider];
[volumeSlider release];
//聲響開關控件(靜音)
UISwitch *swith = [[UISwitch alloc] initWithFrame:CGRectMake(100, 20, 60, 40)];
[swith addTarget:self action:@selector(onOrOff:) forControlEvents:UIControlEventValueChanged];
//默許狀況為翻開
swith.on = YES;
[self.view addSubview:swith];
[swith release];
}
響應的自界說辦法代碼以下
//播放
- (void)play
{
[avAudioPlayer play];
}
//暫停
- (void)pause
{
[avAudioPlayer pause];
}
//停滯
- (void)stop
{
avAudioPlayer.currentTime = 0; //以後播放時光設置為0
[avAudioPlayer stop];
}
//播放進度條
- (void)playProgress
{
//經由過程音頻播放時長的百分比,給progressview停止賦值;
progressV.progress = avAudioPlayer.currentTime/avAudioPlayer.duration;
}
//聲響開關(能否靜音)
- (void)onOrOff:(UISwitch *)sender
{
avAudioPlayer.volume = sender.on;
}
//播放音量掌握
- (void)volumeChange
{
avAudioPlayer.volume = volumeSlider.value;
}
//播放完成時挪用的辦法 (署理裡的辦法),須要設置署理才可以挪用
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[timer invalidate]; //NSTimer暫停 invalidate 使...有效;
}
最初別忘了釋放內存
- (void)dealloc
{
[avAudioPlayer release];
[progressV release];
[volumeSlider release];
[timer release];
[super dealloc];
}
固然,你也能夠再界說一個UISlider來掌握播放進度.
最初運轉起來的就是這個模樣
假如想要播放收集音樂,那末遺憾的是IOS中的AVAudioPlayer不支撐邊下邊播,所以只能下載到當地再播放。
辦法:
NSString *urlStr = @"http://…………xxx.mp3";
NSURL *url = [[NSURL alloc]initWithString:urlStr];
NSData * audioData = [NSData dataWithContentsOfURL:url];
//將數據保留到當地指定地位
NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@.mp3", docDirPath , @"temp"];
[audioData writeToFile:filePath atomically:YES];
//播放當地音樂
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[player play];
留意代碼中的player是類的公有變量,由於在ARC形式下假如界說部分變量,出了感化域後對象會被燒毀。這個成績也是糾結了很久才弄明確。拜見鏈接
固然也能夠不保留文件,只是將player的結構辦法改成用data實例化便可。
總結:
上面說一下我碰到的各類成績:
1、因為添加了All Exception斷點,招致每次都在初始化的時刻主動斷到,由於不是OC異常,又無異常信息顯示。這時候應當編纂斷點,將其類型由All改成OC。這個異常其實不會招致法式的瓦解,所以可以不作處置。
2、網上有的說不克不及播放,處理計劃是應當在 AppDelegate.m文件中的
application didFinishLaunchingWithOptions 添加如許一句代碼:
[[AVAudIOSession sharedInstance]setCategory:AVAudIOSessionCategoryPlaybackerror:nil];
【詳解iOS App中挪用AVAudioPlayer播放音頻文件的用法】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!