UISlider to control AVAudioPlayer

后端 未结 4 1137
执笔经年
执笔经年 2020-12-01 03:40

I\'m trying to implement a little function in my app. I am currently playing sounds as AVAudioPlayers and that works fine. What I would like to add is to control the sound\'

4条回答
  •  天涯浪人
    2020-12-01 04:08

    Shouldn't be a problem - just set the slider to continuous and set the max value to your player's duration after loading your sound file.

    Edit

    I just did this and it works for me...

    - (IBAction)slide {
        player.currentTime = slider.value;
    }
    
    - (void)updateTime:(NSTimer *)timer {
        slider.value = player.currentTime;
    }
    
    - (IBAction)play:(id)sender {
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sound.caf" ofType:nil]];
        NSError *error;
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        if (!player) NSLog(@"Error: %@", error);
        [player prepareToPlay];
        slider.maximumValue = [player duration];
        slider.value = 0.0;
        
        [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];  
        [player play];
    }
    

    The slider is configured in IB, as is a button to start playing.

    Swift 3.0 Update:

    var player: AVAudioPlayer!
    var sliderr: UISlider!
    
    @IBAction func play(_ sender: Any) {
        var url = URL(fileURLWithPath: Bundle.main.path(forResource: "sound.caf", ofType: nil)!)
        var error: Error?
        do {
            player = try AVAudioPlayer(contentsOf: url)
        }
        catch let error {
        }
        if player == nil {
            print("Error: \(error)")
        }
        player.prepareToPlay()
        sliderr.maximumValue = Float(player.duration)
        sliderr.value = 0.0
        Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.updateTime), userInfo: nil, repeats: true)
        player.play()
    }
    
    func updateTime(_ timer: Timer) {
        sliderr.value = Float(player.currentTime)
    }
    
    @IBAction func slide(_ slider: UISlider) {
        player.currentTime = TimeInterval(slider.value)
    }
    

提交回复
热议问题