How to fix the common “no sound from AVPlayer/AVAudioPlayer” problem in iOS/Swift programming

by Joey deVilla on July 6, 2015

maxwell sound and his shoe phone

Maxwell Smart, the protagonist of the 1960s spy comedy Get Smart, and his shoe phone,
quite possibly the first mobile phone to make regular appearances on a TV show.

swift kickWhile answering this question on Stack Overflow, I noticed that in the “Related” list running down the right-hand side of the page that there were several similar questions. They essentially asked the same question:

I’m not getting any errors, so why isn’t the AVPlayer or AVAudioPlayer in my iOS app playing anything?

I’ve seen this question asked often enough that I’ve decided to post this article as an answer that I can point to from now on.

Here’s the short answer:

Because your AVPlayer or AVAudioPlayer went out of scope just as the sound started playing.

The long answer, complete with code examples, appears below.

To make my point as clear as possible, the examples will be simple: they’ll be attempts at creating an app that plays an MP3 located at a given URL. That way, all you have to follow along is either type in or copy-and-paste code without having to bring a sound file into your project. While these examples use an AVPlayer object to play an online MP3, what I’m demonstrating is equally valid for apps that use an AVAudioPlayer object to play an MP3 stored as a local file or in memory.

First, let’s do it the wrong way

Many “why isn’t my app playing any sounds?” questions on Stack Overflow include code snippets that make the same mistake that I’m about to demonstrate. Fire up Xcode and follow along…

xcode new single view application

Create a new single view Swift application in Xcode, then replace the contents of ViewController.swift with the following code:

Feel free to run it in either the simulator or on a device. You’ll see a blank screen and hear…nothing. The only feedback you’ll see in the app is in Xcode’s output pane, where you’ll see the messages from the println functions:

output

If you look at the code, you’ll see that everything happens in the viewDidLoad method. The output from the println functions — combined with the lack of error messages and the fact that the code compiled — suggests that code in viewDidLoad is being executed, even though you’re not hearing anything.

Take a look at where the AVPlayer instance is created. It’s inside the viewDidLoad method, which means that its scope is limited to viewDidLoad. Once viewDidLoad has executed its final line, the println function that outputs “…and we’re playing!”, all its local variables go out of scope, including avPlayer, which contains the reference to the AVPlayer object that’s supposed to be playing the MP3. Without that reference, that AVPlayer object gets cleaned up by the system and gets zapped out of existence. Simply put, the app did start playing the MP3, but stopped very, very shortly afterwards; probably on the order of milliseconds.

viewdidload 1

Hopefully, what I just told you gave you the hint for making the app work properly. Let’s try doing it the right way…

The right way

Replace the contents of ViewController.swift with the following code:

Note the difference: this time, we’re creating avPlayer as an instance variable. That means that its scope is the viewController instance. As long as the viewController exists, so will avPlayer. And since this app has only one view controller, it means that avPlayer — and the AVPlayer object it references — will exist as long as the app is running.

Try running the app again. As long as the simulator or device can access the ‘net and there’s actually an MP3 at the URL specified in urlString, you’ll hear that MP3 playing.

viewdidload 2

Once again: the answer to the problem is that your AVPlayer or AVAudioPlayer instance needs to be scoped at the view controller level, and not at the level of a method. That way, the player exists as long as the view controller instance does.

{ 11 comments… read them below or add one }

1 tiengau September 23, 2015 at 8:35 pm

Nice tutorial , in this xcode 7 swift 2 , should put do{} into , sth like this :
import AVFoudation
var player:AVPlayer!

override func viewDidLoad() {
super.viewDidLoad()

let linkString = “http://m.mp3.zing.vn/xml/song-load/MjAxNSUyRjA4JTJGMDQlMkY3JTJGYiUyRjdiNTI4YTc0YWU2MGExYWJjMDZlYzA5NmE5MzFjMjliLm1wMyU3QzEz”

let link = NSURL(string: linkString)!
player = AVPlayer(URL: link)

do{
player.play()
}
}

