我到處搜索,但找不到我的問題的答案。用按鈕設置AVAudioPlayer音量
我在我的應用程序上播放多個聲音,我讓用戶用一個按鈕調整音量。所以如果用戶想要的話,他可以用0.5音量播放。所以我有這樣的代碼:
NSString *path = [[NSBundle mainBundle] pathForResource:@"Orchestra" ofType:@"mp3"];
theOrchestras = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
[theOrchestras play];
,並降低音量這樣的:
- (IBAction) volumeReduced {
theOrchestras.volume = 0.5;
}
當聲音播放的音量減小,沒有問題。但是當聲音停止並再次播放時,音量總是回到1.
要解決此問題,我嘗試將音量保存爲整數,但由於某些原因該值始終爲0,我無法制作它的工作:
.M
int64_t * volumeSaved;
.H
- (IBAction) volumeReduced {
volumeSaved = .5;
}
NSString *path = [[NSBundle mainBundle] pathForResource:@"Orchestra" ofType:@"mp3"];
theOrchestras = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
theOrchestras.volume = volumeSaved;
[theOrchestras play];
但現在音頻總是0音量,靜音播放。
有沒有辦法用按鈕做到這一點?
謝謝!
謝謝!有用! :d – tomDev