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

Ios 音频播放器进度滑块,定时器显示在目标c中,ios,objective-c,iphone,Ios,Objective C,Iphone,我是来自iOS应用程序开发的新手,我的要求是在播放时需要访问进度滑块,并显示正确的开始和结束时间,播放器正在url流媒体中工作,您能否帮助我提供一些示例代码,以解决此问题,提前感谢。请查找以下代码。我已经在Xcode 8 beta 2中进行了测试 ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end #导入 @界面ViewController:UIVie

我是来自iOS应用程序开发的新手,我的要求是在播放时需要访问进度滑块,并显示正确的开始和结束时间,播放器正在url流媒体中工作,您能否帮助我提供一些示例代码,以解决此问题,提前感谢。

请查找以下代码。我已经在Xcode 8 beta 2中进行了测试

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@end
#导入
@界面ViewController:UIViewController
@结束
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
#导入“ViewController.h”
#进口
@界面视图控制器()
{
iBlider*sliderView;
IBUILabel*lblTimeInterval;
AVAudioPlayer*avpPlayer;
}
@结束
@实现视图控制器
@合成strName;
@合成淀粉;
@综合航迹计数;
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
//项目中添加了第一条注释。
//新的分支1由团队创建。
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(iAction)程序滑动器{
avpPlayer.currentTime=sliderView.value;
}
-(void)updateTime:(NSTimer*)计时器{
sliderView.value=avpPlayer.currentTime;
lblTimeInterval.text=[NSString stringWithFormat:@“%f”,sliderView.value];
}
-(iAction)按播放按钮:(id)发送者{
//从资源文件夹读取声音文件
NSURL*url=[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@“sample.mp3”,类型为nil]];
//初始化AVAudioPlayer
n错误*错误;
avpPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url错误:&error];
//检查播放器是否初始化
如果(!avpPlayer){
NSLog(@“错误:%@”,错误);
}
其他的
{
[avpPlayer prepareToPlay];
sliderView.maximumValue=[avpPlayer持续时间];
sliderView.value=0.0;
[NSTimer scheduledTimerWithTimeInterval:1.0目标:自选择器:@selector(updateTime:)userInfo:nil repeats:YES];
[avpPlayer play];
}
}
@结束
请定义UILAbel以显示时间,UISlider以更新滑块并在Xib中播放按钮

希望这能奏效。

检查此url