2015-04-04 219 views
1

我在服務器中有一個音頻文件,所以我可以使用該網址播放音頻文件。以下是代碼。如何在播放音頻文件時顯示進度條?如何在iOS中播放音頻文件時顯示進度條

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

回答

4

使用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]; 
相關問題