首先要导入AVFoundation框架及

#import <AVFoundation/AVFoundation.h>头文件

注意:要在真机上调试

下面是ipad上的调试效果

下面是代码,代码中都有注释:

//
// RootViewController.h
// SoundDemo
//
// Created by on 13-6-21.
// Copyright (c) 2013年 DoubleMan. All rights reserved.
// #import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h> @interface RootViewController : UIViewController <AVAudioPlayerDelegate>
{
AVAudioPlayer *player;
} @property (nonatomic, retain) AVAudioPlayer *player;
@property (nonatomic, retain) UISlider *slider;
@property (nonatomic, retain) NSTimer *timer; @end
//
// 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学习:AVAudioPlayer播放音乐文件及读取ipod库中的音乐文件的更多相关文章

  1. iOS开发系列--扩展--播放音乐库中的音乐

    众所周知音乐是iOS的重要组成播放,无论是iPod.iTouch.iPhone还是iPad都可以在iTunes购买音乐或添加本地音乐到音乐 库中同步到你的iOS设备.在MediaPlayer.fram ...

  2. java通过文件路径读取该路径下的所有文件并将其放入list中

    java通过文件路径读取该路径下的所有文件并将其放入list中   java中可以通过递归的方式获取指定路径下的所有文件并将其放入List集合中.假设指定路径为path,目标集合为fileList,遍 ...

  3. Jar中的Java程序如何读取Jar包中的资源文件

    Jar中的Java程序如何读取Jar包中的资源文件 比如项目的组织结构如下(以idea中的项目为例): |-ProjectName |-.idea/  //这个目录是idea中项目的属性文件夹 |-s ...

  4. 读取web工程中.properties资源文件的模板代码

    读取web工程中.properties资源文件的模板代码 // 读取web工程中.properties资源文件的模板代码 private void test2() throws IOException ...

  5. SharePoint 2010遍历文档库中所有的文件,文件夹

    转:http://hi.baidu.com/sygwin/item/f99600849d51a12b110ef3eb 创建一个可视WebPart,并拖放一个label控件到ascx文件上,用于显示结果 ...

  6. 获取iPod library中的媒体文件

    [获取iPod library中的媒体文件] The Media Player framework provides facilities for playing movie, music, audi ...

  7. 无法打开虚拟机“master”(D:\文档\Virtual Machines\master\master.vmx):未找到文件。是否从库中移除“master”?

    今天打开虚拟机的时候,出现了这样的弹窗提示: 无法打开虚拟机"master"(D:\文档\Virtual Machines\master\master.vmx):未找到文件.是否从 ...

  8. IOS使用AVAudioPlayer播放mp3歌曲文件并监听来电打断

    本实例实现了AVAudioPlayer播放mp3歌曲文件,实现了播放.暂停.继续操作,音乐音量控制.播放进度显示,同时监听来电打断事件 一.控件初始化 - (void)viewDidLoad { [s ...

  9. [转]iOS学习笔记(2)--Xcode6.1创建仅xib文件无storyboard的hello world应用

    转载地址:http://www.mamicode.com/info-detail-514151.html 由于Xcode6之后,默认创建storyboard而非xib文件,而作为初学,了解xib的加载 ...

随机推荐

  1. 【linux】具体芯片MACHINE_START处理

    欢迎转载,转载时需保留作者信息,谢谢. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http:// ...

  2. Java返回类型泛型的用法小结

    Java返回类型泛型的用法小结 版权声明:本文为博主原创文章,未经博主允许不得转载. 关于Java泛型的基本用法就不多说了,主要是一个编译期的检查,也避免了我们代码中的强制转换,比较经典的用法有泛型D ...

  3. HttpURLConnection中使用代理(Proxy)及其验证(Authentication)

    HttpURLConnection中使用代理(Proxy)及其验证(Authentication) 使用Java的HttpURLConnection类可以实现HttpClient的功能,而不需要依赖任 ...

  4. WSGI详解

    WSGI接口 了解了HTTP协议和HTML文档,我们其实就明白了一个Web应用的本质就是: 浏览器发送一个HTTP请求: 服务器收到请求,生成一个HTML文档: 服务器把HTML文档作为HTTP响应的 ...

  5. (step 8.2.8)hdu 1079(Calendar Game)

    题目大意是: 两个家伙在区域赛前夕闲的无聊,然后玩一种无限纠结的游戏,随即给定一个日期,每次只能移动day OR month.......... 而且如果下一个月没有当前day的话, 你就不能移动mo ...

  6. Swift - 初始化方法返回nil表示初始化失败

    自Swift1.1开始,初始化init方法便有返回nil的能力.我们可以在init声明的时候在其后面加上一个 ? 或者 ! 来表示初始化失败时可能返回nil. 比如,给Int类添加一个将字符串初始化成 ...

  7. No enclosing instance of type Hello is accessible

    1.static 关键字 修饰的成员被所有对象共享(包括成员变量和方法). 修饰的成员优先于对象存在. 存储于方法区(共享数据区)的静态区中. 静态方法只能访问静态成员. 静态方法中不可以使用this ...

  8. 【Tips】Endnote导入IEEE Xplore文献方法《转载》

    1. 在IEEE XPlore中点击“Download Citation”: 2. 选中“Citation & Abstract”和“EndNote,Procite,RefMan”两个选项: ...

  9. Ubuntu下安装ADT(图文教程)

    个人感觉Ubuntu下安装ADT跟在Windows大同小异 一.装上JDK和Eclipse 如果还没有装上的,请参考我前面的博文: http://blog.csdn.net/ljphhj/articl ...

  10. excel使用cube