在ios sdk中的每个UITableViewcell中播放带滑块的音频

时间:2014-12-23 04:35:24

标签: ios iphone audio-streaming

如何在uitebleviewcell中使用所有控制器播放音频。 我正在研究一个项目,当按下一个按钮中的一个单元格时,会发出声音。按钮停止,声音将停止。带有控制器的滑块。并在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

1 个答案:

答案 0 :(得分:1)

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

-(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];
            }

        }
 }