菜鸟教程小白 发表于 2022-12-11 20:22:06

iphone - 如何在 ios5 中播放本地方法的 AVAudioPlayer?


                                            <p><p>我认为它与 iOS 5 中的 ARC 有关,但是当我在方法中声明一个 AVAudioPlayer 并播放它时,它不会播放。我认为它会在方法结束时自动释放,因为我不记得在 iOS5 之前发生过这种情况。如果我将其设为全局变量,则它可以正常工作。有什么方法可以使用本地实例?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>将其设为保留属性,并将您的类设为委托(delegate),以便在播放完毕后进行清理。在您的标题中:</p>

<pre><code>@interface MyClass : NSObject &lt;AVAudioPlayerDelegate&gt;
@property(strong)AVAudioPlayer *player;
</code></pre>

<p>那么,当你使用它时:</p>

<pre><code>self.player = initWithContentsOfURL:URL error:&amp;err];
;
</code></pre>

<p>并在委托(delegate)方法中清理它:</p>

<pre><code>-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)playedSuccessfully {
    self.player = nil;
}
</code></pre>

<p>将它作为属性的一个好处是,如果需要,您可以中断它:</p>

<pre><code>-(void)someInterruptionOccurred {
    ;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - 如何在 ios5 中播放本地方法的 AVAudioPlayer?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/7810936/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/7810936/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - 如何在 ios5 中播放本地方法的 AVAudioPlayer?