8

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!

1
  • If you want buttons to control the music, a scrubbing slider, volume control etc. you'll need to create them yourself and their corresponding methods. You'll probably want to look at NSTimer if you want create a scrubbing slider too.
    – sooper
    Oct 28, 2013 at 15:21

2 Answers 2

17

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;

}
5
  • Oops, added, now. It's really easy to implement, don't get bogged down by all the additional code though (AVAudioSession) Oct 28, 2013 at 15:48
  • wow, yeah, it seems a bit hard :P ... but I don't need the level meter, loop, and these sort of things. Only play/pause and the responsive "timeline". I will try to implement this. I hope I can come with something soon! thanks
    – rekam
    Oct 28, 2013 at 16:01
  • if you are serious, I can add the code required for implementing just the "timeline", it would need an NSTimer and UISlider. Oct 28, 2013 at 16:49
  • If the above code works for you, please approve it as the answer. : ) Oct 28, 2013 at 19:35
  • Impressive! That's working like a charm :) ! Now I need to remove the audio when the navigation view is popped, but that's an other story. Thank you very much!
    – rekam
    Oct 29, 2013 at 9:01
2
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;

}
1
  • What will happen when we pause audio? May 13, 2017 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.