SoFunction
Updated on 2025-04-13

Simple implementation method for audio and video playback in iOS development

Preface

In our daily iOS development, there are many types of audio and video playback. Currently, the system comes with its own AVFoundation framework, which is closer to the underlying layer, so it is very flexible and more convenient to customize.

There is also third-party audio and video playback, which is characterized by powerful functions, simple implementation, and support for streaming media. Let’s introduce them one by one, for reference and learning. Let’s take a look at the detailed introduction below.

Play system sound effects or short sound effects

Notice:

  • The resource length here is up to 30 seconds
  • The resource must be imported in Target --> Build Phases --> Copy Bundle Resources, otherwise the file cannot be obtained
if let soundURL = (forResource: "sourceName", withExtension: "wav") {
 var mySound: SystemSoundID = 0
 AudioServicesCreateSystemSoundID(soundURL as CFURL, &mySound)
 // Play
 AudioServicesPlaySystemSound(mySound);
}

Play music - AVAudioPlayer

  • The resource must be imported in Target --> Build Phases --> Copy Bundle Resources, otherwise the file cannot be obtained
  • Supports multiple audio formats, and can control progress, volume, playback speed, etc.

Define a player property

// Define a player propertyfileprivate var player: AVAudioPlayer?

Initialize and start playing music

func playMusic() {

 // 2. Obtain corresponding music resources guard let fileUrl = (forResource: "309769", withExtension: "mp3") else {
  return }
 // 3. Create the corresponding player do {
  player = try AVAudioPlayer(contentsOf: fileUrl)
 } catch {
  print(error)
 }

 // Set up the proxy listening and playback to complete player?.delegate = self

 // 4. Prepare to play player?.prepareToPlay()

 // 5. Play music player?.play()
}

Stop, pause

// Stop, pauseplayer?.stop()

Listen to AVAudioPlayer playback is complete

// MARK: - AVAudioPlayerDelegate
extension ViewController: AVAudioPlayerDelegate {
 /// Complete playback func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
  if flag {
   print("Playing is completed")
  }
 }
}

Use AVPlayer to play videos

The AVPlayer object must be set to global, otherwise the playback will not be successful.

AVPlayer exists in AVFoundation and can play music, local audio and stream media

However, it has high degree of freedom to control videos and can customize the video playback interface

By notifying the progress of the playback and related parameters

Three elements of AVPlayer

  • AVPlayerItem (providing data, setting data source)
  • AVPlayer (responsible for controlling playback, pause, playback, playback at specified time, etc.)
  • AVPlayerLayer (responsible for displaying that if there is no sound, there is no video)
func playVideo() {
 // 1. Create AVPlayer guard let url = URL(string: "/14559682994064.mp4") else { return }

 // 2. Create a player // Provide data and set data source let item = AVPlayerItem(url: url)
 // Responsible for controlling playback, pause, playback, playback at specified time, etc. let player = AVPlayer(playerItem: item)

 // 3. Create a layer and be responsible for displaying it let layer = AVPlayerLayer(player: player)
  = 
 (layer)

 // 4. Play video ()
}

Use IJKMediaFramework to play videos

ijkPLayer is a lightweight video player developed and open source by Bilibili based on ffmpeg. It supports playing local network videos and also supports streaming media playback

Very powerful, supporting soft and hard decoding of videos

Manually import SDK, add dependencies

import IJKMediaFramework

Initialize the player and play it

func playVideoByIJKPlayer() {

 // 1. Set the configuration and turn on hard decoding let options = ()
 options?.setOptionIntValue(1, forKey: "videotoolbox", of: kIJKFFOptionCategoryPlayer)

 // 2. Initialize the player guard let ijkPlayer = IJKFFMoviePlayerController(contentURLString: "/14559682994064.mp4", with: options) else { return }
  = 
 ()

 // 3. Start playing ()
}

Summarize

The above is the entire content of this article. I hope that the content of this article has a certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.