【问题标题】:Volume on my device doesn't effect the volume我设备上的音量不影响音量
【发布时间】:2014-07-07 05:21:46
【问题描述】:

.h

SystemSoundID sound1;

.m

NSURL *soundURL1 = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"Woosh" ofType:@"mp3"]];

AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL1, & sound1);

AudioServicesPlaySystemSound(sound1);

这是我用来播放 Woosh.mp3 声音的唯一代码。我在 iPad 上运行该应用程序,当我更改设备上的音量时,声音的音量没有改变。我想知道是否有另一种方法可以在应用程序中实现声音,或者以这种方式添加改变音量的功能。

【问题讨论】:

  • 要播放音频文件,您可以使用 audioplayer 控制器或 MPMoviePlayerController,这样您也可以在其中管理音量

标签: ios audio volume


【解决方案1】:
NSURL *soundurl   = [[NSBundle mainBundle] URLForResource: @"mysound" withExtension: @"caf"];
AVAudioPlayer *mySoundPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:soundurl error:&error];
mySoundPlayer .volume=1.0f; //between 0 and 1
[mySoundPlayer prepareToPlay];
mySoundPlayer.numberOfLoops=0; //or more if needed
[mySoundPlayer play];

【讨论】:

  • 感谢这个方法有效,但是在第二行的末尾你输入了“&error”,这一直在 Xcode 上显示一条错误消息,所以我将它更改为“nil”,它似乎运行良好现在。
  • 你也可以写 nil 或者你可以写 &error 并且首先你需要声明 "NSError* error = nil;" .这是为了在分配 AvAUdioPlayer 对象时发生错误
猜你喜欢
  • 1970-01-01
  • 2011-08-12
  • 1970-01-01
  • 1970-01-01
  • 2012-05-04
  • 1970-01-01
  • 2011-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多