Skip to content

Ios Avplayer Error

DodaTech 2 min read

In this tutorial, you'll learn about How to Fix iOS AVPlayer Errors. We cover key concepts, practical examples, and best practices.

The Problem

AVPlayer shows a black screen or the video does not play:

Player is ready but no video visible.

Quick Fix

Step 1: Add AVPlayerLayer to the view hierarchy

WRONG — creating a player without adding the layer:

let player = AVPlayer(url: url)
// Missing: player layer not added to view

RIGHT — add the player layer:

import AVKit

let player = AVPlayer(url: url)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = view.bounds
view.layer.addSublayer(playerLayer)
player.play()

Step 2: Use AVPlayerViewController

import AVKit

let player = AVPlayer(url: url)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
present(playerViewController, animated: true) {
    player.play()
}

Step 3: Check the URL is valid

guard let url = URL(string: "https://example.com/video.mp4") else {
    print("Invalid URL")
    return
}

let asset = AVAsset(url: url)
print("Playable: \(asset.isPlayable)")
print("Duration: \(asset.duration.seconds)")

Step 4: Handle playback errors

player.currentItem?.observe(\.status) { item, _ in
    switch item.status {
    case .readyToPlay:
        player.play()
    case .failed:
        print("Failed: \(item.error?.localizedDescription ?? "")")
    case .unknown:
        print("Loading...")
    @unknown default:
        break
    }
}

Step 5: Enable audio playback in the background

In Info.plist:

<key>UIBackgroundModes</key>
<array>
    <string>audio</string>
</array>

Step 6: Handle buffering

player.currentItem?.preferredForwardBufferDuration = 2.0

Prevention

  • Test with a known working URL before debugging player code.
  • Add AVPlayerLayer to the view before calling play().
  • Check status and error properties on the player item.

Common Mistakes with avplayer error

  1. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  2. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  3. Misunderstanding that String is [Char] with poor performance for large text operations

These mistakes appear frequently in real-world IOS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between AVPlayer and AVPlayerViewController?

AVPlayer is the core playback object. AVPlayerViewController provides a pre-built UI with controls, full-screen support, and picture-in-picture.

Why is there no audio from AVPlayer?

The device may be muted (silent switch), or the audio session is not configured. Add try? AVAudioSession.sharedInstance().setActive(true).

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro