avplayer has no sounds when play video

后端 未结 5 1871
逝去的感伤
逝去的感伤 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:19

    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.")
    }
    
    0 讨论(0)
  • 2020-12-29 20:25

    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.")
        }
    
    0 讨论(0)
  • 2020-12-29 20:30

    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.

    0 讨论(0)
  • 2020-12-29 20:33

    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)
    
    0 讨论(0)
  • 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
    0 讨论(0)
提交回复
热议问题