I just begun to code iOS apps with xCode and it's not very easy nor intuitive to find how things work. I'm very new into this and my app goes on very slowly ^^. Anyway, I'm now trying things on iOS7, at least.
I managed to create dynamic tables with customs cells and dynamic height but now I don't find any solution to my problem... Maybe I didn't search at the right place... anyway.
I have an audio playing, thanks to these lines:
NSString *path = [[NSBundle mainBundle] pathForResource:@"song" ofType:@"mp3"];
AVAudioPlayer *audio = [[AVAudioPlayer alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
[audio play];
[audio updateMeters];
Now, that's great, my audio plays. But I don't have any controls. I successfully added a play/pause button, but how to navigate inside the audio? Do I have to code ALL the interface? There isn't a simple interface with a button and a responsive progress bar?
And if I have to code it, well, hum... where do I start?
Thanks a lot!
Use a UISlider with AVAudioPlayer's playAtTime: Method, there is no built-in seek bar for AVAudioPlayer.
Check out this sample code, it implements what you want in the class avTouchController
https://developer.apple.com/library/ios/samplecode/avTouch/Introduction/Intro.html
add a UISLider to you interface and link the valueChanged: to the method seekTime:
in .h
@property (nonatomic, weak) IBOutlet UISlider *seekbar;
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@property (nonatomic, strong) NSTimer *updateTimer;
in .m in viewDidLoad after loading AVAudioPlayer,
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:@"There's A Long, Long Trail A-Winding" ofType:@"mp3"]];
AVAudioPlayer *audio = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
self.audioPlayer = audio;
self.seekbar.minimumValue = 0;
self.seekbar.maximumValue = self.audioPlayer.duration;
[[self audioPlayer] play];
self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateSeekBar) userInfo:nil repeats:YES]
and add the following method
- (void)updateSeekBar{
float progress = self.audioPlayer.currentTime;
[self.seekbar setValue:progress];
}
- (IBAction)seekTime:(id)sender {
self.audioPlayer.currentTime = self.seekbar.value;
}
Play audio and update UISlider with it.
-(void)viewWillAppear:(BOOL)animated
{
NSString *soundFile=[[NSBundle mainBundle] pathForResource:@"name of your audio file." ofType:@".mp3"];
NSURL *soundFileUrl=[NSURL fileURLWithPath:soundFile];
soundPlayer=[[AVAudioPlayer alloc] initWithContentsOfURL:soundFileUrl error:nil];
[soundPlayer prepareToPlay];
soundPlayer.delegate=self;
slider.maximumValue=[soundPlayer duration];
slider.minimumValue=0.0;
soundPlayer.currentTime=slider.value;
[soundPlayer play];
[self startTimer];
}
#pragma mark Timer Methods
- (void)startTimer
{
timerForPlaying= [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];
}
- (void)stopTimer
{
[timerForPlaying invalidate];
timerForPlaying = nil;
}
// This method for updating UISlider when sound start to play.
- (void)updateSlider
{
[slider setValue:soundPlayer.currentTime animated:NO];
startTime=slider.value;
}
// When we adjust sound according to slider value connect this method to your slider value change event.
-(void)valueChange:(id)sender
{
soundPlayer.currentTime=slider.value;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With