I need to implement music player using AV Player in SWIFT - swift

I need to implement streaming music player using AV Player in SWIFT
that music file Stored in server.Please help me to any one..How to implement this one

According to this: http://www.techotopia.com/index.php/IOS_8_Video_Playback_using_AVPlayer_and_AVPlayerViewController
You could add following code in your ViewController, like viewDidLoad method or viewWillAppear, depends on the view will show repeatedly or not. If Yes, add it in the viewWillAppear method.
let player = AVPlayer(URL: url)
let playerController = AVPlayerViewController()
playerController.player = player
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
player.play()

Related

Black Screen when stream video with avkit

Hi I want to stream remote video on my app. URL is: http://test.ext/myvideo.mkv
I use AVKit with this code but screen and audio not works. I see only progress bar that go forward.
let player = AVPlayer(url: url)
// Create a new AVPlayerViewController and pass it a reference to the player.
let controller = AVPlayerViewController()
controller.player = player
controller.player?.automaticallyWaitsToMinimizeStalling = false
controller.player?.playImmediately(atRate: 1.0)
// Modally present the player and call the player's play() method when complete.
self.present(controller, animated: true) {
player.play()
}
AVPlayer don't support mkv video format.
Anyone have a solution for playing all video format ?
I've tested MobileVLCKit but it's too heavy (1,5Go)...

How to stream remote audio in iOS 13? (SwiftUI)

This code using AVPlayer works only on Playground
import AVFoundation
var player = AVPlayer()
let playerItem = AVPlayerItem(url: URL(string: "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3")!)
player = AVPlayer(playerItem: playerItem)
player.play()
When I tried to run it on my SwiftUI App on my physical device, using this code:
Button(action:{
var player = AVPlayer()
let playerItem = AVPlayerItem(url: URL(string: "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3")!)
player = AVPlayer(playerItem: playerItem)
player.play()
print("Works")
},label:{
Image("play")
})
It prints Works to the console. However, it does not play any sound on the device.
Would appreciate any help, can't find anything yet here.
Thank you so much!
In SwiftUI, Views are value types. They are only data that describe the things on screen. They can be created or destroyed or copied at any time. AVPlayer is a reference to a specific player object. You're assuming here that it will continue to exist, and there will only be one of them. That's not something that a SwiftUI View provides.
You need to move your AVPlayer outside of the View (into Model objects), and just bind UI actions to it.

Is it possible to make preview image while scroll video timeline like in Netflix app?

Is it possible to make peview image while listing video timeline, like in Netflix App with AVPlayer?
exmple here
I found that mp4 video has that preview window, but m3u8 format has not it
func playVideo(cell: MovieCell) {
guard let urlVideo = URL(string: cell.movieSrc) else { return }
// Create an AVPlayer, passing it the HTTP Live Streaming URL.
let player = AVPlayer(url: urlVideo)
// Create a new AVPlayerViewController and pass it a reference to the player.
let controller = AVPlayerViewController()
controller.player = player
// Modally present the player and call the player's play() method when complete.
self.present(controller, animated: true) {
player.play()
}
}
This is called "Trick Play" and must be included in the HLS manifest for AVPlayer to handle this for you.
See the Trick Play section in HLS Authoring Specification for Apple Devices:
6.1. I-frame playlists (EXT-X-I-FRAME-STREAM-INF) MUST be provided to support scrubbing and scanning UI.

How to add a video in the UI in Swift

I want to be able to add a video (called "Logo-Animation4.mp4") into the UI in Swift, just like you can a UIImage in a UIImageView.
Right now, I've tried just putting an AVPlayer on the view, but it comes up with the default iOS AVPlayer, which contains the regular fullscreen, controls, and volume stuff that I don't want. I just want the video to play in the UI without any way to have the user interact with it.
I've thought about animating the video using a regular UIImageView's animation feature, but my video isn't that short, and it would be hard to get every single frame, and input it into the code.
How should I go about doing this?
To achieve what you are asking, you'll need to use AVPlayerLayer
Add a UIView outlet
#IBOutlet weak var videoView: UIView!
Import AVFoundation
Create player variables
var player : AVPlayer!
var avPlayerLayer : AVPlayerLayer!
Add the following code to your viewDidLoad
guard let path = Bundle.main.path(forResource: "Logo-Animation4", ofType:"mp4") else {
debugPrint("Logo-Animation4.mp4 not found")
return
}
player = AVPlayer(url: URL(fileURLWithPath: path))
avPlayerLayer = AVPlayerLayer(player: player)
avPlayerLayer.videoGravity = AVLayerVideoGravity.resize
videoView.layer.addSublayer(avPlayerLayer)
player.play()
Add this method
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
avPlayerLayer.frame = videoView.layer.bounds
}

How can I play an intro video the first time I launch an iPhone app using swift (NOT objective-c)?

I want to play an intro video for my iPhone app the first time it launches. I have it set up to detect that it's the first launch, but how do I play the video from the app (not a url) using SWIFT? Do I set it up in the first ViewController file or in the AppDelegate? I've tried using MPMoviePlayerController, but I just can't seem to get it to actually play. Thank you for your help!
I had this same trouble. Here's my solution:
func playVideo() {
let path = NSBundle.mainBundle().pathForResource("video", ofType:"mp4")
let url = NSURL.fileURLWithPath(path!)
moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = moviePlayer {
player.view.frame = self.view.bounds
player.controlStyle = .None
player.prepareToPlay()
player.scalingMode = .AspectFit
self.view.addSubview(player.view)
}
}
Don't forget to import MediaPlayer and declare your variable - var moviePlayer: MPMoviePlayerController?
Then call your function when you want to play it playVideo()
See my question for how to dismiss it - In iOS how do I programmatically add a video view and then remove it after it finishes playing using Swift?
I got a really helpful answer there.
Also notice that I disabled my controllers because I don't want it to be seen.