歌曲不会在iOS上快进

歌曲不会在iOS上快进,ios,objective-c,iphone,audio,Ios,Objective C,Iphone,Audio,我正在实现一个音频播放器。它工作正常,但滑块拖动不会更新歌曲。这首歌是从头开始的,而不是快进的 /* * Updates the time label display and * the current value of the slider * while audio is playing */ - (void)updateTime:(NSTimer *)timer { //to don't update every second. When scrubber is mouse

我正在实现一个音频播放器。它工作正常,但滑块拖动不会更新歌曲。这首歌是从头开始的,而不是快进的

/*
 * Updates the time label display and
 * the current value of the slider
 * while audio is playing
 */
- (void)updateTime:(NSTimer *)timer {
    //to don't update every second. When scrubber is mouseDown the the slider will not set
    NSLog(@"slider = %f",self.currentTimeSlider.value);

        long currentPlaybackTime = [self.audioPlayer getCurrentAudioTime];
   // self.currentTimeSlider.value = currentPlaybackTime / [self.audioPlayer getAudioDuration];
    if ((currentPlaybackTime / [self.audioPlayer getAudioDuration]) > self.currentTimeSlider.value )
   {
        self.currentTimeSlider.value = currentPlaybackTime / [self.audioPlayer getAudioDuration];

    self.timeElapsed.text = [NSString stringWithFormat:@"%@",
                             [self.audioPlayer timeFormat:[self.audioPlayer getCurrentAudioTime]]];

    self.duration.text = [NSString stringWithFormat:@"%@",
                          [self.audioPlayer timeFormat:[self.audioPlayer getAudioDuration] - [self.audioPlayer getCurrentAudioTime]]];
    }
    else{
        //self.currentTimeSlider.value = currentPlaybackTime / [self.audioPlayer getAudioDuration];

        NSLog(@"sliderValuessszz = %f",self.currentTimeSlider.value);

        NSLog(@"audio time = %f",[self.audioPlayer getCurrentAudioTime]);



        self.timeElapsed.text = [NSString stringWithFormat:@"%@",
                                 [self.audioPlayer timeFormat:[self.audioPlayer getCurrentAudioTime]]];

        NSLog(@"time elapsed = %d",self.timeElapsed.text);



        self.duration.text = [NSString stringWithFormat:@"%@",
                              [self.audioPlayer timeFormat:[self.audioPlayer getAudioDuration] - [self.audioPlayer getCurrentAudioTime]]];
    }

}

- (IBAction)sliderChanged:(id)currentTimeSlider
{
    NSLog(@"sliderValue = %f",self.currentTimeSlider.value);



    NSLog(@"sliderValuesss = %f",self.currentTimeSlider.value);

    //[self.timer invalidate];
    [self.audioPlayer setCurrentAudioTime:self.currentTimeSlider.value];


    [NSTimer scheduledTimerWithTimeInterval:0.01
                                     target:self
                                   selector:@selector(updateTime:)
                                   userInfo:nil
                                    repeats:NO];





    /*if (!self.isPaused){
        [self.audioPlayer stopAudio];
        [self.audioPlayer setCurrentAudioTime:self.currentTimeSlider.value];
        [self.audioPlayer prepareToPlayAudio];
        [self.audioPlayer playAudio];
    }
    else
    {
        [self.audioPlayer setCurrentAudioTime: self.currentTimeSlider.value];
    }
    */

}
在滑块上拖动歌曲从开始开始,计时器标签也显示开始时间。有人能帮我解决这个问题吗

谢谢,

您的MyAudioPlayer.h

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface MyAudioPlayer : UIViewController

@property (nonatomic, retain) AVAudioPlayer *audioPlayer;

// Public methods
- (void)initPlayer:(NSString*) audioFile fileExtension:(NSString*)fileExtension;
- (void)playAudio;
- (void)pauseAudio;
- (void)setCurrentAudioTime:(float)value;
- (float)getAudioDuration;
- (NSString*)timeFormat:(float)value;
- (NSTimeInterval)getCurrentAudioTime;

@end
init方法:

 self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileLocationURL error:&error];
视图控制器类

 #import "MyAudioPlayer.h"

@interface ViewController : UIViewController

@property (nonatomic, strong) YMCAudioPlayer *audioPlayer;

@property (weak, nonatomic) IBOutlet UISlider *currentTimeSlider;
@property (weak, nonatomic) IBOutlet UIButton *playButton;
@property (weak, nonatomic) IBOutlet UILabel *duration;
@property (weak, nonatomic) IBOutlet UILabel *timeElapsed;

@property BOOL isPaused;
@property BOOL scrubbing;

@property NSTimer *timer;

@end
ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.audioPlayer = [[MyAudioPlayer alloc] init];
    [self setupAudioPlayer:@"audiofile"];
}

/*
 * Setup the AudioPlayer with
 * Filename and FileExtension like mp3
 * Loading audioFile and sets the time Labels
 */
