here is all my codes for play a mp4,the mp4 is playing but no sounds out
import UIKit
import AVFoundation
class ViewController: UIViewController {
@IBOutl
For Swift 4.2, put following lines in viewDidLoad
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
}
catch {
print("Setting category to AVAudioSessionCategoryPlayback failed.")
}
Its working 100% please try this import AVKit , import AVFoundation and put below code in viewDidLoad() Method.
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, mode: AVAudioSessionModeDefault, options: [])
}
catch {
print("Setting category to AVAudioSessionCategoryPlayback failed.")
}
Check if the iPhone has silent mode engaged, it is a button above the 2 top left buttons. A toggle switch that allows silent mode.
Check your phone is silent mode or not. If it is silent try add code below to viewDidLoad
:
Swift 2.3
try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: [])
Swift 3
try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [])
Swift 4.2
try! AVAudioSession.sharedInstance().setCategory(.playback, options: [])
Or just
try! AVAudioSession.sharedInstance().setCategory(.playback)
I had multiple view controllers that needed to play audio even when the device what set to mute. So instead of updating every single viewDidLoad
, I added the following to the didFinishLaunchingWithOptions
method in the AppDelegate.swift file.
try? AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback)
Notes:
AVFoundation
try?
.