本文介绍了iOS 7:AVAudioPlayer的简单音频控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

非常感谢!

推荐答案

将UISlider与AVAudioPlayer的playAtTime配合使用:方法,AVAudioPlayer没有内置的搜索栏。

Use a UISlider with AVAudioPlayer's playAtTime: Method, there is no built-in seek bar for AVAudioPlayer.

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

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

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

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;

加载AVAudioPlayer后,在viewDidLoad中的.m中的

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]

并添加以下方法

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

- (IBAction)seekTime:(id)sender {

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

}

这篇关于iOS 7:AVAudioPlayer的简单音频控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 22:10