2 DazChong January 28, 2016 at 7:31 am

Thanks! The logic is simple but we overlooked it! :D

3 Radu Ursache February 7, 2016 at 6:50 am

thank you, this saved my day!

4 Bhavya March 25, 2016 at 8:43 am

Thanks. It was helpful.

5 Montana May 17, 2016 at 5:53 pm

Thanks, but that’s not what my problem is.
I have an app that plays local media, then rewinds after each play of the last item in an AVQueuePlayer’s queue and plays from the given point. The AVQueuePlayer itself is a property of my UIViewController. My problem is, each item will play just once.

Code:

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad {
[super viewDidLoad];
self.queuePlayer = [[AVQueuePlayer alloc] init];

AVPlayerItem *playerItem1 = [AVPlayerItem playerItemWithURL:[[NSBundle mainBundle] URLForResource:@”placeholder” withExtension:@”m4a”]];
[self.queuePlayer insertItem:playerItem1 afterItem:nil];
AVPlayerItem *item2 = [AVPlayerItem playerItemWithURL:[[NSBundle mainBundle] URLForResource:@”man-2″ withExtension:@”m4a”]];
[self.queuePlayer insertItem:item2 afterItem:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shouldReplay) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
[self.queuePlayer play];
}

-(void)doReplay:(BOOL)insertLastItem

{
/*
if (insertLastItem) {
[self.queuePlayer insertItem:lastItem afterItem:nil];

}*/
AVPlayerItem *lastItem = [self.queuePlayer.items lastObject];

[lastItem seekToTime:CMTimeMakeWithSeconds(1, NSEC_PER_SEC) toleranceBefore:CMTimeMake(1, 10)
toleranceAfter:CMTimeMake(1, 10) completionHandler:^(BOOL finished) {

}];
while (self.queuePlayer.status != AVPlayerStatusReadyToPlay) {
// do nothing
}
BOOL readyToPlay = lastItem.status == AVPlayerItemStatusReadyToPlay;
NSLog(@”Ready to play: %@”,readyToPlay ? @”Yes” : @”No”);
NSLog(@”Item time: %d”,lastItem.currentTime);
[self.queuePlayer play];
}

-(void)shouldReplay
{
if ([[self.queuePlayer items] count] == 1)
{

[self doReplay:false];
}

}

@end

6 YJCH0I June 23, 2016 at 11:12 pm

Hello there! Thank you so much for making this post. You have no idea how long I was struggling to get my code to work! This was my issue/problem.

7 Azad August 8, 2016 at 10:29 am

Hello Team,

Greetings! I am using same AVPlayer and style to play some audio stream url in my apps but with IPv6 network all these urls doesn’t play any streaming urls so please assist me to overcome with it.

URLS :
http://s1.voscast.com:8438/listen.pls
http://us2.streamingpulse.com:7080
http://us2.streamingpulse.com:7080/listen.pls
http://us2.streamingpulse.com:7080
http://kanga.college.columbia.edu:8000

Same code i am using which you mentioned & all the urls works only for IPv4 address but doesn’t work on IPv6.

Your help would be very much appreciable!

Thanks,
Azad

8 Adnan August 27, 2016 at 6:25 am

Thanks for the post it solved my problem

9 olo November 2, 2016 at 11:26 am

thanks!!!!!! it worked like a charm

10 Mihir November 20, 2016 at 11:22 pm

Thank you very much for this. No StackOverflow post answered this. Saved a lot of painful research!

11 DuGRuT January 7, 2017 at 12:03 am

I have same problem. AVPlayer works great, all videos play but there is no sound with the running video on a device. The simulators always work perfect.
The instance are created prior to viewdidload. Anyway it’s all very strange. Are there any suggestions? Help please.

Leave a Comment

{ 1 trackback }

Previous post:

Next post: