Xingou Xingou - 1 month ago 5
iOS Question

avplayer has no sounds when play video

here is all my codes 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() {
// 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.playerLayer.player?.volume = 1

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


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: [])