iOS 7:AVAudioPlayer的简单音频控件?

rek*_*kam 8 avaudioplayer ios xcode5

我刚开始使用xCode编写iOS应用程序,并且找到工作原理并不是很容易也不直观.我对此很新,我的应用程序继续非常慢^^.无论如何,我现在正在尝试iOS7上的东西,至少.

我设法创建了具有海关单元和动态高度的动态表格,但现在我找不到解决问题的方法......也许我没有在正确的地方搜索...无论如何.

我有一个音频播放,感谢这些线路:

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

[audio play];
[audio updateMeters];
Run Code Online (Sandbox Code Playgroud)

现在,这很棒,我的音频播放.但我没有任何控制.我成功添加了播放/暂停按钮,但如何在音频内导航?我必须编写所有接口的代码吗?没有简单的界面与按钮和响应进度条?

如果我必须编码,那么,哼......我从哪里开始?

非常感谢!

Man*_*Mac 17

使用带有AVAudioPlayer的playAtTime:Method的UISlider,AVAudioPlayer没有内置的搜索栏.

看看这个示例代码,它在类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;
Run Code Online (Sandbox Code Playgroud)

加载AVAudioPlayer后在.m中的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]
Run Code Online (Sandbox Code Playgroud)

并添加以下方法

- (void)updateSeekBar{
float progress = self.audioPlayer.currentTime;
[self.seekbar setValue:progress];
}

- (IBAction)seekTime:(id)sender {

self.audioPlayer.currentTime = self.seekbar.value;

}
Run Code Online (Sandbox Code Playgroud)