音频播放器在目标c中显示定时器的进度滑块

问题描述:

我是iOS应用开发新手,我的需求是需要在播放时访问进度滑块,并显示正确的开始和结束时间,播放器正在使用网址流,可以你请帮我给一些示例代码,以解决这个问题,在此先感谢。音频播放器在目标c中显示定时器的进度滑块

+0

检查此URL http://stackoverflow.com/questions/27970317/how-to-pause-an-audio-and-its-timer-slider-in-ios –

enter image description here请找到下面的代码。我在Xcode 8测试版已经测试了2

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 
@end 

ViewController.m

#import "ViewController.h" 
#import <AVFoundation/AVFoundation.h> 

@interface ViewController() 
{ 
    IBOutlet UISlider  *sliderView; 
    IBOutlet UILabel  *lblTimeInterval; 
    AVAudioPlayer   *avpPlayer; 
} 
@end 

@implementation ViewController 
@synthesize strName; 
@synthesize strTitle; 
@synthesize trackCount; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    //First comment added to Proj. 
    //New Branch one is created by Team. 

} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


- (IBAction)progrssSlider { 
    avpPlayer.currentTime = sliderView.value; 
} 

- (void)updateTime:(NSTimer *)timer { 
    sliderView.value = avpPlayer.currentTime; 

    lblTimeInterval.text = [NSString stringWithFormat:@"%f",sliderView.value]; 

} 

- (IBAction)pressPlayButton:(id)sender { 

    //Read sound file from resource folder 
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sample.mp3" ofType:nil]]; 


    //Initialize AVAudioPlayer 
    NSError *error; 
    avpPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; 

    //Check Player is initialized 
    if (!avpPlayer){ 
     NSLog(@"Error: %@", error); 
    } 
    else 
    { 
     [avpPlayer prepareToPlay]; 
     sliderView.maximumValue = [avpPlayer duration]; 
     sliderView.value = 0.0; 

     [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES]; 
     [avpPlayer play]; 
    } 


} 

@end 

请定义的UILabel显示时间,UISlider来更新滑块和XIB播放按钮。

希望这会起作用。