简体   繁体   English

将值发送到AVAudioPlayer对象以播放不同的声音

[英]Sending a Value to an AVAudioPlayer Object to Play a Different Sound

I have an iphone app where I am playing a sound with one button using an AVAudioPlayer object. 我有一个iPhone应用程序,使用AVAudioPlayer对象通过一个按钮播放声音。 However, I would like to use a variable for the sound name and send a value to that variable. 但是,我想使用一个变量作为声音名称,并向该变量发送一个值。 I have two other buttons that play different sounds and would like to send the sound name values to the variable that is in the AVAudioPlayer. 我还有另外两个按钮,它们播放不同的声音,并希望将声音名称值发送到AVAudioPlayer中的变量。 How do I do that? 我怎么做?

Thanks! 谢谢!

you can keep your audio files (in resource) with numeric names such as 1.mp3, 20.mp3, 41.wav etc and then you can use [NSString stringWithFormat:@"%d.mp3",soundId]; 您可以使用数字名称(例如1.mp3、20.mp3、41.wav等)保留音频文件(在资源中),然后可以使用[NSString stringWithFormat:@“%d.mp3”,soundId]; to generate sound file name to use generating absolute resource path and pass it to AVAudioPLayer 生成声音文件名以使用生成绝对资源路径并将其传递给AVAudioPLayer

Best of Luck!! 祝你好运!

This cannot be done. 无法做到这一点。 Create a new AVAudioPlayer for each new sound. 为每个新声音创建一个新的AVAudioPlayer。 If you wish to interrupt the current sound, send stop to the current instance. 如果要中断当前声音,请向当​​前实例发送停止信号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM