IOS 7: simple audio devices with AVAudioPlayer?

I just started coding iOS apps using xCode, and it is not very simple and not intuitive to find how everything works. I am very new to this and my application is very slow ^^. Anyway, I'm trying to do something on iOS7 right now.

I managed to create dynamic tables with customs and dynamic height, but now I can’t find a solution to my problem ... Maybe I didn’t look for the right place ... anyway.

I have a sound 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 sound is playing. But I have no control. I have successfully added a play / pause button, but how to navigate through the audio? Should I encode ALL the interface? There is no easy interface with a button and a responsive progress bar?

And if I need to encode it, well, buzz ... where to start?

Thank you so much!

+7
ios xcode5 avaudioplayer
source share
2 answers

Use UISlider with AVAudioPlayer playAtTime: method, there is no built-in search bar for AVAudioPlayer.

Check out this sample code, it implements what you want in the avTouchController class

https://developer.apple.com/library/ios/samplecode/avTouch/Introduction/Intro.html

add a UISLider to you interface and bind valueChanged: to the seekTime method:

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 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; } 
+17
source share
 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; } 
+2
source share

All Articles