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

时间:2013-10-28 15:14:27

标签: ios avaudioplayer xcode5

我刚开始使用xCode编写iOS应用程序代码,并且找不到工作原理并不是很容易也不直观。我对此很新,我的应用程序进展非常缓慢^^。无论如何,我现在至少在iOS7上尝试过。

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

我有一个音频播放,感谢这些界限:

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

[audio play];
[audio updateMeters];

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

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

非常感谢!

2 个答案:

答案 0 :(得分:17)

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

查看此示例代码,它在类avTouchController

中实现了您想要的功能

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

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

in .h

@property (nonatomic, weak) IBOutlet UISlider *seekbar;

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;

@property (nonatomic, strong) NSTimer *updateTimer;
加载AVAudioPlayer后,在viewmidLoad中的.m中,

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;

}

答案 1 :(得分: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;

}