我到处搜索,但找不到问题的答案。
我在我的应用程序上播放多种声音,并让用户通过按钮调节音量。所以如果用户愿意的话,他可以用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 音量、静音播放。
有没有办法用按钮来做到这一点?
谢谢!
最佳答案
您将volumeSaved
声明为指向整数而不是整数的指针,然后像整数一样对其进行赋值。这是一个奇怪的现象。
但你真正的问题是 volume
property AVAudioPlayer
中是一个 float
,而不是 int64_t
。因此将类型更改为:
float volumeSaved;
关于iphone - 使用按钮设置 AVAudioPlayer 音量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10588934/