Loop Background Music

How to loop background music without lag/delay when song repeats (seamless)

You can use AVPlayerLooper and AVQueuePlayer to do this.

import UIKit
import AVFoundation

class ViewController: UIViewController {

var queuePlayer = AVQueuePlayer()
var playerLooper: AVPlayerLooper?

override func viewDidLoad() {
super.viewDidLoad()

guard let url = Bundle.main.url(forResource: "Gamescene(new)", withExtension: "mp3") else { return }
let playerItem = AVPlayerItem(asset: AVAsset(url: url))
playerLooper = AVPlayerLooper(player: queuePlayer, templateItem: playerItem)
queuePlayer.play()
}
}

How to make music autoplay and loop in background


  • The loop attribute makes the audio file loop.

  • The autoplay attribute makes the file start playing without the user
    needing to play the file.

  • The controls attribute shows the controls, omitting it will hide the
    controls.

<audio loop autoplay>
<source src="path/to/file" type="audio/filetype">
</audio>

Audio filetype can be: .mp3, .wav, .ogg

Loop background music?

You can use the AVAudioPlayer:

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"mySound" ofType:@"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = -1; //infinite

[player play];

and you can jump through by setting the currentTime:

player.currentTime = 10; //jump to 10th second

How to loop through background music at specific time in SwiftUI

Just play it first time and then loop with

'AudioPlayer.currentTime += 30'

How to play a background music and make it loop?

If this is not XNA, you can reference System.Media and do this:

SoundPlayer sound = new SoundPlayer("path");`
sound.PlayLooping();

For XNA you would do something like this:

SoundEffect bgEffect;
bgEffect = Content.Load<SoundEffect>("EpicUnease");
SoundEffectInstance instance = bgEffect .CreateInstance();
instance.IsLooped = true;
bgEffect.Play(0.1f, 0.0f, 0.0f);

For more information, check out this msdn article: http://msdn.microsoft.com/en-us/library/dd940203.aspx



Related Topics



Leave a reply



Submit