Apologies
You must be signed in to watch this lesson.
Play Episode with AVPlayer
Podcasts
We did a great job designing the detailed view for our Podcast Episode, showing all the relevant information a user would be interested in. Now it's time we made our application play the audio stream that is associated with each episode. To implement this feature, we import AVKit and using a very powerful component called AVPlayer. To get audio playback going for our app, we simply construct our URL, feed it into our player, and hit the play. Nothing too complicated, let me know if your streams are playing correctly.

Comments (12)
mamali86
6 years ago
Hello Brian, Thanks for the great videos. It would be great if you can also do a tutorial on RxSwift
Raph
6 years ago
please let the episodes come so that we can do them this week end.......... :)
rinconchris
6 years ago
Hi Brian - great tutorial so far! Question - what's the reason you're modifying the app transport security at this point rather than using the http -> https string extension you used earlier?
Brian Voong
6 years ago
rinconchris
6 years ago
thanks!
therafbuddy
5 years ago
The eyebrow shrug when it played and paused haha. Is there any particular reason why #selector is being used instead of just a regular method that can pause or play using the player? And the @objc tag... what's that all about?
Brian Voong
5 years ago
Mido Cool
5 years ago
Hey Guys I'm stuck on this lecture I'm making a different app not podcast I store mp3 as Binary Data in coreData in function below I couldn't figure it out, I know the value should be string it keeps yelling "cannot convert value of type 'Data?' to expected argument type 'String" Is there any way to convert it to string or an alternative solution, any help will be appreciate it. Thanks fileprivate func playSong() { print("trying to play mp3", mp3.urlSong) guard let url = URL(string: mp3.urlSong) else { return } let playerItem = AVPlayerItem(url: url) player.replaceCurrentItem(with: playerItem) player.currentItem player.play() }
Brian Voong
5 years ago
StanyMiles
5 years ago
Hey Brian. Thanx for courses! I have learned a lot from you! I'm a little bit stuck on this lesson. Somehow on playing podcast I get some strange exception and app breaks. I have no clue why. I followed all your steps and overchecked all couple times. ``` libc++abi.dylib`__cxa_throw: -> 0x109918089 <+0>: pushq %rbp 0x10991808a <+1>: movq %rsp, %rbp 0x10991808d <+4>: pushq %r15 0x10991808f <+6>: pushq %r14 0x109918091 <+8>: pushq %r12 0x109918093 <+10>: pushq %rbx 0x109918094 <+11>: movq %rdx, %r14 0x109918097 <+14>: movq %rsi, %r15 0x10991809a <+17>: movq %rdi, %rbx 0x10991809d <+20>: callq 0x109917ee1 ; __cxa_get_globals 0x1099180a2 <+25>: movq %rax, %r12 0x1099180a5 <+28>: callq 0x109918610 ; std::get_unexpected() 0x1099180aa <+33>: movq %rax, -0x60(%rbx) 0x1099180ae <+37>: callq 0x109918648 ; std::get_terminate() 0x1099180b3 <+42>: movq %rax, -0x58(%rbx) 0x1099180b7 <+46>: movq %r15, -0x70(%rbx) 0x1099180bb <+50>: movq %r14, -0x68(%rbx) 0x1099180bf <+54>: movabsq $0x434c4e47432b2b00, %rax ; imm = 0x434C4E47432B2B00 0x1099180c9 <+64>: movq %rax, -0x20(%rbx) 0x1099180cd <+68>: movq $0x1, -0x78(%rbx) 0x1099180d5 <+76>: incl 0x8(%r12) 0x1099180da <+81>: leaq -0x20(%rbx), %r14 0x1099180de <+85>: leaq 0x1d(%rip), %rax ; __cxxabiv1::exception_cleanup_func(_Unwind_Reason_Code, _Unwind_Exception*) 0x1099180e5 <+92>: movq %rax, -0x18(%rbx) 0x1099180e9 <+96>: movq %r14, %rdi 0x1099180ec <+99>: callq 0x10991ab9c ; symbol stub for: _Unwind_RaiseException 0x1099180f1 <+104>: movq %r14, %rdi 0x1099180f4 <+107>: callq 0x109918125 ; __cxa_begin_catch 0x1099180f9 <+112>: movq -0x58(%rbx), %rdi 0x1099180fd <+116>: callq 0x109918656 ; std::__terminate(void (*)()) ``` this is all I got from there. Can you point me direction to look what may be wrong please.
Brian Voong
5 years ago
StanyMiles
5 years ago
it crashes on avPlayer.play()
Brian Voong
5 years ago
StanyMiles
5 years ago
Yeah. It not really breaks app but stops at some breakpoint on line 2 of what I poster above -> 0x109918089 <+0>: pushq %rbp I am on Xcode Beta right now and ios 12 beta too.. maybe this is the issue.
StanyMiles
5 years ago
Xcode 10 stable version and iPhone 8 Plus simulator the same result
Brian Voong
5 years ago
StanyMiles
5 years ago
Every episode and podcast I try is the same issue.
StanyMiles
5 years ago
Yeeah. I figured it out. I had active objc_exception_throw breakpoint. just disabled it and it worked. If somebody will run into the same problem I hope it helps. btw Thank you for the fastest response!
Umberto Grimaldi
5 years ago
Hi Brian, I'm having problem while tapping on playPauseButton and I don't know why. This is what I have in console: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PodcastCourseLBTA.PlayerDetailsView playPauseButton:]: unrecognized selector sent to instance 0x7f9bf0f2a1e0'. Could you give any suggestions on how can I fix. Thanks Brian
Brian Voong
5 years ago
Umberto Grimaldi
5 years ago
Thanks Brian, I solved. And thanks for all you courses, They are awesome!!!
ttanew
3 years ago
Hell Brian, I had same issue like Umberto. so I solve this problem using @IBAction function. Like this, @IBOutlet weak var pausePlayButton: UIButton! @IBAction func handlePlayPause(_ sender: UIButton) { print("Trying to play and pause") if player.timeControlStatus == .paused { player.play() } else { player.pause() } } When I changed my code like upper one, It works :) Are there any other problems when converting this way? Could you give any suggestions? Thank you so much Brian!
Kevin Jefferson
5 years ago
Hello Brian, If I search other podcast at times the album image will not show up and I am using your file. for example espn will not show image after a podcast is selected. is there a reason?
stevezhou
5 years ago
Is it weird that When I run my code on iPad it worked but on my iPhone it doesn't ? I tried your code on my iPhone and it worked as well.
Maxnelson997
5 years ago
Try deleting the app and re-installing it to your device. Maybe you denied access to something. The mic I think is what it asks for.
ZiadHamdieh
4 years ago
Hey Brian, would you consider using a ternary operator just for its side effect to be bad practice? i.e. player.timeControlStatus == .isPlaying ? player.pause() : player.play()
Brian Voong
4 years ago
李承諴
3 years ago
AVPlayer seems can't play item with "window?.addSubview()" when we select podcast but it works when we use".present(_:animated:completion:)"
juansebar
3 years ago
Hi Brian, Thanks for the amazing content! By any chance do you know how to fix the data leak that is being caused by creating the AVPlayer? I have been trying to research about a solution and from some sources I have found that if you run the app in the simulator then it causes this issue. But, when you run it on a device it shouldn't. Thank in advance.
HELP & SUPPORT