8

我刚刚开始使用 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];

现在,太好了,我的音频播放了。但我没有任何控制。我成功添加了播放/暂停按钮,但如何在音频中导航?我必须对所有接口进行编码吗?没有带有按钮和响应式进度条的简单界面吗?

如果我必须对其进行编码,嗯,嗯......我从哪里开始?

非常感谢!

4

2 回答 2

17

使用带有 AVAudioPlayer 的 playAtTime: 方法的 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;

在加载 AVAudioPlayer 后 viewDidLoad 中的 .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;

}
于 2013-10-28T15:21:11.967 回答
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;

}
于 2014-01-16T09:14:15.770 回答