温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-08-10 11:55:44 来源:网络 阅读:335 作者:刘轶荟 栏目:网络安全



 

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 <UIKit/UIKit.h>  

  10. #import <AVFoundation/AVFoundation.h>  

  11. #import <MediaPlayer/MediaPlayer.h>  

  12.   

  13. @interface RootViewController : UIViewController <AVAudioPlayerDelegate>  

  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


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI