AVAudioPlayer播放音乐文件及读取ipod库中的音乐文件
IOS学习:AVAudioPlayer播放音乐文件及读取ipod库中的音乐文件
在德江等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供做网站、成都做网站 网站设计制作定制网站开发,公司网站建设,企业网站建设,品牌网站设计,网络营销推广,外贸营销网站建设,德江网站建设费用合理。
首先要导入AVFoundation框架及
下面是代码,代码中都有注释:
[cpp] view plaincopy
//
// RootViewController.h
// SoundDemo
//
// Created by on 13-6-21.
// Copyright (c) 2013年 DoubleMan. All rights reserved.
//
#import
#import
#import
@interface RootViewController : UIViewController
{
AVAudioPlayer *player;
}
@property (nonatomic, retain) AVAudioPlayer *player;
@property (nonatomic, retain) UISlider *slider;
@property (nonatomic, retain) NSTimer *timer;
@end
[cpp] view plaincopy
//
// RootViewController.m
// SoundDemo
//
// Created by on 13-6-21.
// Copyright (c) 2013年 DoubleMan. All rights reserved.
//
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
@synthesize player;
@synthesize slider;
@synthesize timer;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *musicPlay = [UIButton buttonWithType:UIButtonTypeRoundedRect];
musicPlay.frame = CGRectMake(10, 10, 90, 35);
[musicPlay setTitle:@"Play" forState:UIControlStateNormal];
[musicPlay addTarget:self action:@selector(playMusic) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:musicPlay];
UIButton *pause = [UIButton buttonWithType:UIButtonTypeRoundedRect];
pause.frame = CGRectMake(115, 10, 90, 35);
[pause setTitle:@"Pause" forState:UIControlStateNormal];
[pause addTarget:self action:@selector(pause) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pause];
UIButton *stop = [UIButton buttonWithType:UIButtonTypeRoundedRect];
stop.frame = CGRectMake(220, 10, 90, 35);
[stop setTitle:@"stop" forState:UIControlStateNormal];
[stop addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:stop];
slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 65, 300, 20)];
[slider addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider];
//
NSString *path = [[NSBundle mainBundle] pathForResource:@"找一个相爱的理由-晨熙-艾歌" ofType:@"wav"];
NSURL *url = [NSURL fileURLWithPath:path];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
// 设置循环次数,-1为一直循环
player.numberOfLoops = -1;
// 准备播放
[player prepareToPlay];
// 设置播放音量
player.volume = 50;
// 当前播放位置,即从currentTime处开始播放,相关于android里面的seekTo方法
player.currentTime = 15;
// 设置代理
player.delegate = self;
int dur = player.duration;
slider.maximumValue = dur;
// 一秒一次更新播放进度
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];
// 从ipod库中读出音乐文件
// MPMediaQuery *everything = [[MPMediaQuery alloc] init];
// // 读取条件
// MPMediaPropertyPredicate *albumNamePredicate =
// [MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInt:MPMediaTypeMusic ] forProperty: MPMediaItemPropertyMediaType];
// [everything addFilterPredicate:albumNamePredicate];
//
// NSLog(@"Logging items from a generic query...");
// NSArray *itemsFromGenericQuery = [everything items];
// for (MPMediaItem *song in itemsFromGenericQuery) {
// NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
// NSLog (@"%@", songTitle);
// }
//
// [everything release];
}
// 更新播放进度
- (void)updateSlider {
slider.value = player.currentTime;
}
// 进度滑块变化时,跳转到进度播放
- (void)sliderValueChange:(UISlider *)mSlider {
player.currentTime = mSlider.value;
NSLog(@"value: %.0f", mSlider.value);
}
// 停止
- (void)stop {
player.currentTime = 0;
[player stop];
}
// 暂停
- (void)pause {
[player pause];
NSLog(@"pause");
}
// 开始播放
- (void)playMusic {
NSLog(@"start play");
[player play];
}
#pragma mark - AVAudioPlayerDelegate
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
// 播放完成时调用 只有当播放结束时才会调用,循环播放时不会调
[timer invalidate];
NSLog(@"audioPlayerDidFinishPlaying");
}
/* if an error occurs while decoding it will be reported to the delegate. */
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error {
// 解码出错时调用
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc
{
[player stop];
[player release];
[slider release];
[timer release];
[super dealloc];
}
@end
上一篇IOS学习:用UIWindow自定义AlertView(最基本代码)
下一篇IOS开发学习:MKMapView自定义CalloutView
顶
2
踩
分享名称:AVAudioPlayer播放音乐文件及读取ipod库中的音乐文件
文章起源:http://cdiso.cn/article/psgspj.html