iOS 7:AVAudioPlayer简单的audio控制?

我刚开始用xCode编写iOS应用程序,发现事情如何工作并不容易也不直观。 我是非常新的,我的应用程序非常缓慢^^。 无论如何,我现在正在尝试iOS7上的东西,至less。

我设法创build了具有关联单元格和dynamic高度的dynamic表格,但是现在我找不到解决我的问题的方法…也许我没有在正确的位置search…无论如何。

我有一个audio播放,感谢这些线:

NSString *path = [[NSBundle mainBundle] pathForResource:@"song" ofType:@"mp3"]; AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil]; ; ; 

现在,这很好,我的audio播放。 但我没有任何控制。 我成功添加了播放/暂停button,但是如何在audio内部导航? 我必须编写所有的界面? 有没有一个简单的界面与一个button和响应进度栏?

如果我必须编码,嗯,呃…我从哪里开始?

非常感谢!

与AVAudioPlayer的playAtTime:Method一起使用UISlider,AVAudioPlayer没有内置的search栏。

看看这个示例代码,它在类avTouchController中实现你想要的

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

添加一个UISLider给你的界面,并将valueChanged:链接到方法seekTime:

在.h

 @property (nonatomic, weak) IBOutlet UISlider *seekbar; @property (nonatomic, strong) AVAudioPlayer *audioPlayer; @property (nonatomic, strong) NSTimer *updateTimer; 

在加载AVAudioPlayer后的viewDidLoad中,

 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] 

并添加以下方法

 - (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; }