I have developed an iOS app with audio playback functionality. I have used a Progress View for the playback of the audio and have used a Horizontal Slider for the volume control.
2 Things I need help with:
The Progress View is uneditable. By that, I mean when the track is playing the user should be able to drag it to certain point and play from there. Same as iOS music app or Spotify??
The Volume slider allows user to adjust volume but it doesnt work alongside the iOS device audio. Meaning, when the audio level is changed, it should change on the actual device.
Here is my code:
NSString *stringPath = [[NSBundle mainBundle]pathForResource:@"01" ofType:@"MP3"];
NSURL *url = [NSURL fileURLWithPath:stringPath];
NSError *error;
avPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
[avPlayer setNumberOfLoops:2];
[avPlayer setVolume:self.sliderVolumeOutlet.value];
[avPlayer stop];
[NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(updateMyProgress) userInfo:nil repeats:YES];
-(void)updateMyProgress
{
float progress = [avPlayer currentTime]/[avPlayer duration];
self.myProgressView.progress = progress;
}
- (IBAction)sliderVolumeAction:(id)sender {
UISlider *mySlider = sender;
[avPlayer setVolume:mySlider.value];
}
- (IBAction)stopButton:(id)sender {
[avPlayer stop];
[avPlayer setCurrentTime:0];
}
- (IBAction)pauseButton:(id)sender {
[avPlayer pause];
}
- (IBAction)playButton:(id)sender {
[avPlayer play];
}
- (IBAction)backButtonEvent:(id)sender {
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)viewWillDisappear:(BOOL)animated
{
[avPlayer stop];
}