《30天精通iPhone手机编程》-Day21-音乐选播器

这一章加入了UIProgressView(进度视图)控件

- (void)viewDidLoad {
    [super viewDidLoad];
	//数组存放所有播放声音的名称对象
	NSArray *array=[[NSArray alloc] initWithObjects:@"第一套广播体操",@"第二套广播体操",@"第三套广播体操",@"眼保健操",@"第八套广播体操",@"2009广播体操",nil];
	//定以播放声音的名称数组对象
	self.musicColumn = array;
	[array release];
}
- (IBAction)play{ 
    //获取在声音选择器中当前用户选择的行数项目
	NSInteger row = [musicPicker selectedRowInComponent:0];
	//当选择不同行时
	if(selectedMusic != [musicColumn objectAtIndex:row]){
		//定义用户选择的声音为用户选择的行数项目
		selectedMusic = [musicColumn objectAtIndex:row];
		//获取用户选择声音所播放的声音文件的路径名  
		NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: selectedMusic ofType: @"mp3"]; 
		//转换字符类型变量的路径为URL链接路径  
	    NSURL *fileURL = [[[NSURL alloc] initFileURLWithPath: soundFilePath] autorelease];
		//初始化播放器,定义播放连接内容  
	    self.player = [[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL error: nil] autorelease];
	    [self.player prepareToPlay]; 
		[self.player setDelegate:self];
        //定义声音播放进程文字标签为时间文字0:00
	    self.currentPositionLabel.text = @"0:00"; 
		//player.duration为声音播放器的声音长度属性值
	    self.durationLabel.text = [self timeToString:self.player.duration];
	    [self updateDisplay]; 
	    [NSTimer scheduledTimerWithTimeInterval:.1                         
					     target:self                     
					   selector:@selector(updateDisplay) 
					   userInfo:nil 
					    repeats:YES];	
	}
	[self.player play];
}
//建立文本格式转换方法,转换播放器中标签的显示格式
-(NSString*)timeToString:(int)time{
    return [NSString stringWithFormat:@"%02d:%02d",time/60,time%60];
}
//显示更新方法,更新各个控件的进程
- (void)updateDisplay {
	//定以播放声音的进度视图的进度属性值
	self.currentPositionProgress.progress = self.player.currentTime / self.player.duration;
	//定以播放声音的进度文字标签
	self.currentPositionLabel.text =  [self timeToString:self.player.currentTime];
}
- (IBAction)pause{
	[self.player stop];
}
//滑块调整播放音量
- (IBAction)setVolume {		
	self.player.volume = self.volumeSlider.value;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
	return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
	return [musicColumn count];
}
//定义选取器的内容,显示选取器视图中的行与列
-(UIView *)pickerView:(UIPickerView *)pickerView
		  titleForRow:(NSInteger)row
		 forComponent:(NSInteger)component
{
	return [musicColumn objectAtIndex:row];
}
《30天精通iPhone手机编程》-Day21-音乐选播器_第1张图片

你可能感兴趣的:(编程,url,iPhone,音乐,手机,UIView)