Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to know how to add background music in Swift for a project Im working on
You can use the AVAudioPlayer:
let soundFilePath = NSBundle.mainBundle().pathForResource("mySound", ofType: "mySound")
let soundFileURL = NSURL(fileURLWithPath: soundFilePath!)
let player = AVAudioPlayer(contentsOfURL: soundFileURL, error: nil)
player.numberOfLoops = -1 //infinite
player.play()
And don't forget to import AVFoundation in your Project.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm new on Swift. I write because I want to ask a question. Me and my friend we are developing an audio player, but we have a problem. The player also works in background and remote controls from the lockscreen and the control center work, but if music is interrupted by one of these two controls, the play/pause button of our player is not updated with the correct icon.
My question is, how can I make it clear to the player that the music was starting/stopping by one of the remote controls and the player to act accordingly, changing the icon of play/pause button? thanks a lot, I hope I was clear.
You need to use the MPRemoteCommandCenter to do this. For example in your viewControllers viewDidLoad() you can add this:
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.beginReceivingRemoteControlEvents()
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.pauseCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
//Update your button here for the pause command
return .success
}
commandCenter.playCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
//Update your button here for the play command
return .success
}
}
Just change the comments I have included to update your buttons UI. You will also need to import MediaPlayer and MediaPlayer.framework if you have not done so already.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to use this one also when app is on background, Tried to add that in capabilities -> background -> play audio. And also checked that the plist have this line.
But that doesn't work. What am I missing? Can I only play regular audio?
Add on target's info.plist the key "Required background modes" to "App plays audio or streams audio/video using AirPlay".
Configure the audio session in your AppDelegate:
NSError *error = NULL;
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:&error];
if(error) {
// Do some error handling
}
[session setActive:YES error:&error];
if (error) {
// Do some error handling
}
Don't forget to import AVFoundation.h
Credits to #nicu
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
AVAudioPlayer: How to Change the Playback Speed of Audio?
I want to control the speed of the audio played by AVAudioPlayer, So is it possible to control the speed of the audio from slider controller?
I got solution with the help of OpenAL only. With the help of SimpleAudioEngine n CDAudioManager I have done it. referance link : http://www.learn-cocos2d.com/api-ref/1.0/CocosDenshion/html/interface_simple_audio_engine.html
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
2 mp3 sounds at the same time in iphone?
Hi i am creating test application where i am using two mp3 audio file.One for the background music and second file play when user clicked the button.I am using AVAudioPlayer for both and the background music is playing in appDelegate class but i am handling it from the ViewController class.Please if anybody know about this problem please help me out.
You cannot play two compressed formats simultaneously. Try converting one of the files to AIFF, CAF or some other uncompressed format.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Can i see if a song exists on the phone? This is for when i'm loading up the app, so i can see if they've removed the song or not.
Are you wanting to check this based on a song that they've selected before whilst your app is running or based on song title and artist?
If it's based on a media item that they've selected from their iPod library in the past whilst using your app then you can identify a song based on it's MPMediaItemPropertyPersistentID and do an MPMediaQuery the next time you load and do a check:
//Use the MPMediaItemPropertyPersistentID to find the song
MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:savedPersistenID forProperty:MPMediaItemPropertyPersistentID];
MPMediaQuery *songQuery = [[MPMediaQuery alloc] init];
[songQuery addFilterPredicate: predicate];
if (songQuery.items.count > 0) {
//song exists
}
Note: I haven't actually run this code, just looked at the documentation and some example pieces of code that I found. Hopefully it's along the lines of what you're wanting, but I haven't actually tested it or used the code myself.
Also, having looked into the MPMediaItem Documentation it would seem that you could do a search based off artist and title etc. Please look at the MPMediaItem class reference here, for further details on what you can search with. But if you want to search based on an exact song that has been selected in the past then it would be more reliable basing it off the MPMediaItemPropertyPersistentID.