【day09-1-AVAudioPlayer】:播放音乐案例
实现多媒体需要准备以下两点:
需要引入一个框架AVFoundation.framework
然后引入#import <AVFoundation/AVFoundation.h>
代码如下:
@interface MXViewController () @property(nonatomic,strong) AVAudioPlayer *player; @end @implementation MXViewController - (void)viewDidLoad { [super viewDidLoad]; // 此对象可以表示两种路径1.本地路径file2.网络路径url NSURL *url = [NSURL fileURLWithPath:@".."]; self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:Nil]; [self.player play];// 播放 // [self.player pause];// 暂停 // [self.player stop]; // self.player.currentTime;当前时间 // self.player.duration;总时间 }
【day0902_playMusic】:播放界面的操作
-(void)playMusic{ if (self.currentMusicIndex == self.musicArray.count) { self.currentMusicIndex = 0; // 从头播放 }else if (self.currentMusicIndex == -1){ self.currentMusicIndex = self.musicArray.count - 1; // 播放最后一个 } self.title = [[[self.musicArray objectAtIndex:self.currentMusicIndex] lastPathComponent] componentsSeparatedByString:@"."][0]; self.artWorkImageView.image = [Utils getArtworkByPath:[self.musicArray objectAtIndex:self.currentMusicIndex]]; // 获取专辑图片 NSURL *url = [NSURL fileURLWithPath:[self.musicArray objectAtIndex:self.currentMusicIndex]]; // 获取音乐的url // NSLog(@"即将:%@ 正在:%@",url,self.app.player.url); if (![[url description] isEqualToString:[self.app.player.url description]]) { // 如果传进来的url根当前的url是一样的就不创建新的player self.app.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:Nil]; } self.app.player.delegate = self; [self.app.player play]; // 播放 self.durationTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d",(int)self.app.player.duration/60,(int)self.app.player.duration%60]; // 播放时间 self.playSlider.maximumValue = self.app.player.duration; self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updataUI) userInfo:Nil repeats:YES]; // 歌曲时间 } -(void)updataUI{ self.playTimeLabel.text = [NSString stringWithFormat:@"%02d:%02d",(int)self.app.player.currentTime/60,(int)self.app.player.currentTime%60]; self.playSlider.value = self.app.player.currentTime; } - (IBAction)singerButton:(UIButton *)sender { switch (sender.tag) { case 0: // self.app.player.currentTime -= 5; // 快退 self.currentMusicIndex--;// 上一首 [self playMusic]; break; case 1: if (self.app.player.isPlaying) { [self.app.player pause]; [sender setTitle:@"播放" forState:UIControlStateNormal]; }else{ [self.app.player play]; [sender setTitle:@"暂停" forState:UIControlStateNormal]; } break; case 2: // self.app.player.currentTime += 5; // 快进 self.currentMusicIndex++; [self playMusic]; break; } } - (IBAction)changeValueSlider:(UISlider *)sender { self.app.player.currentTime = sender.value; } // 播放完时调用 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{ // NSLog(@"播放完成"); self.currentMusicIndex++; // 下一首 [self playMusic]; } // 界面消失之后调用,注意不要用viewDidAppear这个是界面显示之后 -(void)viewDidDisappear:(BOOL)animated{ [super viewDidAppear:animated]; [self.timer invalidate]; // 停掉计时器,此时页面会被干掉 }
注意要在AppDelegata中声明audioPlayer
@property(nonatomic,strong) AVAudioPlayer *player; // 在此声明多媒体播放对象,防止控制页面返回时player被干掉
arc只会release掉自己页面的对象
strong把原来的值释放掉 把新新传来的值retain赋值
assign,weak把传进来的值赋值给属性
原文:http://www.cnblogs.com/yangmx/p/3561277.html