mp3 file not streaming however no errors - ios

This file is not playing as an mp3 file, I am not sure where I messed up in my code. The println statement is printed.
let urlString = "http://anything2mp3.com/system/files/mp3/Siberian%20Husky%20gets%20ICE%20POOL%20Extended_q1zY1dp1_ig_youtube.mp3"
#IBAction func tempPlay(sender: AnyObject) {
println("runningtemp")
let url = NSURL(string: urlString)
var avPlayer = AVPlayer(URL: url)
avPlayer.play()
}

You just need to declare your AVPlayer out of your IBAction.
let urlString = "http://anything2mp3.com/system/files/mp3/Siberian%20Husky%20gets%20ICE%20POOL%20Extended_q1zY1dp1_ig_youtube.mp3"
var avPlayer:AVPlayer!
#IBAction func tempPlay(sender: AnyObject) {
if let url = NSURL(string: urlString) {
avPlayer = AVPlayer(URL: url)
avPlayer.play()
}
}

Related

How to play video only in webview not in full screen in swift

I am new in swift and I am facing problem in playing video in WKWebView.
My code is like this
#IBOutlet var youtubeWebView: WKWebView!
func setContentView(homeVideoListModel:HomeVideoListModel?){
//youtubeWebView.configuration.allowsInlineMediaPlayback = true
guard var videoUrl = homeVideoListModel?.videoUrl else {
return
}
videoUrl = videoUrl + "?playsinline=1"
print(videoUrl)
let youtubeUrl = URL(string: videoUrl)!
let youtubeRequest = URLRequest(url: youtubeUrl ?? URL(string: "")!)
youtubeWebView.configuration.allowsInlineMediaPlayback = true
youtubeWebView.load(youtubeRequest)
I only want to play video in WKWebView not in full screen

Using AVAudioPlayer to play remote mp3 file in Swift

I'm new to Swift, but I want to change my view controller to play a remote mp3 file in my iOS app. I started with this code to play a song locally, and it works (with functions for the player after):
import AVFoundation
class Music1ViewController: UIViewController {
//5 -
var songPlayer = AVAudioPlayer()
//15 -
var hasBeenPaused = false
//6 -
func prepareSongAndSession() {
do {
//7 - Insert the song from our Bundle into our AVAudioPlayer
songPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "localsong", ofType: "mp3")!))
//8 - Prepare the song to be played
songPlayer.prepareToPlay()
After looking at the AVAudioPlayer documentation, .prepareToPlay() preloads the buffer, which makes me think all I need to do is change the initializer to target a URL.
Then I change the initializer:
songPlayer = try AVAudioPlayer(contentsOf: URL(string: "https://s3.amazonaws.com/kargopolov/kukushka.mp3")!)
I don't get any errors in XCode, but when I run it, I see an error in the console for Thread 1: EXC_BAD_ACCESS (code=1, address=0x48) which makes me think I am approaching this wrong.
Is there a better way to access the remote mp3 file?
Try this code :
You need to add AVKit & AVFoundation to your frameworks path and import them :
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
var player = AVPlayer()
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func localPress(_ sender: Any) {
let path = Bundle.main.resourcePath!+"/sound.mp3"
print(path)
let url = URL(fileURLWithPath: path)
let playerItem = AVPlayerItem(url: url)
player = AVPlayer(playerItem: playerItem)
player.play()
}// i have created a btn for playing a local file, this is it's action
#IBAction func urlPressed(_ sender: Any) {
let playerItem = AVPlayerItem(url: URL(string: "https://yourURL.mp3")!)
player = AVPlayer(playerItem: playerItem)
player.play()
}// i have created another btn for playing a URL file, this is it's action
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
My approach
func preparePlayer() {
guard let url = URL(string: "https://yourURL.mp3") else {
print("Invalid URL")
return
}
do {
let session = AVAudioSession.sharedInstance()
try session.setCategory(AVAudioSessionCategoryPlayback)
let soundData = try Data(contentsOf: url)
audioPlayer = try AVAudioPlayer(data: soundData)
audioPlayer.volume = 1
let minuteString = String(format: "%02d", (Int(audioPlayer.duration) / 60))
let secondString = String(format: "%02d", (Int(audioPlayer.duration) % 60))
print("TOTAL TIMER: \(minuteString):\(secondString)")
} catch {
print(error)
}
}

