Ben Novakovic
Ben Novakovic

Reputation: 581

AVAudioPlayer and AirPlay - possible?

I'm trying to ascertain whether it's possible to toggle AirPlay support using the AVAudioPlayer class.

From what I have read:

AirPlay is a technology that lets your application stream audio to Apple TV and to third-party AirPlay speakers and receivers. AirPlay support is built in to the AV Foundation framework and the Core Audio family of frameworks. Any audio content you play using these frameworks is automatically made eligible for AirPlay distribution. Once the user chooses to play your audio using AirPlay, it is routed automatically by the system. [ Ref ]

Based on this info; it should work with AVAudioPlayer as it is part of the AVFoundation framework; but I can't seem to find any documentation supporting this assumption.

I have also found some documentation saying it can be done with MPMoviePlayerViewController:

Support for playing video using AirPlay is included in the MPMoviePlayerController class. This support allows you to play video-based content on AirPlay–enabled hardware such as Apple TV. When the allowsAirPlay property of an active MPMoviePlayerController object is set to YES and the device is in range of AirPlay–enabled hardware, the movie player presents the user with a control for sending the video to that hardware. [ Ref ]

Seems like there is some conflicting information here. Does anyone know if its possible to use AVAudioPlayer to route to AirPlay or are we forced to use the MPMoviePlayerController class?

Thanks very much.

Upvotes: 5

Views: 4128

Answers (3)

user8391200
user8391200

Reputation:

Yes, AVAudioPlayer works with AirPlay. However, there's a quirk that events like audioPlayerDidFinishPlaying are latency-corrected (i.e. from the viewpoint of your app, they occur 2 seconds "too late"), which may be a problem if you rely on them happening in the correct order with other mechanics in your app.

Upvotes: 0

bpolat
bpolat

Reputation: 3908

Its pretty easy with Interface Builder so you can easily utilize Auto Layout. Place anywhere you want an UIView object onto your main view then create a subclass of MPVolumeView and use this custom class for your UIView object

enter image description here

and the class VolumeView subclass of MPVolumeView

import UIKit
import MediaPlayer
class VolumeView: MPVolumeView {
   convenience init() {
       self.init()
}

override func drawRect(rect: CGRect) {
    self.showsVolumeSlider = false
    self.layer.cornerRadius = 10.0
    self.clipsToBounds = true

}

}

Upvotes: 0

Ben Novakovic
Ben Novakovic

Reputation: 581

In response to my own question. It looks like the simple way is to add the custom volume controller is mentioned here: [ ref ], and it will work perfectly with the MPAudioPlayer. Its just a matter of positioning it.

Upvotes: 2

Related Questions