我有一个表视图来显示声音文件以及每个原型单元格中的播放按钮。当用户点击播放按钮时,我正在尝试播放与所选单元格合并的声音文件。
在代码中,一旦用户点击它,我就会更改UIButton
的{{1}}。完成播放后,它将重置为默认按钮图像。但是,如果用户在播放当前曲目时单击下一个单元格按钮,则无法更改先前选择的UIImage
。
请告诉我我的代码出了什么问题。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dirPath = [paths objectAtIndex:0];
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirPath error:nil];
// UITableViewPlayer *cellPlayer;
_cellPlayer = [tableView dequeueReusableCellWithIdentifier:@"cell"];
_cellPlayer.btnPlay.tag = indexPath.row;
_cellPlayer.btnPlay.titleLabel.hidden = YES;
[_cellPlayer.btnPlay addTarget:self action:@selector(playCurrenTrack:) forControlEvents:UIControlEventTouchUpInside];
_cellPlayer.pView.tag = indexPath.row;
_cellPlayer.songTitle.text = [fileList objectAtIndex:indexPath.row];
return _cellPlayer;
}
-(void)playCurrenTrack : (id) sender{
UIButton *clicked = (UIButton *) sender;
NSInteger rowNumber = clicked.tag;
// reset the default button
for (int i = 0; i < DirctoryList.count; i++) {
UITableViewPlayer *cellPlayer = [self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
[cellPlayer.btnPlay setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
}
NSIndexPath *myIP = [NSIndexPath indexPathForRow:rowNumber inSection:0];
UITableViewPlayer *currentView = (UITableViewPlayer *)[myTableView cellForRowAtIndexPath:myIP];
if(rowNumber == playerIndex && [player isPlaying]){
[currentView.btnPlay setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
[player pause];
return;
}else if(rowNumber == playerIndex && isPlayerBusy){
[currentView.btnPlay setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
[player play];
return;
}
[player stop];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *fileListAct = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSString *FinalFile= [NSString stringWithFormat:@"%@",fileListAct[rowNumber]];
NSString *strippedContent = [FinalFile stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *FinalPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:strippedContent];
NSURL * soundFileURL = [NSURL fileURLWithPath:FinalPath];
NSError *error;
player = [[AVAudioPlayer alloc]
initWithContentsOfURL:soundFileURL
error:&error];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: NO error: nil];
if (error)
{
NSLog(@"Error in audioPlayer: %@",
[error localizedDescription]);
} else {
player.delegate = self;
[player prepareToPlay];
// set the index for checking current playing track
playerIndex = clicked.tag;
isPlayerBusy = TRUE;
[player play];
[currentView.btnPlay setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
/*
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(UpdateProgressBar:) userInfo:nil repeats:YES];
*/
}
}
答案 0 :(得分:1)
做两件事:
首先cellForRowAtIndexPath
selectedImage
_cellPlayer.btnPlay
内[_cellPlayer.btnPlay setImage:@"selectedImage" forState:UIControlStateSelected];
为:{/ p>
playCurrenTrack
然后在[_cellPlayer.btnPlay setSelected:YES];
中将其设置为已选中。
PT_CLAIMS IN CLIM_BEAN_TAB,
PO_CLAIMS_CUSROR OUT SYS_REFCURSOR
当你玩其他列表的项目时,不要忘记重新调整它。 ; - )
答案 1 :(得分:1)
这是工作代码可能会帮助某人。这对我有用
- (void)viewDidLoad{
playerIndex = -1;
prevSelection = nil;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString
*dirPath = [paths objectAtIndex:0];
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirPath error:nil];
// UITableViewPlayer *cellPlayer;
cellPlayer = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cellPlayer.btnPlay.tag = indexPath.row;
cellPlayer.btnPlay.titleLabel.hidden = YES;
[cellPlayer.btnPlay addTarget:self action:@selector(playCurrenTrack:) forControlEvents:UIControlEventTouchUpInside];
cellPlayer.pView.tag = indexPath.row;
cellPlayer.songTitle.text = [fileList objectAtIndex:indexPath.row];
return cellPlayer;
}
-(void)playCurrenTrack : (id) sender{
UIButton *clicked = (UIButton *) sender;
NSInteger rowNumber = clicked.tag;
NSIndexPath *myIP = [NSIndexPath indexPathForRow:rowNumber inSection:0];
UITableViewPlayer *currentView = (UITableViewPlayer *)[myTableView cellForRowAtIndexPath:myIP];
if(rowNumber == playerIndex){
if([player isPlaying]){
[currentView.btnPlay setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
[player pause];
}else{
[currentView.btnPlay setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
[player play];
}
}else{
if([player isPlaying]){
[player stop];
}
if (prevSelection != nil) {
UITableViewPlayer *prevView = (UITableViewPlayer *)[myTableView cellForRowAtIndexPath:prevSelection];
// make your priviousView player stop code here using priviousView & reload that indexpath
[prevView.btnPlay setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
[myTableView reloadRowsAtIndexPaths:@[prevSelection] withRowAnimation:UITableViewRowAnimationNone];
}
prevSelection = nil;
[currentView.btnPlay setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *fileListAct = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSString *FinalFile= [NSString stringWithFormat:@"%@",fileListAct[rowNumber]];
NSString *strippedContent = [FinalFile stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *FinalPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:strippedContent];
NSURL * soundFileURL = [NSURL fileURLWithPath:FinalPath];
NSError *error;
player = [[AVAudioPlayer alloc]
initWithContentsOfURL:soundFileURL
error:&error];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: NO error: nil];
if (error)
{
NSLog(@"Error in audioPlayer: %@",
[error localizedDescription]);
} else {
player.delegate = self;
[player prepareToPlay];
// set the index for checking current playing track
playerIndex = clicked.tag;
//set prevCell
prevSelection = myIP;
isPlayerBusy = TRUE;
[player play];
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(UpdateProgressBar:) userInfo:nil repeats:YES];
}
}
答案 2 :(得分:0)
因为你必须存储以前的单元索引路径。为NSIndexPath *priviousSelected;
并修改你的playCurrenTrack,如下所示,为UITableViewPlayer *priviousView
编写停止图像逻辑而不是注释//让你的privfulView播放器停止
-(void)playCurrenTrack : (id) sender{
UIButton *clicked = (UIButton *) sender;
NSInteger rowNumber = clicked.tag;
// reset the default button
for (int i = 0; i < DirctoryList.count; i++) {
UITableViewPlayer *cellPlayer = [self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
[cellPlayer.btnPlay setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
}
NSIndexPath *myIP = [NSIndexPath indexPathForRow:rowNumber inSection:0];
UITableViewPlayer *currentView = (UITableViewPlayer *)[myTableView cellForRowAtIndexPath:myIP];
if(rowNumber == playerIndex && [player isPlaying]){
[currentView.btnPlay setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
[player pause];
priviousSelected = nil;
return;
}else if(rowNumber == playerIndex && isPlayerBusy){
[currentView.btnPlay setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
if (priviousSelected != nil) {
UITableViewPlayer *priviousView = (UITableViewPlayer *)[myTableView cellForRowAtIndexPath:priviousSelected];
// make your priviousView player stop code here using priviousView & reload that indexpath
}
priviousSelected = myIP;
[player play];
return;
}
[player stop];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *fileListAct = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSString *FinalFile= [NSString stringWithFormat:@"%@",fileListAct[rowNumber]];
NSString *strippedContent = [FinalFile stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *FinalPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:strippedContent];
NSURL * soundFileURL = [NSURL fileURLWithPath:FinalPath];
NSError *error;
player = [[AVAudioPlayer alloc]
initWithContentsOfURL:soundFileURL
error:&error];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: NO error: nil];
if (error)
{
NSLog(@"Error in audioPlayer: %@",
[error localizedDescription]);
} else {
player.delegate = self;
[player prepareToPlay];
// set the index for checking current playing track
playerIndex = clicked.tag;
isPlayerBusy = TRUE;
[player play];
[currentView.btnPlay setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
/*
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(UpdateProgressBar:) userInfo:nil repeats:YES];
*/
}
}