AVPlayer Inside A UIView

So I have a view on my HomeViewController that I want to play a video inside of. on viewDidLoad() I am calling playVideo() here is my code:
func playVideo() {
guard let filePath = Bundle.main.path(forResource: "video", ofType: "mov") else {
debugPrint("video.mov not found.")
return
}
let avPlayer = AVPlayer(url: URL(fileURLWithPath: filePath))
let avPlayerController = AVPlayerViewController()
avPlayerController.player = avPlayer
avPlayerController.view.frame = playerView.bounds
avPlayerController.showsPlaybackControls = false
avPlayerController.player?.play()
playerView.addSubview(avPlayerController.view)
playerView.bringSubview(toFront: QRButton)
}
All I get is a black background and no video. What am I doing wrong? I have tried to solve this for a while now.
iOS: 11.2 beta
Xcode: 9.2 beta
Try to use avPlayer as a class variable, maybe it has been released before it can start to play.
class ViewController: UIViewController {
var avPlayer:AVPlayer?
func playVideo() {
guard let filePath = Bundle.main.path(forResource: "video", ofType: "mov") else {
debugPrint("video.mov not found.")
return
}
avPlayer? = AVPlayer(url: URL(fileURLWithPath: filePath))
let avPlayerController = AVPlayerViewController()
avPlayerController.player = avPlayer
avPlayerController.view.frame = playerView.bounds
avPlayerController.showsPlaybackControls = false
avPlayerController.player?.play()
playerView.addSubview(avPlayerController.view)
playerView.bringSubview(toFront: QRButton)
}
}

Play video from data chunks

I received video from node sever in chunks of data. But video does not play in AVPlayer. Here is my code.
let videoUrl = http://staging.teemo.me/api/video/stream/sample12.MOV
playVideo(path:videoUrl, self)
func playVideo(path:String, controller:UIViewController){
let yourFinalVideoURL = path
try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: [])
if (yourFinalVideoURL != "") {
let player = AVPlayer(url: NSURL(fileURLWithPath: yourFinalVideoURL) as URL)
let playerController = AVPlayerViewController()
playerController.player = player
controller.present(playerController, animated: true) {
//player.play()
if #available(iOS 10.0, *) {
player.playImmediately(atRate: 1.0)
} else {
player.play()
}
}
}
}
The follow code works for me:
let videoURL = URL(string: "Some url")
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
However when I tried to use your url it doesn't work.
I added the NSAppTransportSecurity in .plist, but it didn't work either.
It is obvious that you have a problem with the url, try to change the extension of the file sample12.MOV to sample12.mp4

how to use AVKit to playback the HTTP stream video

var url:NSURL! = NSURL(string: "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8")
var playItem:AVPlayerItem! = AVPlayerItem(URL: url);
var player:AVPlayer! = AVPlayer(playerItem: playItem);
var playerLayer:AVPlayerLayer! = AVPlayerLayer(player: player);
self.view.layer.addSublayer(playerLayer);
player.play()
when i run these code, there will be crash, could anyone told me how to use AVKit to playback the video? Thx ^ ^
try this:
//stream url
let movieUrl:NSURL? = NSURL (string: "http://YOUR URL")
if let url = movieUrl {
self.avPlayer = AVPlayer(URL: url)
self.avPlayerViewController.player = self.avPlayer
}
//Button action
#IBAction func live(sender: UIButton) {
self.presentViewController(self.avPlayerViewController, animated: true) { () -> Void in
self.avPlayerViewController.player?.play()
}

Resources