有一个 UITableView,我在每个单元格中放置了一个音频按钮,点击按钮时,有一个应该开始播放的进度条。当我点击播放按钮时,它应该显示停止按钮并且应该完成进度在进度条中。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
cell.backgroundColor=[UIColor blackColor];
[cell.textLabel setFont:[UIFont fontWithName:@"Trebuchet MS" size:14.0]];
cell.textLabel.textColor=[UIColor whiteColor];
UILabel *name=[[UILabel alloc]initWithFrame:CGRectMake(70, 10, 240, 30)];
name.text=[audiosArray objectAtIndex:indexPath.row];
name.tag=indexPath.row;
name.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:name];
playBtn=[[UIButton alloc]initWithFrame:CGRectMake(10, 5, 20, 20)];
[playBtn setBackgroundColor:[UIColor greenColor]];
playBtn.tag=indexPath.row;
[playBtn addTarget:self action:@selector(playBtnClk:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:playBtn];
stopBtn=[[UIButton alloc]initWithFrame:CGRectMake(10, 5, 20, 20)];
[stopBtn setBackgroundColor:[UIColor redColor]];
stopBtn.tag=indexPath.row;
[stopBtn addTarget:self action:@selector(stopAudio) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:stopBtn];
progressBar=[[UISlider alloc]initWithFrame:CGRectMake(40, 40, 200, 20)];
progressBar.maximumValue = audioPlayer.duration;
[progressBar addTarget:self action:@selector(sliderChanged:) forControlEvents:
UIControlEventValueChanged];
[cell.contentView addSubview:progressBar];
return cell;
}
-(IBAction)playBtnClk:(id)sender{
;
UIButton *selectedBtn=(UIButton*)sender;
[progressBar setHidden:NO];
[stopBtn setHidden:NO];
NSString *songStr=[NSString stringWithFormat:@"%@",[audiosArray objectAtIndex:selectedBtn.tag]];
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:[songStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
ofType:@"mp3"]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
if (error)
{
NSLog(@"Error in audioPlayer: %@",
[error localizedDescription]);
} else {
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
[self playAudio];
}
playbackTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self
selector:@selector(updateSlider) userInfo:nil repeats:YES];
// progressBar.maximumValue = audioPlayer.duration;
// Set the valueChanged target
// [progressBar addTarget:self action:@selector(sliderChanged:) forControlEvents:
// UIControlEventValueChanged];
}
-(void)playAudio
{
playbackTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateTime)
userInfo:nil
repeats:YES];
[audioPlayer play];
}
-(void)stopAudio
{
[playBtn setHidden:NO];
[playbackTimer invalidate];
[audioPlayer stop];
}
-(void)adjustVolume
{
if (audioPlayer != nil)
{
audioPlayer.volume = volumeControl.value;
}
}
-(void)updateTime
{
float minutes = floor(audioPlayer.currentTime/60);
float seconds = audioPlayer.currentTime - (minutes * 60);
float duration_minutes = floor(audioPlayer.duration/60);
float duration_seconds =
audioPlayer.duration - (duration_minutes * 60);
NSString *timeInfoString = [[NSString alloc]
initWithFormat:@"%0.0f.%0.0f / %0.0f.%0.0f",
minutes, seconds,
duration_mi`enter code here`nutes, duration_seconds]; timerLabel.text = timeInfoString; }
- (void)updateSlider {
// Update the slider about the music time
progressBar.value = audioPlayer.currentTime;
}
- (IBAction)sliderChanged:(UISlider *)sender {
// Fast skip the music when user scroll the UISlider
[audioPlayer stop];
[audioPlayer setCurrentTime:progressBar.value];
[audioPlayer prepareToPlay];
[audioPlayer play];
}