播放视频时,avplayer没有声音

时间:2023-01-23 23:25:29

here is all my codes for play a mp4,the mp4 is playing but no sounds out

这是我的所有代码播放mp4,mp4播放但没有声音

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 ?

我确定它有声音,因为我可以听到使用VLC播放mp4时,我是否想念一些东西?

1 个解决方案

#1


55  

Check your phone is silent mode or not. If it is silent try add code below to viewDidLoad:

检查手机是否处于静音模式。如果它是静默的,请尝试将以下代码添加到viewDidLoad:

Swift 2.3

斯威夫特2.3

try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: [])

Swift 3

斯威夫特3

try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [])

#1


55  

Check your phone is silent mode or not. If it is silent try add code below to viewDidLoad:

检查手机是否处于静音模式。如果它是静默的,请尝试将以下代码添加到viewDidLoad:

Swift 2.3

斯威夫特2.3

try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: [])

Swift 3

斯威夫特3

try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [])