I am attempting to create an iOS application that takes various audio files from the Files app and plays them inside the app using the AudioKit framework. However, from what I have researched, AudioKit's file API appears to be incompatible with external URLs. Does anybody know any alternative music-based frameworks or methods, that can play audio files that are external URL based?
This is the another way. We can play audio with AVPlayer without download. No need to download the audio, with streaming we can play.
Check below code.
let url = URL(string:"https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3")
let playerItem:AVPlayerItem = AVPlayerItem(url: url!)
let player = AVPlayer(playerItem: playerItem)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = CGRect(x:0, y:0, width:200, height:200)
self.view.layer.addSublayer(playerLayer)
player.volume = 1.0
player.play()
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 have the following code which is not working with the mov file, but it works when i use an MP4 file.
The important thing about to mov file is because it has an alpha channel.
I'm open to find solutions that offers me an alpha channel as mov file does.
class MovViewController: UIViewController {
var avPlayer: AVPlayer!
override func viewDidLoad() {
super.viewDidLoad()
let filepath: String? = Bundle.main.path(forResource: "animacion_logo", ofType: "mov")
let fileURL = URL.init(fileURLWithPath: filepath!)
let player = AVPlayer(url: fileURL)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
// self.view.layer.addSublayer(playerLayer)
self.view.layer.insertSublayer(playerLayer, at: 0)
player.play()
}
}
This is the original mov file: https://ufile.io/jcbfn
AVPlayer do not support ProRes format with alpha:
Using AVPlayer to view transparent video
But there is some options out there. Take a look at this Medium post:
https://medium.com/#quentinfasquel/ios-transparent-video-with-coreimage-52cfb2544d54
You can play H.264 or H.265 (on newer iOS hardware) on iOS, but these formats do not support an alpha channel. There is no built in support for alpha channel, you will need to use a 3rd party library, the other option is a series of PNG images or decoding WebM, but both of these options will consume boat loads of CPU resources.
I am trying to play URL audio with AVPlayer. When I put a URL with .mp3 extension, it's working fine. but when I used a URL with no extension. it's not worked. Here's URL
Here's Code.
let url = URL(string: "https://redirector.googlevideo.com/videoplayback?ipbits=0&mn=sn-xcvoxoxu-aixe%2Csn-npoeen7k&ip=117.53.42.8&mm=31%2C29&ms=au%2Crdu&mv=m&mt=1547352278&id=o-AIUfw7X-7-vO4ebqBskvYlVvVmhmgp9WJBItzTwZ7c51&keepalive=yes&pl=24&source=youtube&fvip=6&requiressl=yes&sparams=clen%2Cdur%2Cei%2Cgir%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Ckeepalive%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csource%2Cexpire&itag=250&gir=yes&clen=4252061&txp=5511222&dur=490.541&lmt=1540081943361780&ei=G7k6XNuzA9TNVv6RjvgI&expire=1547373947&c=WEB&key=yt6&mime=audio%2Fwebm&initcwndbps=143750&signature=774AA79C717B4D9AC6A68743C5730CCBF6A8B9B4.0D91F9D2F80CF12A6623D260B9FC5BB3E69BBAE1&title=Coke-Studio-Season-10-Latthay-Di-Chaadar-Quratulain-Balouch-Farhan-Saeed")
let playerItem:AVPlayerItem = AVPlayerItem(url: url!)
player = AVPlayer(playerItem: playerItem)
if player?.rate == 0
{
player.play()
} else {
player.pause()
}
Is there any other way to play audio in iOS with URL's that do not contains .mp3 or any other extension. I don't know why the player is not playing the audio file, there is no sound output on the device and there is no error shown.
I am making a small website for video viewing as a project,
I need to be able to play a video using the local player of the phone, in Android i managed to find the solution:
intent://localhost/video.avi#Intent;action=android.intent.action.VIEW;scheme=http;type=video/mp4;end
This tells the Android OS to open this file using a media player, I need something like this for iOS.
I couldn't find anything on the internet about this, all other posts deal with making an actual application, I don't have access to an iPhone making this even harder to test and play around with it.
How can I do this for iPhone? if not possible are there alternatives?
Notice the code is a mere URL and needs absolutely no further implementations
There's URL Schemes provided by Apple to launch different native stuff via links.
try this: videos://"your video url here"
as an alternative: youtube://"youtube video url here"
About Apple iPhone URL schemes:
https://developer.apple.com/library/content/featuredarticles/iPhoneURLScheme_Reference/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007899-CH1-SW1
Building an iOS application you can use AVFoundation framework, it has AVPlayer (samples are in Swift):
let videoUrl = NSURL(string: "video url here")
let videoPlayer = AVPlayer(URL: videoUrl!)
let playerLayer = AVPlayerLayer(player: videoPlayer)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
you can also use AVPlayerViewController class for it:
let videoUrl = NSURL(string: "video url here")
let videoPlayer = AVPlayer(URL: videoUrl!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
if let player = playerViewController.player {
player.play()
}
}
or you can use a UIWebView to play video from a player within a web page:
let webview = UIWebView(frame: CGRect(x: 0, y: 0, width: 240, height: 375))
I'm using AVFoundation's AVPlayer for streaming external mp3 files. I have a counter on the back-end that counts how many times a file loaded. The only client for this service is only me and whenever I trigger to play the AVPlayer, the counter increases two which means AVPlayer makes the request twice. Is there a reason for this, or how can I prevent that from happening? Here is my code:
#IBAction func listen(sender: UIButton) {
let urlstring = "http://api.server.com/endpoint-to-mp3"
let url = NSURL(string: urlstring)
let playerItem = AVPlayerItem(URL: url!)
let player = AVPlayer(playerItem: playerItem)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = CGRectMake(0, 0, 300, 50)
self.view.layer.addSublayer(playerLayer)
player.volume = 1.0
player.play()
}
AVPlayer is making a network request to the URL whenever you initialize the player with AVPlayerItem. This call only fetches the file information and file size. (At this point I am able to observe 2 requests sometime, which could increase your count to 3)
Later when you are attaching the player to any view, another call is happening to fetch the complete file. (You can use Charles to observe your network traffic, fyi)
This behaviour is same when you init the player with init(url:) So I don't see any way that could prevent this from happening at the moment.