iOS 7 : simple audio controls with AVAudioPlayer?

别等时光非礼了梦想. 提交于 2019-12-03 03:57:10

问题


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:


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;

}



回答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;

}


来源:https://stackoverflow.com/questions/19638497/ios-7-simple-audio-controls-with-avaudioplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!