- (void)setupAudioPlayer:(NSString*)fileName
{
    //insert Filename & FileExtension
    NSString *fileExtension = @"mp3";

    //init the Player to get file properties to set the time labels
    [self.audioPlayer initPlayer:fileName fileExtension:fileExtension];
    self.currentTimeSlider.maximumValue = [self.audioPlayer getAudioDuration];

    //init the current timedisplay and the labels. if a current time was stored
    //for this player then take it and update the time display
    self.timeElapsed.text = @"0:00";

    self.duration.text = [NSString stringWithFormat:@"-%@",
                          [self.audioPlayer timeFormat:[self.audioPlayer getAudioDuration]]];

}

/*
 * PlayButton is pressed
 * plays or pauses the audio and sets
 * the play/pause Text of the Button
 */
- (IBAction)playAudioPressed:(id)playButton
{
    [self.timer invalidate];
    //play audio for the first time or if pause was pressed
    if (!self.isPaused) {
        [self.playButton setBackgroundImage:[UIImage imageNamed:@"audioplayer_pause.png"]
                                   forState:UIControlStateNormal];

        //start a timer to update the time label display
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                      target:self
                                                    selector:@selector(updateTime:)
                                                    userInfo:nil
                                                     repeats:YES];

        [self.audioPlayer playAudio];
        self.isPaused = TRUE;

    } else {
        //player is paused and Button is pressed again
        [self.playButton setBackgroundImage:[UIImage imageNamed:@"audioplayer_play.png"]
                                   forState:UIControlStateNormal];

        [self.audioPlayer pauseAudio];
        self.isPaused = FALSE;
    }
}

/*
 * Updates the time label display and
 * the current value of the slider
 * while audio is playing
 */
- (void)updateTime:(NSTimer *)timer {
    //to don't update every second. When scrubber is mouseDown the the slider will not set
    if (!self.scrubbing) {
        self.currentTimeSlider.value = [self.audioPlayer getCurrentAudioTime];
    }
    self.timeElapsed.text = [NSString stringWithFormat:@"%@",
                             [self.audioPlayer timeFormat:[self.audioPlayer getCurrentAudioTime]]];

    self.duration.text = [NSString stringWithFormat:@"-%@",
                          [self.audioPlayer timeFormat:[self.audioPlayer getAudioDuration] - [self.audioPlayer getCurrentAudioTime]]];
}

/*
 * Sets the current value of the slider/scrubber
 * to the audio file when slider/scrubber is used
 */
- (IBAction)setCurrentTime:(id)scrubber {
    //if scrubbing update the timestate, call updateTime faster not to wait a second and dont repeat it
    [NSTimer scheduledTimerWithTimeInterval:0.01
                                     target:self
                                   selector:@selector(updateTime:)
                                   userInfo:nil
                                    repeats:NO];

    [self.audioPlayer setCurrentAudioTime:self.currentTimeSlider.value];
    self.scrubbing = FALSE;
}

/*
 * Sets if the user is scrubbing right now
 * to avoid slider update while dragging the slider
 */
- (IBAction)userIsScrubbing:(id)sender {
    self.scrubbing = TRUE;
}

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

@end


self.audioPlayer = [[MyAudioPlayer alloc] init];
音频播放器

self.timeElapsed.text = @"0:00";

self.duration.text = [NSString stringWithFormat:@"-%@",
                      [self.audioPlayer timeFormat:[self.audioPlayer getAudioDuration]]];
更新时间

- (void)updateTime:(NSTimer *)timer {
    //to don't update every second. When scrubber is mouseDown the the slider will not set
    if (!self.scrubbing) {
        self.currentTimeSlider.value = [self.audioPlayer getCurrentAudioTime];
    }
    self.timeElapsed.text = [NSString stringWithFormat:@"%@",
                             [self.audioPlayer timeFormat:[self.audioPlayer getCurrentAudioTime]]];

    self.duration.text = [NSString stringWithFormat:@"-%@",
                          [self.audioPlayer timeFormat:[self.audioPlayer getAudioDuration] - [self.audioPlayer getCurrentAudioTime]]];
}

滑块
minimumValue
maxiumvalue
设置为什么?如果为默认值(分别为0和1),则在更改滑块时,
AVAudioPlayer
setCurrentAudioTime
将被设置为一个介于0和1之间的值,这使它看起来像是在重新开始。@bobnoble感谢您的想法奏效。我将“最大值”设置为歌曲的持续时间,它现在正在工作。谢谢
- (void)updateTime:(NSTimer *)timer {
    //to don't update every second. When scrubber is mouseDown the the slider will not set
    if (!self.scrubbing) {
        self.currentTimeSlider.value = [self.audioPlayer getCurrentAudioTime];
    }
    self.timeElapsed.text = [NSString stringWithFormat:@"%@",
                             [self.audioPlayer timeFormat:[self.audioPlayer getCurrentAudioTime]]];

    self.duration.text = [NSString stringWithFormat:@"-%@",
                          [self.audioPlayer timeFormat:[self.audioPlayer getAudioDuration] - [self.audioPlayer getCurrentAudioTime]]];
}