【问题标题】:How to show the progressbar while playing audio file in iOS如何在iOS中播放音频文件时显示进度条
【发布时间】:2015-04-17 21:06:58
【问题描述】:

我在服务器中有一个音频文件,所以我可以播放带有 url 的音频文件。下面是代码。如何在播放音频文件时显示进度条?

-(void)playselectedsong{
    AVPlayer *player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:urlString]];
    [player play];
}

【问题讨论】:

    标签: ios iphone avfoundation avaudioplayer audio-streaming


    【解决方案1】:

    使用 URL 创建 AVAsset,然后获取使用 URL 创建的 AVAsset 的持续时间。

    AVAsset *audio = [AVAsset assetWithURL:]; // create an audio instance.
    float duration = CMTimeGetSeconds([audio currentTime]); // gives the seconds of audio file.
    
    AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: audio]; // create a player item.
    
    AVPlayer *avPlayer = [[AVPlayer alloc] initWithPlayerItem: item]; // initialise the AVPlayer with the AVPlayerItem.
    
    [avPlayer play]; // play the AVPlayer
    

    然后你可以设计进度条,播放音频文件后每隔几秒更新一次。

    UIProgressView *myProgressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
    

    每秒调用一次更新方法。

    NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:300 target:self selector:@selector(methodToUpdateProgress) userInfo:nil repeats:YES];
    

    然后在“methodToUpdateProgress”中更新进度条。

    [myProgressView setProgress:someFloat animated:YES];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-22
      相关资源
      最近更新 更多