iPhone: AVAudioPlayer unsupported file type

At long last i have found a solution to this problem! Instead of initializing the audio player with the NSData object, I saved the file to the Documents folder, and then initialized the player with the file URL //download file and play from disk NSData *audioData = [NSData dataWithContentsOfURL:someURL]; NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) … Read more

How to play mp3 audio from URL in iOS Swift?

Use AVPlayer instead of AVAudioPlayer to play remote content. As per documentation AVAudioPlayer needs mp3 file to play Audio. AVAudioPlayer not provide support for streaming. Try this code , its working fine for me func play(url:NSURL) { print(“playing \(url)”) do { let playerItem = AVPlayerItem(URL: url) self.player = try AVPlayer(playerItem:playerItem) player!.volume = 1.0 player!.play() } … Read more

iOS 13.1 Crash in AVAudio Player

I found a crash issue in AVAudioPlayer with iOS 13.1. Here Is Solution Why My AVAudioPlayer crash? because I initialise AVAudioPlayer like var wrongMusicPlayer: AVAudioPlayer = AVAudioPlayer() and then i try to reassign wrongMusicPlayer as below wrongMusicPlayer = try AVAudioPlayer(contentsOf: wrongURL) And my app get crash. Solution If you initialise your AVAudioPlayer like var wrongMusicPlayer: … Read more

iOS background audio not playing

add a key named Required background modes in property list (.plist) file .. as following picture.. may you get help.. and add following code in AppDelegate.h #import <AVFoundation/AVFoundation.h> #import <AudioToolbox/AudioToolbox.h> AppDelegate.m in application didFinishLaunchingWithOptions [[AVAudioSession sharedInstance] setDelegate:self]; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; [[AVAudioSession sharedInstance] setActive:YES error:nil]; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; UInt32 size = sizeof(CFStringRef); CFStringRef route; AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, … Read more

How do I start playing audio when in silent mode & locked in iOS 6?

You need to make couple of changes in plist file. i.e. 1) Set Required background mode to App plays audio 2) set Application does not run in background to YES. NSError *setCategoryErr = nil; NSError *activationErr = nil; [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr]; [[AVAudioSession sharedInstance] setActive:YES error:&activationErr]; Then, you need to write these much code … Read more

Playing a sound with AVAudioPlayer

Made modification to your code: import UIKit import AVFoundation class ViewController: UIViewController { var audioPlayer = AVAudioPlayer() override func viewDidLoad() { super.viewDidLoad() var alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(“button-09”, ofType: “wav”)) println(alertSound) // Removed deprecated use of AVAudioSessionDelegate protocol AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil) AVAudioSession.sharedInstance().setActive(true, error: nil) var error:NSError? audioPlayer = AVAudioPlayer(contentsOfURL: alertSound, error: &error) audioPlayer.prepareToPlay() audioPlayer.play() } } … Read more

Detecting the iPhone’s Ring / Silent / Mute switch using AVAudioPlayer not working?

Well I found the answer thanks to someone from the Developer Forums, and you guys aren’t gonna like it! I’ve had a response from Apple on this. They’ve said they don’t and never have provided a method for detecting hardware mute switch and don’t intend to do so. 🙁 IMO there is definitely value in … Read more

Play music in the background using AVAudioplayer

I had solved this question by referring iOS Application Background tasks and make some changes in .plist file of our application.. Update write this code in your controller’s view did load method like this – (void)viewDidLoad { NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@”in-the-storm” ofType:@”mp3″]]; AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback … Read more