Hy, I'm trying to understand if there is a way to get sound from the player even when your iPhone is set to silent .. Is there a Swift code to enable the Tone?
//IMPOSRT FOUNDATION PER PLAYER
import AVFoundation
let url = NSURL(string: "https://mywebsite.site/file/\(audio_r!)")
playerItem = AVPlayerItem(URL: url!)
player = AVPlayer(playerItem: playerItem!)
let playerLayer = AVPlayerLayer(player: player!)
playerLayer.frame = CGRectMake(0,0,300,50)
self.view.layer.addSublayer(playerLayer)
//imposto il volume
player!.muted = false
player!.volume = 1.0
Someone can help me?
A playback category should not obey the mute switch
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
AVAudioSession.sharedInstance().setActive(true)
Related
guys
I am making a project that plays video using Swift AVPlayer.
There is a problem that the video stops, or there is only sound but no video.
When a video URL is received and played through AVPlyer, the file extension is HLS (.m3u8)
Same issue with both AVPlayer and Safari. However, it plays normally in Chrome.
Is it a video file problem? Or is there something the app needs to handle?
This is the sample video URL I am using
https://demo.unified-streaming.com/k8s/features/stable/video/tears-of-steel/tears-of-steel.ism/.m3u8
https://devstreaming-cdn.apple.com/videos/streaming/examples/img_bipbop_adv_example_fmp4/master.m3u8
http://d3rlna7iyyu8wu.cloudfront.net/skip_armstrong/skip_armstrong_stereo_subs.m3u8
self.playerItem = AVPlayerItem(url: URL(string: urlString)!)
self.playerItem?.addObserver(self, forKeyPath: #keyPath(AVPlayerItem.status), options: [.initial, .new], context: &self.playerItemContext)
self.playerItemObserver = true
if self.player == nil {
self.player = AVPlayer(playerItem: self.playerItem)
self.playerLayer = AVPlayerLayer(player: self.player)
self.player?.play()
return
}
I am using the above code to insert the url and play it.
I'm using MPMoviePlayerController to play avi formatted video files.
The audio works, but the video doesn't. Why is that?
MPMoviePlayerViewController only supports .mov, .mp4,.mpv, and .3gp and is deprecated.
From Apple's documentation:
The MPMoviePlayerViewController class is formally deprecated in iOS 9.
(The MPMoviePlayerController class is also formally deprecated.) To
play video content in iOS 9 and later, instead use the
AVPictureInPictureController or AVPlayerViewController class from the
AVKit framework, or the WKWebView class from WebKit.
You should use AVPlayerViewController:
import AVFoundation
import AVKit
let videoPath = "https://video.avi"
let videoURL = URL(string: videoPath)
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
present(playerViewController, animated: true) {
playerViewController.player?.play()
}
or just add AVPlayer in as a subview:
let videoURL = URL(string: videoPath)
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
view.layer.addSublayer(playerLayer)
player.play()
it supports the following audio/video formats:
.mpeg, .mpeg-2-video, .avi, .aifc-audio, .aac-audio, .mpeg-4, .au-audio, .aiff-audio, .mp2, .3gpp2, .ac3-audio, .mp3, .mpeg-2-transport-stream, .3gpp, .mpeg-4-audio
I think you cannot play .avi file using MPMoviePlayerController in ios.
As .avi format contents are not possible to decode the video on a iOS device.
I am trying to play videos in my app, but when I selected larger files from data that I download from Dropbox, I get a crossed out play button, and can't see or play my video.
I understand that there must be a couple of reasons why the player might disable itself, but is there a way to get feedback from the player to see what it might be complaining about?
This is my code:
let path = URL.init(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
let url = path.appendingPathComponent("temp.mov")
do {
try data.write(to: url, options: .atomic)
} catch let error {
print("WRITING: \(error)")
}
player = AVPlayer(url: url)
let playerController = AVPlayerViewController()
playerController.player = player
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = videoView.frame
player.play()
Thanks!
JK
Im trying to display a local recorded video in a AVPlayerLayer which works sometimes. I can hear the audio from the recorded video but can't see the video. Sometimes both video and audio is working, sometimes only audio.
I've tried both with a AVPlayerLayer and AVPlayerViewController but the same issue occurs in both cases. So it's not because of the frames being wrong.
Example code AVPlayerViewController:
let player = AVPlayer(url: url)
let playerController = AVPlayerViewController()
playerController.player = player
self.present(playerController, animated: true) {
player.play()
}
Example code AVPlayerLayer:
let asset = AVAsset(url: url)
let item = AVPlayerItem(asset: asset)
self.player = AVPlayer(playerItem: item)
self.player?.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions(), context: nil)
self.playerLayer = AVPlayerLayer(player: self.player!)
self.playerLayer?.frame = imageView.bounds
imageView.layer.addSublayer(self.playerLayer!)
Edit 1:
When observing the status of the player, error is nil and the status is readyToPlay
Edit 2:
Works fine if the URL is remote.
Edit 3:
Seems to work if I wait a couple of seconds after the video has completed the export. Could it be something to have with the file not 100% written to the filesystem?
Edit 4:
Video of the problem, in this case it played the 3rd time.
Here's how I set a AVPlayerLayer with the video working (I think what you're missing is the videoGravity parameter).
let bundle = Bundle.main
let moviePath = bundle.path(forResource: "myVideo", ofType: "mp4")
let moviePlayer = AVPlayer(url: URL(fileURLWithPath: moviePath!))
playerLayer = AVPlayerLayer(player: moviePlayer)
playerLayer.frame = movieView.bounds
playerLayer.videoGravity = AVLayerVideoGravityResizeAspect
movieView.layer.addSublayer(playerLayer)
playerLayer.player?.play()
It's the frame height or width is equal to zero
i have the same issues as you did. the reason is in iOS 10.xx , if you export video with animationTool .
You will meet the trouble like that .
try to fix them by remove this code .
something like that
mainComposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer, in: parentlayer)
Hope to help you
What caused this issue for me was I was changing assets to play a new video. The problem was I was reinitializing the same AVPlayer and setting setting it to the playerLayer which was previously set
Incorrect
player = AVPlayer()
playerLayer = AVPlayerLayer(player: player)
// ...
player?.replaceCurrentItem(with: playerItem)
Correct
if player == nil {
player = AVPlayer()
playerLayer = AVPlayerLayer(player: player)
// ...
}
player?.replaceCurrentItem(with: playerItem)
Or better yet I should've just called this by itself
player?.replaceCurrentItem(with: playerItem)
I am using AVPlayer to play a video. But I can not set the Layer of video to be on full screen on iPhone 4s. Is there any difference on iPhone 4s?
Here is what I do:
var playerItem = AVPlayerItem(URL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("demo_video", ofType: "mp4")!))
var player = AVPlayer(playerItem: playerItem)
var playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.frame
self.view.layer.addSublayer(playerLayer)
Thanks
Your video might not be in the frame of the whole screen. Add background color to the PlayerLayer and see if it is full screen.