47

Here is all my code for play a mp4,the mp4 is playing but no sounds out

import UIKit

import AVFoundation
class ViewController: UIViewController {

@IBOutlet weak var containerView: UIView!

var playerLayer:AVPlayerLayer!
var player:AVPlayer!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let path = NSBundle.mainBundle().pathForResource("huoguo", ofType: "mp4")
    let url = NSURL(fileURLWithPath: path!)
    let asset = AVAsset(URL: url)
    let playerItem = AVPlayerItem(asset: asset)
    self.player = AVPlayer(playerItem: playerItem)
    self.playerLayer = AVPlayerLayer(player: player)
    self.playerLayer.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.width * 9 / 16)
    self.containerView.layer.addSublayer(playerLayer)
    //self.playerLayer.player?.volume = 1
    self.playerLayer.player?.play()
}
}

i'm sure it has sounds,because i can hear when play the mp4 using VLC, did i miss something ?

starball
  • 20,030
  • 7
  • 43
  • 238
Xingou
  • 1,141
  • 1
  • 10
  • 20

6 Answers6

114

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)
Varun
  • 85
  • 1
  • 7
vien vu
  • 4,277
  • 2
  • 17
  • 30
  • 4
    Note to self... make sure that the video you are trying to get to play sounds actually has sounds in it. FFS! Thanks :D – Fogmeister Oct 23 '17 at 13:54
  • 1
    Ok, my phone is not on silent but the audio in the avPlayer is not sounding. It's not about silent mode. I don't want to bypass a users' choice to silence the phone. I just want the audio from an avPlayer to sound. Any other suggestions. – MultiGuy May 24 '23 at 00:08
32

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.")
}

For Swift 5

do {
            try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
           }
           catch {
               print("Setting category to AVAudioSessionCategoryPlayback failed.")
           }
Gang Fang
  • 787
  • 7
  • 14
12

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.

https://www.kproapps.com

Kingsley Mitchell
  • 2,412
  • 2
  • 18
  • 25
  • 1
    I wasted hours on that. I think when you pressing the sound up button and iOS showing sound level but do not creates any sound because of silent mode is misleading. – EmreSURK Feb 05 '20 at 13:00
6

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:

  • import AVFoundation
  • I didn't need any other error catching so I just used try?.
  • I am supporting down to iOS 9. Gang Fang's answer required iOS 10. Only using the single parameter as I did above, though, worked fine for iOS 9.
  • Tested with Swift 5
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
4

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.")
    }
Maulik Patel
  • 2,045
  • 17
  • 24
0

I tried with accepted answer but unfortunately it didn't works for me, failed with below error:

type 'String' has no member 'playback

Then i jump to the definition and found that it takes String value as parameter, use below code if you have same error

        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, mode: AVAudioSessionModeDefault, options: [])
        }
        catch {
            print("Setting category to AVAudioSessionCategoryPlayback failed.")
        }
guru
  • 2,727
  • 3
  • 27
  • 39