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
Decode Base64Url to Base64 -- Swift
Pixel Array to Uiimage in Swift
Xcode - Mpnowplayinginfocenter Info Is Not Displayed on iOS 8
Xcode 9 - Failed to Emit Precompiled Header
Getting Client Certificate to Work for Mutual Authentication Using Swift 3 and Alamofire 4
How to Go from Skscene to Uiviewcontroller by Code
Firebase Undefined Symbols for Architecture X86_64
Open Mobile Safari from a Link in a Webview
What Is "Error in _Connection_Block_Invoke_2: Connection Interrupted" in iOS
Getting a Normal Looking Unicode Down Arrow in a Uilabel Like This ⬇
Using JSONencoder to Encode a Variable with Codable as Type
Nsdata Won't Accept Valid Base64 Encoded String
How to Add Different Types Conforming to a Protocol with an Associated Type to a Collection
Posting Photo to Facebook Fan Page via iOS App by Regular Non-Admin Users
How to Add the Wechat API to a Swift Project
Ios: Detect If the Device Is iPhone X Family (Frameless)
iOS 11 - Keyboard Height Is Returning 0 in Keyboard Notification