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
Related
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.
I am developing an iOS app that monitors input every second. This app also produces music at the same time. The input and the output are interfering. Is there a way to prevent the output from being picked up by the input unit?
Have a look at AVAudioSession:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];
[[AVAudioSession sharedInstance] setActive:YES error: &error];
For AVAudioSessionCategoryPlayAndRecord the docs say:
This category is appropriate for simultaneous recording and playback, and also for apps that record and play back but not simultaneously.
I have been trying to do this for a few days now using AVFoundation as well as trying to use MPMoviePlayerViewController. The closest I can get is allowing one to play at a time. I would like to think that this is possible because of Facetime. However, I know this is a little different because there is no separate video file.
Any ideas would help, and thanks.
I'm not sure where this is documented, but to get AVCaptureVideoPreviewLayer and MPMoviePlayerViewController to play together at the same time you need to set a mixable audio session category first.
Here's one way to do that:
AVAudioSession* session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
UInt32 mixable = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(mixable), &mixable);
[session setActive:YES error:nil];
See the Audio Session Programming Guide and Audio Session Cookbook for more info.
Have you tried to play video on one thread and recording video on another? That would allow both of them to run while maintaining their separation.
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.
I am unable to adjust volume of sounds in my app when testing on new iPhone 4 (tried two devices) it works on an iPad and my colleague just told me that on 3Gs with iOS 4 the volume goes down when trying to increase ... that's something doesn't make sense at all.
I am using AudioToolbox/AudioServices.h to just play short sounds in a game that are Wav format. It definitely looks like that the software version affects that a lot but I have no idea how to fix it ... anyone? :)
Thanks,
Ondrej
Hi I was having the same problem, I found out it's because I hadn't set the Session category:
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];
if (setCategoryError) { //handle error }
you can find out more about sessions Here: http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Cookbook/Cookbook.html#//apple_ref/doc/uid/TP40007875-CH6-SW6