2012-02-28 67 views
0

我已經在我的iPhone應用中實現了AVAudioPlayer,但播放的音頻太響了。爲了使聲音稍微變平,我將音量改爲0.2f,看看它是否會起作用。音量保持完全一樣。以下是我正在使用的代碼:AVAudioPlayer太響了

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Blip.mp3", [[NSBundle mainBundle] resourcePath]]]; 

NSError *error; 
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; 
audioPlayer.numberOfLoops = 0; 
[audioPlayer setVolume:0.2f]; 

if (audioPlayer == nil) 
    NSLog(@"Whoopsies, there was an error with the sound!"); 
else 
    [audioPlayer play]; 

爲什麼這不會降低音量?

有人可以解釋我可以嗎?

謝謝!

回答

1

替換代碼:

[audioPlayer setVolume:0.2f]; 

與下面的代碼:

audioPlayer.volume = 0.2; 

希望這可以幫助你。

+0

我很確定這兩行代碼做同樣的事情。 – gregheo 2012-03-01 04:43:53

+0

是最初嘗試過,但沒有工作:( – 2012-03-01 19:39:24

+0

試試這個嘗試只是用這個替換你的,也許它與NSURL上的用法有關,而不是NSString \t NSString * name = [[NSString alloc] initWithFormat: @ 「光點」]; \t的NSString *源= [[一個NSBundle mainBundle] pathForResource:名稱ofType:@ 「MP3」]; \t如果(audioPlayer){ \t \t [audioPlayer停止]; \t \t audioPlayer =零; \t} \t audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fil eURLWithPath:source] error:NULL]; \t audioPlayer.delegate = self; \t audioPlayer.volume = 0.2; \t [audioPlayer play]; – 2012-03-01 20:01:53

0

正如蘋果Audio & Video Coding How-To's中所解釋的:您無法設置硬件音量。

How do I access the hardware volume controller?
Global system volume, including your application's volume, is handled by iPhone OS and is not accessible by applications.

AVAudioPlayer實例的體積是相對體積。您可以用兩個AVAudioPlayer實例播放兩個聲音,並將它們的音量相對於彼此設置。

編輯:

如果你想控制你的設備的全球體積,你可以使用MPVolumeView

MPVolumeView *myVolumeView = [[MPVolumeView alloc] initWithFrame:mpVolumeViewParentView.bounds]; 
[mpVolumeViewParentView addSubview:myVolumeView]; 
[myVolumeView release]; 
+0

那麼,如何在特定音量下播放聲音 - 與硬件音量不同?有沒有不同的方式來做到這一點? – 2012-02-28 08:24:05

+0

其他應用程序,如TweetBot做到了嗎? – 2012-02-28 08:24:38

+0

我想['MPVolumeView'](https://developer.apple.com/library/ios/#DOCUMENTATION/MediaPlayer/Reference/MPVolumeView_Class/Reference/Reference.html)會幫助你。 – dom 2012-02-28 20:23:51