gpt4 book ai didi

ios - 在 ios sdk 中的每个 TableViewcell 中使用 slider 播放音频

转载 作者:行者123 更新时间:2023-11-29 02:25:25 25 4
gpt4 key购买 nike

如何在 uiebleviewcell 中使用所有 Controller 播放音频。我正在做一个项目,当按下按钮中的单元格时,将播放声音。按钮停止,声音将停止。带有 Controller 的 slider 。并在 tableview 单元格中显示标签持续时间。我已将我的声音放入一个数组,像这样:

- (void)viewDidLoad
{
[super viewDidLoad];
//Data

arr_music=[[NSMutableArray alloc]init];
[arr_music addObject:@"04 Rupaiyya - DownloadMing.INFO"];
[arr_music addObject:@"03 Suit Tera Laal Rang Da - www.FreshMaza.Info"];
[arr_music addObject:@"audiofile"];
[[self tableView] reloadData];

}

enter image description here

最佳答案

这是我在应用程序中使用的示例代码,用于在 UICollectionViewCell 单元格中播放音频 - 您可以在播放按钮上调用此方法,在项目中添加 AVFoundation 框架:

-(void)playPauseClick:(id)sender{
UIButton *button =(UIButton*)sender;
int value=(int)button.tag;
UICollectionViewCell *mycell =(UICollectionViewCell *)[self.collectionView cellForItemAtIndexPath:oldIndexPath];

// if audio is already playing stop it and return
if (![button.currentImage isEqual:[UIImage imageNamed:@"Play"]])
{

[mycell.btnPlayPause setImage:[UIImage imageNamed:@"Play"] forState:UIControlStateNormal];
[player pause];
[updateTimer invalidate];
updateTimer=nil;
mycell.slider.userInteractionEnabled=NO;
mycell.slider.value=mycell.slider.minimumValue;
mycell.lblPlayTime.text=[self timeFormatted:(int)mycell.slider.maximumValue];
selectedRow=-1;
return;
}


// if user click on another audio control then stop old one and play other
if(selectedRow!=-1)
{
NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:selectedRow inSection:0];

UICollectionViewCell *oldcell =
(UICollectionViewCell *)[self.collectionView cellForItemAtIndexPath:oldIndexPath];
oldcell.btnPlayPause setImage:[UIImage imageNamed:@"Play"] forState:UIControlStateNormal];
oldcell.slider.userInteractionEnabled=NO;
oldcell.slider.value=oldcell.slider.minimumValue;
oldcell.lblPlayTime.text=[self timeFormatted:(int)oldcell.slider.maximumValue];

[player pause];

}

NSURL *fileUrl;
NSString *filePath;
// to keep track of selected row
selectedRow=value;

NSString *fileName= @"Your File Name";

// playing Audio From Document Directory
NSString *appFolder = [self applicationDocumentsDirectory] ;
filePath = [appFolder stringByAppendingPathComponent:fileName];
fileUrl=[[NSURL alloc]initFileURLWithPath:filePath];

[self setupAVPlayerForURL:fileUrl];

double mplayed=CMTimeGetSeconds([player.currentItem currentTime]);

mycell.lblPlayTime.text=[self timeFormatted:(int)mplayed];
[mycell.slider setValue:mplayed];

mycell.slider.userInteractionEnabled=YES;

[mycell.slider addTarget:self
action:@selector(sliderValueChanged:)
forControlEvents:UIControlEventValueChanged];

updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(updateSeekBar)
userInfo:nil
repeats:YES];




[mycell.btnPlayPause setImage:[UIImage imageNamed:@"Pause"] forState:UIControlStateNormal];

NSLog(@"msg is :%@",msg );

}
}


// Set up avplayer

-(void) setupAVPlayerForURL: (NSURL*) url {
AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];
player = [AVPlayer playerWithPlayerItem:anItem];
[player play];

}


- (void)sliderValueChanged:(UISlider *)slider {


int32_t timeScale = player.currentItem.asset.duration.timescale;

[player seekToTime: CMTimeMakeWithSeconds(slider.value, timeScale)
toleranceBefore: kCMTimeZero
toleranceAfter: kCMTimeZero
completionHandler: ^(BOOL finished) {
[player play];
}];


NSIndexPath *indexPath = [NSIndexPath indexPathForRow:selectedRow inSection:0];
UICollectionViewCell *mycell =
(UICollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
NSString *stPlayTime;

stPlayTime=[NSString stringWithFormat:@"%.2f",mycell.slider.value/60 ];

//Label to display Time
mycell.lblPlayTime.text=stPlayTime;

}

- (void)playerItemDidReachEnd {
[player.currentItem seekToTime:kCMTimeZero];
[player pause];

[updateTimer invalidate];
updateTimer=nil;

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:selectedRow inSection:0];

UICollectionViewCell *mycell =
(UICollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
[mycell.btnPlayPause setImage:[UIImage imageNamed:@"Play"] forState:UIControlStateNormal];

mycell.slider.userInteractionEnabled=NO;


mycell.lblPlayTime.text=[self timeFormatted:(int)mycell.slider.maximumValue];

mycell.slider.value=mycell.slider.minimumValue;


}


- (void)updateSeekBar{

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:selectedRow inSection:0];

UICollectionViewCell *mycell =
(UICollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];

if( mycell.slider.userInteractionEnabled)
{

double mplayed=CMTimeGetSeconds([player.currentItem currentTime]);

mycell.lblPlayTime.text=[self timeFormatted:(int)mplayed];

// NSLog(@"player current Time for local is :%f",mplayed);
[mycell.slider setValue:mplayed];

if(mycell.slider.value==mycell.slider.maximumValue)
{
[self playerItemDidReachEnd];
}

}
}

关于ios - 在 ios sdk 中的每个 TableViewcell 中使用 slider 播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27614134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com