2010-07-16 20 views
18

在當前正在播放的AVPlayer項目上獲取音量更改的唯一方法是遵循此過程;調整正在播放的AVPlayer的音量

  • 卸載當前播放AVPlayerItem的資產
  • 抓住當前播放時間爲AVPlayerItem
  • 裝入資產成新AVPlayerItem
  • 用新
  • 等待替換當前AVPlayerItem在AVPlayer上更改當前項目
  • 準備您的AudioMix並尋找之前的播放時間

我是否錯過了某個基本原理,或者是否僅僅是爲了管理音量級別?

我無法使用AVAudioPlayer,因爲我需要將iTunes曲目加載到播放器中。

+0

AVPlayer是第三方框架/應用程序嗎? – alexy13 2010-11-12 21:55:33

+0

@Tyler:AVAudioPlayer不是這裏的問題...我們正在討論AVPlayer,因爲AVAudioPlayer無法處理ipod庫的URL – 2011-11-18 10:06:26

回答

21

您可以在使用這裏所描述的方法打更改音量:

http://developer.apple.com/library/ios/#qa/qa1716/_index.html

雖然文章的文字似乎表明,它只能用來靜音音頻,實際上你可以設置將音量設置爲您喜歡的任何音樂,並且您可以在音頻播放後進行設置。例如,假設您的AVAsset實例被稱爲「資產」,您的AVPlayerItem實例稱爲「playerItem」,並且您要設置的音量稱爲「音量」,下面的代碼應該做你想要的:

NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio]; 

NSMutableArray *allAudioParams = [NSMutableArray array]; 
for (AVAssetTrack *track in audioTracks) { 
    AVMutableAudioMixInputParameters *audioInputParams = 
    [AVMutableAudioMixInputParameters audioMixInputParameters]; 
    [audioInputParams setVolume:volume atTime:kCMTimeZero]; 
    [audioInputParams setTrackID:[track trackID]]; 
    [allAudioParams addObject:audioInputParams]; 
} 

AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix]; 
[audioMix setInputParameters:allAudioParams]; 

[playerItem setAudioMix:audioMix]; 
+0

感謝您的鏈接。我在該頁面的底部發現了一條非常重要的信息。 「注意:AVAudioMix只支持基於文件的資產。「我無法弄清楚爲什麼我的淡入淡出,現在我知道了,因爲我最初的測試是使用本地文件,現在它們是流媒體。似乎沒有辦法使用AVPlayer調整流媒體音軌的音量...... ? – SteveB 2011-06-07 17:01:56

+0

有趣的是,它似乎與流媒體一起工作,所以可能還有其他一些因素必須如此。我想在我有空的時候編寫一個簡單的測試應用程序 – 2011-06-13 13:51:22

+0

can你幫我這個代碼'AVPlayer * avPlayer = [[AVPlayer alloc] initWithURL:songURL];'' '[avPlayer play];'當我有這個簡單的線路時,我需要使用這麼長的代碼嗎? – DShah 2011-09-17 06:23:18

4

您是否嘗試過準備AVMutableAudioMix並在AVPlayerItem仍在播放時將其設置?你應該能夠向AVPlayer詢問它的currentItem,它可以提供AVMutableAudioMix的AVMutableAudioMixInputParameters應該引用的軌跡。您爲混音提供的時間與混音時間有關。

+1

只需設置[[player currentItem] setAudioMix:audioMix]; (從UISlider valueChanged動作)是可以的。 – 2011-11-23 11:26:57

2
- (IBAction)sliderAction:(id)sender { 
NSLog(@"slider :%f ", self.mixerSlider.value); 

NSArray *audioTracks = [self.videoHandler.videoAsset tracksWithMediaType:AVMediaTypeAudio]; 

// Mute all the audio tracks 
NSMutableArray *allAudioParams = [NSMutableArray array]; 
for (AVAssetTrack *track in audioTracks) { 
    AVMutableAudioMixInputParameters *audioInputParams =[AVMutableAudioMixInputParameters audioMixInputParameters]; 
    [audioInputParams setVolume:self.mixerSlider.value atTime:kCMTimeZero]; 
    [audioInputParams setTrackID:[track trackID]]; 
    [allAudioParams addObject:audioInputParams]; 
} 
AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix]; 
[audioMix setInputParameters:allAudioParams]; 

[[self.mPlayer currentItem] setAudioMix:audioMix]; } 

該方法由滑塊值改變調用。你可以在視頻播放時調用它。

0

請注意,AVMutableAudioMix爲基礎的方法在原始問題(和幾個答案)只有描述的基於文件的資產,而不是流媒體。

這蘋果,這裏寫的文檔中詳細說明:

https://developer.apple.com/library/content/qa/qa1716/_index.html

當我試圖做到這一點與音頻流我沒有收到任何曲目從AVAsset的tracks(withMediaType:)方法(SWIFT)返回。

相關問題