背景:我有一個UITableView,它最初加載了一些標準的UITableViewCells。當單擊其中一個時,表被重新加載,但被單擊的單元格變成了名爲'RecordingTableCell'的自定義子類,它具有2個UILabels,一個UIButton和一個UISlider。目前這工作正常。UISlider&UILabels自定義tableviewcell不更新與avaudioplayer直到第二次播放音頻
當按下按鈕時,會導致設置AVAudioPlayer並播放聲音文件。但是,這樣做的方法([self setupAudio]
)也獲得了2個UILabels和UISlider,以便它們可以隨音頻的進度而更新(我不確定是否以正確的方式得到它們)。
當按下首次播放按鈕,音頻播放,但滑塊或標籤不發生任何變化。但是,音頻結束後(即一旦調用audioPlayerDidFinishPlaying),如果再次按下播放按鈕,則所有內容都按原樣運行。
下面是代碼:(!遺憾的大塊)
...
-(void)setupAudio
{
//Get path
recordingPaths = [[NSUserDefaults standardUserDefaults] objectForKey:@"recordingPaths"];
recordedAudioURL = [NSURL URLWithString:recordingPaths[selectedRowIndex.row]];
NSError *audioError;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[self recordedAudioURL] error:&audioError];
player.delegate = self;
[player prepareToPlay];
recordingCurrentCell = (RecordingTableCell *)[self.tableView viewWithTag:REC_TABLE_CELL_TAG];
//The following are subviews of recordingTableCell
currentTimeLabel = [recordingCurrentCell currentTimeLabel];
totalTimeLabel = [recordingCurrentCell totalTimeLabel];
slider = [recordingCurrentCell progressSlider];
//Setup slider & labels
slider.maximumValue = [player duration];
totalTimeLabel.text = [NSString stringWithFormat:@"%.1f", player.duration];
slider.value = 0.0;
currentTimeLabel.text = [NSString stringWithFormat:@"%.1f", slider.value];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
selectedRowIndex = indexPath;
rowSelected=YES;
//Update the table so that the row with an indexPath equal to 'selectedRowIndex' becomes a custom subclass of UITableViewCell
[tableView beginUpdates];
[self.tableView reloadData];
[tableView endUpdates];
if(!player)
{
if(selectedRowIndex.row!=indexPath.row)
{
[[self player] stop];
player=nil;
//Change button on selected cell to the 'play' image
[actionButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
//If the NSTimer is still going, invalidate it
if(timer)
{
[timer invalidate];
timer = nil;
}
}
}
[self setupAudio];
}
//This is hooked up to the button on the custom tableviewcell
- (IBAction)actionButtonPressed:(id)sender
{
actionButton = (UIButton *)sender;
if(!player.playing)
{
[actionButton setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
[timer invalidate];
timer = nil;
[player play];
timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
else
{
if(timer)
{
[timer invalidate];
timer = nil;
}
[actionButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
[player pause];
}
}
//This is connected to the slider on the custom tableviewcell.
- (IBAction)slide:(id)sender
{
if(player)
{
player.currentTime = slider.value;
}
currentTimeLabel.text = [NSString stringWithFormat:@"%.1f", slider.value];
}
- (void)updateTime:(NSTimer *)timer
{
if(player)
{
slider.value = player.currentTime;
currentTimeLabel.text = [NSString stringWithFormat:@"%.1f", slider.value];
}
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
if(timer)
{
[timer invalidate];
timer = nil;
}
[self setupAudio];
[actionButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
}
我已經試過移動周圍的一些位,我不知道如果我要去在[self setupAudio]中獲取'RecordingTableCell'子視圖時出錯,或者音頻播放器設置錯誤。
任何幫助將不勝感激!
謝謝您的回覆(+1),我測試你的代碼在斯威夫特和它的,因爲它適用於自己的,然而,當合併入我原來objc我仍然會得到同樣的錯誤UISlider&UILabels只會在第二場比賽後作出迴應。 –