资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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

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

在德江等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供做网站、成都做网站 网站设计制作定制网站开发,公司网站建设,企业网站建设,品牌网站设计,网络营销推广,外贸营销网站建设,德江网站建设费用合理。

首先要导入AVFoundation框架及

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

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

[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.   

  9. #import   

  10. #import   

  11. #import   

  12.   

  13. @interface RootViewController : UIViewController   

  14. {  

  15.     AVAudioPlayer *player;  

  16. }  

  17.   

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

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

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

  21.   

  22. @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.   

  9. #import "RootViewController.h"  

  10.   

  11. @interface RootViewController ()  

  12.   

  13. @end  

  14.   

  15. @implementation RootViewController  

  16.   

  17. @synthesize player;  

  18. @synthesize slider;  

  19. @synthesize timer;  

  20.   

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

  22. {  

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

  24.     if (self) {  

  25.         // Custom initialization  

  26.           

  27.           

  28.     }  

  29.     return self;  

  30. }  

  31.   

  32. - (void)viewDidLoad  

  33. {  

  34.     [super viewDidLoad];  

  35.       

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

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

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

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

  40.     [self.view addSubview:musicPlay];  

  41.       

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

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

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

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

  46.     [self.view addSubview:pause];  

  47.       

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

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

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

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

  52.     [self.view addSubview:stop];  

  53.       

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

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

  56.     [self.view addSubview:slider];  

  57.       

  58.     //   

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

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

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

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

  63.     player.numberOfLoops = -1;  

  64.     // 准备播放  

  65.     [player prepareToPlay];  

  66.     // 设置播放音量  

  67.     player.volume = 50;  

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

  69.     player.currentTime = 15;  

  70.     // 设置代理  

  71.     player.delegate = self;  

  72.     int dur = player.duration;  

  73.     slider.maximumValue = dur;  

  74.       

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

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

  77.       

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

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

  80. //    // 读取条件  

  81. //    MPMediaPropertyPredicate *albumNamePredicate =  

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

  83. //    [everything addFilterPredicate:albumNamePredicate];  

  84. //      

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

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

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

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

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

  90. //    }  

  91. //      

  92. //    [everything release];  

  93. }  

  94.   

  95. // 更新播放进度  

  96. - (void)updateSlider {  

  97.     slider.value = player.currentTime;  

  98. }  

  99.   

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

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

  102.     player.currentTime = mSlider.value;  

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

  104. }  

  105.   

  106. // 停止  

  107. - (void)stop {  

  108.     player.currentTime = 0;  

  109.     [player stop];  

  110. }  

  111.   

  112. // 暂停  

  113. - (void)pause {  

  114.     [player pause];  

  115.     NSLog(@"pause");  

  116. }  

  117.   

  118. // 开始播放  

  119. - (void)playMusic {  

  120.     NSLog(@"start play");  

  121.     [player play];  

  122. }  

  123.   

  124. #pragma mark - AVAudioPlayerDelegate  

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

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

  127.     [timer invalidate];  

  128.     NSLog(@"audioPlayerDidFinishPlaying");  

  129. }  

  130.   

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

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

  133.     // 解码出错时调用  

  134. }  

  135.   

  136. - (void)didReceiveMemoryWarning  

  137. {  

  138.     [super didReceiveMemoryWarning];  

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

  140. }  

  141.   

  142. - (void)dealloc  

  143. {  

  144.     [player stop];  

  145.     [player release];  

  146.     [slider release];  

  147.     [timer release];  

  148.     [super dealloc];  

  149. }  

  150.   

  151. @end  


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

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

  • 2


网站标题:AVAudioPlayer播放音乐文件及读取ipod库中的音乐文件
网站链接:http://cdkjz.cn/article/psgspj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220