avplayer has no sounds when play video

后端 未结 5 1870
逝去的感伤
逝去的感伤 2020-12-29 19:44

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         


        
5条回答
  •  囚心锁ツ
    2020-12-29 20:35

    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

提交回复
热议问题