Programmatically created EZAudioPlot does not draw

1k Views Asked by At

I am playing audio file using EZAudioPlayer. I want the soundwave to be drawn on EZAudioPlot.

I succeeded in doing this. However, when I create EZAudioPlot programmatically, the view shows but no soundwave is drawn.

Here is some of the code

var audioPlayer: EZAudioPlayer!
@IBOutlet weak var plot1: EZAudioPlot!
var plot1Flag:Bool = true

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let plotType: EZPlotType = EZPlotType(rawValue: 1)!;
    plot1?.plotType = plotType
    plot1?.shouldFill = true;
    plot1?.shouldMirror = true;


    plot2 = EZAudioPlot()
    plot2.plotType = plotType
    plot2.shouldFill = true;
    plot2.shouldMirror = true;
    plot2.frame = CGRectMake(10, 200, 200, 200)
    plot2.backgroundColor = UIColor.blueColor()
    plot2.color = UIColor.whiteColor()
    self.view.addSubview(plot2)
}

@IBAction func playSound(sender: UIButton) {
    audioPlayer = EZAudioPlayer(URL: NSURL.fileURLWithPath(NSBundle.mainBundle().pathForResource("Alright", ofType: "wav")!), delegate: self)
    audioPlayer.play()
    plot1Flag = !plot1Flag
}

func audioPlayer(audioPlayer: EZAudioPlayer!, playedAudio buffer: UnsafeMutablePointer<UnsafeMutablePointer<Float>>, withBufferSize bufferSize: UInt32, withNumberOfChannels numberOfChannels: UInt32, inAudioFile audioFile: EZAudioFile!) {
    if(plot1Flag){
        print("plot1")
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            self.plot1?.updateBuffer(buffer[0], withBufferSize: bufferSize);
        })
    }else{
        print("plot2")
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            self.plot2.updateBuffer(buffer[0], withBufferSize: bufferSize);
        })
    }

}

Can you tell me why the manually created plot2 doesn't draw?

Thank you for your insight!!!

1

There are 1 best solutions below

0
On BEST ANSWER

for real time displays you need to add this line to your plot2 var

plot2.shouldOptimizeForRealtimePlot = false;

Hope it helps you :)

Here is the documentation:

@property (nonatomic, assign) BOOL shouldOptimizeForRealtimePlot;

A BOOL that allows optimizing the audio plot’s drawing for real-time displays. Since the update function may be updating the plot’s data very quickly (over 60 frames per second) this property will throttle the drawing calls to be 60 frames per second (or whatever the screen rate is). Specifically, it disables implicit path change animations on the waveformLayer and sets up a display link to render 60 fps (audio updating the plot at 44.1 kHz causes it to re-render 86 fps - far greater than what is needed for a visual display).