iphone - 使用按钮设置 AVAudioPlayer 音量

标签 iphone ios avfoundation 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 音量、静音播放。

有没有办法用按钮来做到这一点?

谢谢!

最佳答案

您将volumeSaved声明为指向整数而不是整数的指针,然后像整数一样对其进行赋值。这是一个奇怪的现象。

但你真正的问题是 volume property AVAudioPlayer 中是一个 float,而不是 int64_t。因此将类型更改为:

float volumeSaved;

关于iphone - 使用按钮设置 AVAudioPlayer 音量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10588934/

相关文章:

iphone - AVAudioPlayer:声音重叠

iphone - 游戏中心问题

ios - TSTableView swift - "Unrecognized selector sent to instance XXXX"

ios - Parse.com 如何在 iOS 客户端中创建双向关系?

ios - 如何为我在 Swift 中以编程方式创建的 UIViewController 子类创建 init?

ios - 如何使用 AVCaptureDeviceDiscoverySession 获取前置摄像头、后置摄像头和音频

iphone - iOS 设置.plist

iphone - 从 UIView 推送 UIViewController

iphone - UIView 帧序列动画

ios - 在 AVFoundation 中进行帧间视频压缩的方法