千家信息网

AVAudioPlayer播放音乐文件及读取ipod库中的音乐文件

发表于:2025-12-01 作者:千家信息网编辑
千家信息网最后更新 2025年12月01日,IOS学习:AVAudioPlayer播放音乐文件及读取ipod库中的音乐文件首先要导入AVFoundation框架及下面是代码,代码中都有注释:[cpp] view plaincopy//// Ro
千家信息网最后更新 2025年12月01日AVAudioPlayer播放音乐文件及读取ipod库中的音乐文件



IOS学习:AVAudioPlayer播放音乐文件及读取ipod库中的音乐文件


首先要导入AVFoundation框架及



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


[cpp] view plaincopy

  1. //

  2. // RootViewController.h

  3. // SoundDemo

  4. //

  5. // Created by on 13-6-21.

  6. // Copyright (c) 2013年 DoubleMan. All rights reserved.

  7. //

  8. #import

  9. #import

  10. #import

  11. @interface RootViewController : UIViewController

  12. {

  13. AVAudioPlayer *player;

  14. }

  15. @property (nonatomic, retain) AVAudioPlayer *player;

  16. @property (nonatomic, retain) UISlider *slider;

  17. @property (nonatomic, retain) NSTimer *timer;

  18. @end


[cpp] view plaincopy

  1. //

  2. // RootViewController.m

  3. // SoundDemo

  4. //

  5. // Created by on 13-6-21.

  6. // Copyright (c) 2013年 DoubleMan. All rights reserved.

  7. //

  8. #import "RootViewController.h"

  9. @interface RootViewController ()

  10. @end

  11. @implementation RootViewController

  12. @synthesize player;

  13. @synthesize slider;

  14. @synthesize timer;

  15. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

  16. {

  17. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

  18. if (self) {

  19. // Custom initialization

  20. }

  21. return self;

  22. }

  23. - (void)viewDidLoad

  24. {

  25. [super viewDidLoad];

  26. UIButton *musicPlay = [UIButton buttonWithType:UIButtonTypeRoundedRect];

  27. musicPlay.frame = CGRectMake(10, 10, 90, 35);

  28. [musicPlay setTitle:@"Play" forState:UIControlStateNormal];

  29. [musicPlay addTarget:self action:@selector(playMusic) forControlEvents:UIControlEventTouchUpInside];

  30. [self.view addSubview:musicPlay];

  31. UIButton *pause = [UIButton buttonWithType:UIButtonTypeRoundedRect];

  32. pause.frame = CGRectMake(115, 10, 90, 35);

  33. [pause setTitle:@"Pause" forState:UIControlStateNormal];

  34. [pause addTarget:self action:@selector(pause) forControlEvents:UIControlEventTouchUpInside];

  35. [self.view addSubview:pause];

  36. UIButton *stop = [UIButton buttonWithType:UIButtonTypeRoundedRect];

  37. stop.frame = CGRectMake(220, 10, 90, 35);

  38. [stop setTitle:@"stop" forState:UIControlStateNormal];

  39. [stop addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];

  40. [self.view addSubview:stop];

  41. slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 65, 300, 20)];

  42. [slider addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];

  43. [self.view addSubview:slider];

  44. //

  45. NSString *path = [[NSBundle mainBundle] pathForResource:@"找一个相爱的理由-晨熙-艾歌" ofType:@"wav"];

  46. NSURL *url = [NSURL fileURLWithPath:path];

  47. player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];

  48. // 设置循环次数,-1为一直循环

  49. player.numberOfLoops = -1;

  50. // 准备播放

  51. [player prepareToPlay];

  52. // 设置播放音量

  53. player.volume = 50;

  54. // 当前播放位置,即从currentTime处开始播放,相关于android里面的seekTo方法

  55. player.currentTime = 15;

  56. // 设置代理

  57. player.delegate = self;

  58. int dur = player.duration;

  59. slider.maximumValue = dur;

  60. // 一秒一次更新播放进度

  61. timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];

  62. // 从ipod库中读出音乐文件

  63. // MPMediaQuery *everything = [[MPMediaQuery alloc] init];

  64. // // 读取条件

  65. // MPMediaPropertyPredicate *albumNamePredicate =

  66. // [MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInt:MPMediaTypeMusic ] forProperty: MPMediaItemPropertyMediaType];

  67. // [everything addFilterPredicate:albumNamePredicate];

  68. //

  69. // NSLog(@"Logging items from a generic query...");

  70. // NSArray *itemsFromGenericQuery = [everything items];

  71. // for (MPMediaItem *song in itemsFromGenericQuery) {

  72. // NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];

  73. // NSLog (@"%@", songTitle);

  74. // }

  75. //

  76. // [everything release];

  77. }

  78. // 更新播放进度

  79. - (void)updateSlider {

  80. slider.value = player.currentTime;

  81. }

  82. // 进度滑块变化时,跳转到进度播放

  83. - (void)sliderValueChange:(UISlider *)mSlider {

  84. player.currentTime = mSlider.value;

  85. NSLog(@"value: %.0f", mSlider.value);

  86. }

  87. // 停止

  88. - (void)stop {

  89. player.currentTime = 0;

  90. [player stop];

  91. }

  92. // 暂停

  93. - (void)pause {

  94. [player pause];

  95. NSLog(@"pause");

  96. }

  97. // 开始播放

  98. - (void)playMusic {

  99. NSLog(@"start play");

  100. [player play];

  101. }

  102. #pragma mark - AVAudioPlayerDelegate

  103. - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {

  104. // 播放完成时调用 只有当播放结束时才会调用,循环播放时不会调

  105. [timer invalidate];

  106. NSLog(@"audioPlayerDidFinishPlaying");

  107. }

  108. /* if an error occurs while decoding it will be reported to the delegate. */

  109. - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error {

  110. // 解码出错时调用

  111. }

  112. - (void)didReceiveMemoryWarning

  113. {

  114. [super didReceiveMemoryWarning];

  115. // Dispose of any resources that can be recreated.

  116. }

  117. - (void)dealloc

  118. {

  119. [player stop];

  120. [player release];

  121. [slider release];

  122. [timer release];

  123. [super dealloc];

  124. }

  125. @end




  • 上一篇IOS学习:用UIWindow自定义AlertView(最基本代码)

  • 下一篇IOS开发学习:MKMapView自定义CalloutView

  • 2


进度 文件 音乐 代码 学习 循环 时调 更新 位置 只有 方法 条件 框架 次数 注释 理由 音量 上一 代理 准备 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 高校网络安全岗 湖南宁可互联网科技公司 无锡新区软件开发招聘信息 育碧服务器失去链接图片 海南新概念互联网科技有限公司 方巨网络技术有限公司 2020年国网络安全 网络安全领域意识形态调研 国土空间总体规划数据库工作思路 华为网络技术大赛时间 页面反向显示数据库数据 天津通讯软件开发设施厂家现货 深圳开创网络技术有限公司 vm12虚拟机架设服务器 户型图软件开发 宿州出入库软件开发平台 织梦系统数据库连接不了 北京交通大学数据库考试题 美国网络安全人才研究院 软件开发成本如何分摊 编程属不属于软件开发 计算机网络技术于英语有关吗 医院网络安全检查整改措施 通信网络安全问题及其解决 应用软件开发方法 无锡新区软件开发招聘信息 深圳以软件开发为主的公司 网络安全攻防学什么 软件开发与应用属于化学吗 软件开发ba和bs的区别
0