菜鸟教程小白 发表于 2022-12-11 18:21:45

ios - 同时播放多首歌曲


                                            <p><p>我有 <strong><code>five.caf</code></strong> 文件(具有不同音乐家的声音),我想同时播放多个文件。为此,我使用了 <code>AVAudioPlayer</code> 类。我创建了五个 <code>AVAudioPlayer</code> 实例并设置了 url 并播放歌曲。但它没有正确同步。看起来一两个音乐家的声音会延迟一秒钟。 <strong>如何同时同步所有歌曲并播放。</strong></p>

<p>下面是我的代码:</p>

<pre><code>// Song1
NSString *FilePath = GetDocPath], SongFileName];
NSString *url = ;
player = [ initWithContentsOfURL: error:nil];
;
;
player.numberOfLoops = 0;

// Song2
NSString *url1 = ;
player1 = [ initWithContentsOfURL: error:nil];
;
;
player1.numberOfLoops = 0;

// Song3
NSString *url2 = ;
player2 = [ initWithContentsOfURL: error:nil];
;
;
player2.numberOfLoops = 0;

// Song4
NSString *url3 = ; // ALT.caf
player3 = [ initWithContentsOfURL: error:nil];
;
;
player3.numberOfLoops = 0;
NSLog(@&#34;player3: %f&#34;, player3.duration);

// Song5
NSString *url4 = ;
player4 = [ initWithContentsOfURL: error:nil];
;
;
player4.numberOfLoops = 0;

; // Band
; // Song1
; // Song2
; // Song3
; // Song4
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>创建一个并发调度队列,并在调度队列的并发分支中播放文件</p>

<pre><code>dispatch_queue_t parallelQueue = dispatch_queue_create(&#34;com.unique.name.queue&#34;, DISPATCH_QUEUE_CONCURRENT);

dispatch_async(parallelQueue, ^{
    ;
});
dispatch_async(parallelQueue, ^{
    ;
});
dispatch_async(parallelQueue, ^{
   ;
});
dispatch_async(parallelQueue, ^{
   ;
});
dispatch_async(parallelQueue, ^{
   ;
});
</code></pre>

<p>希望这会有所帮助。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 同时播放多首歌曲,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41235312/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41235312/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 同时播放多首歌曲