千家信息网

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

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


0