菜鸟教程小白 发表于 2022-12-12 18:29:46

ios - 使用 AudioServicesPlaySystemSound 一次播放 1 个声音


                                            <p><p>我正在阅读有关使用 AudioServicesPlaySystemSound 一次播放一种声音的堆栈溢出文章。 </p>

<p> <a href="https://stackoverflow.com/questions/1512121/stop-sound-in-iphone/8379975#8379975" rel="noreferrer noopener nofollow">stop sound in iPhone</a> </p>

<p>我遇到了同样的问题,使用以下代码,当您在另一个按钮停止播放之前按下一个按钮时,第二个声音会在第一个声音的顶部播放,这样两个声音就会重叠。 </p>

<pre><code>-(IBAction)button1:(id)sender{
SystemSoundID soundID;   
NSString *soundFile = [   
                     pathForResource:@&#34;sound1&#34;
                     ofType:@&#34;wav&#34;];
AudioServicesCreateSystemSoundID((CFURLRef),   &amp;soundID);
AudioServicesPlaySystemSound(soundID);
;
}

-(IBAction)button2:(id)sender{
SystemSoundID soundID;   
NSString *soundFile = [   
                     pathForResource:@&#34;sound2&#34;
                     ofType:@&#34;wav&#34;];
AudioServicesCreateSystemSoundID((CFURLRef), &amp;soundID);
AudioServicesPlaySystemSound(soundID);
;
}
</code></pre>

<p>根据苹果的<a href="http://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/SystemSoundServicesReference/Reference/reference.html" rel="noreferrer noopener nofollow">documentation</a> :</p>

<p><code>AudioServicesPlaySystemSound</code> 应该一次只播放一种声音。</p>

<p>另外,当你使用<code>AudioServicesPlaySystemSound</code>功能时:</p>

<ul>
<li>以当前系统音量播放声音,没有可用的程序化音量控制</li>
<li>声音立即播放</li>
<li>循环和立体定位不可用</li>
<li>无法同时播放:<em>一次只能播放一种声音</em></li>
</ul>

<p>所以我不明白为什么可以同时播放多个声音。</p>

<p>我尝试使用 <code>AudioServicesDisposeSystemSoundID</code>,但声音仍然重叠,我做错了吗?</p>

<pre><code>-(IBAction)sound2:(id)sender{
AudioServicesDisposeSystemSoundID
SystemSoundID soundID;   
NSString *soundFile = [   
                     pathForResource:@&#34;sound2&#34;
                     ofType:@&#34;wav&#34;];
AudioServicesCreateSystemSoundID((CFURLRef), &amp;soundID);
AudioServicesPlaySystemSound(soundID);
;
}
</code></pre>

<p>目前,我正在使用 <code>AVAudioPlayer</code>,但声音开始延迟,如果可能,我想使用 <code>AudioServicesPlaySystemSound</code>,因为它会立即播放声音。</p>

<p>请问有其他人遇到过同样的问题吗?</p>

<p>非常感谢您的宝贵时间。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>尝试使用 <code>AudioServicesDisposeSystemSoundID</code>。 </p>

<p>这只是解决您问题的解决方案。
如果需要再次播放,则必须重新创建声音。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 使用 AudioServicesPlaySystemSound 一次播放 1 个声音,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/8456731/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/8456731/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 使用 AudioServicesPlaySystemSound 一次播放 1 个声音