2013-10-28 71 views
7

我剛開始用xCode編寫iOS應用程序,發現事情如何工作並不容易也不直觀。我非常新,並且我的應用程序非常緩慢地^^。無論如何,我現在至少在iOS7上嘗試一些東西。iOS 7:AVAudioPlayer簡單的音頻控制?

我設法創建了具有海關單元格和動態高度的動態表格,但是現在我找不到解決方案來解決我的問題...也許我沒有在正確的地方搜索...無論如何。

我有一個音頻播放,由於這些行:

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

[audio play]; 
[audio updateMeters]; 

現在,這是偉大的,我的音頻播放。但我沒有任何控制。我成功添加了播放/暫停按鈕,但是如何在音頻內部導航?我是否必須編寫所有界面?有沒有一個簡單的界面與按鈕和響應式進度欄?

如果我必須對它進行編碼,那麼,哼哼......我從哪裏開始?

非常感謝!

+0

如果你想按鈕來控制音樂,擦洗滑塊,音量控制等你需要自己和他們的相應方法創建它們。如果你想創建一個滑動滑塊,你可能會想看'NSTimer'。 – sooper

回答

17

在AVAudioPlayer的playAtTime:Method中使用UISlider,AVAudioPlayer沒有內置的搜索欄。

退房此示例代碼,它實現了你在課堂上要什麼avTouchController

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

添加UISLider給您接口和鏈接的valueChanged:該方法seekTime:

。 ħ

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

@property (nonatomic, strong) AVAudioPlayer *audioPlayer; 

@property (nonatomic, strong) NSTimer *updateTimer; 

在viewDidLoad中的.m加載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; 

} 
+0

沒有在您的答案中提供鏈接... – sergio

+0

哎呀,現在補充。這很容易實現,不要陷入所有額外的代碼,儘管(AVAudioSession) – ManicMonkOnMac

+0

哇,是的,這似乎有點難:P ...但我不需要電平表,循環和這些類似的東西。只播放/暫停和響應式「時間線」。我會盡力實現這一點。我希望我能儘快趕到!謝謝 – rekam

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; 

} 
+0

當我們暫停音頻時會發生什麼? –