Code Monkey home page Code Monkey logo

akhilcb / acbavplayerextension Goto Github PK

View Code? Open in Web Editor NEW
79.0 3.0 15.0 2.32 MB

An extension on AVPlayer which converts it to have all useful features of AVAudioPlayer but with streaming support. Also added additional methods to support Audio visualization from AVPlayer streaming.

License: MIT License

Objective-C 94.56% Swift 3.36% Ruby 2.08%
avplayer avaudioplayer objective-c swift extension metering audio-visualizer audio-processing audio-streaming audio-player

acbavplayerextension's Introduction

ACBAVPlayerExtension

CocoaPods Compatible Carthage Compatible Platform License

An extension on AVPlayer which converts it to have all useful features of AVAudioPlayer but with streaming support. Also added additional methods to support Audio visualization from AVPlayer streaming. This extension adds some missing features to AVPlayer.

This projects shows a simple example on how to use it. ViewController has a UIProgressView which displays averagePower as a volume meter while streaming an audio through AVPlayer. This is very similar to how we would normally use AVAudioPlayer, but with some additional methods which makes it easy to display the averagePower. audioPCMBufferFetchedWithCallbackBlock can be used to fetch AVAudioPCMBuffer which can be used for Audio Visualization. Added stop method to stop player.

Demo


Setup

Carthage or Cocoapods can be used to integrate this to a project.

Carthage

github "akhilcb/ACBAVPlayerExtension" ~> 2.1

Cocoapods

pod 'ACBAVPlayer'

AVPlayer Extension Interface

    @property (nonatomic, readonly, assign) int numberOfChannels;
    //stop video
    - (void)stop;
    
    /* metering */
    @property (nonatomic, getter=isMeteringEnabled) BOOL meteringEnabled; /* turns level metering on or off. default is off. */

    - (void)updateMeters; /* call to refresh meter values */
    - (float)peakPowerForChannel:(NSUInteger)channelNumber; /* for peak power in decibels for a given channel. returns average power for now */
    - (float)averagePowerForChannel:(NSUInteger)channelNumber; /* returns average power in decibels for a given channel */
    //use this to repeatedly fetch average power list(in decibels) for all channels in iMeteringCallbackBlock. This block will be executed each time a value is fetched. Index of array is channel number
    - (void)averagePowerListWithCallbackBlock:(ACBAVPlayerMeteringBlock)iMeteringCallbackBlock;
    - (float)averagePowerInLinearFormForChannel:(NSUInteger)channelNumber; //returns in average power in linear form. Value is in between 0 to 1.
    //fetch average power list in linear form(values in between 0 and 1)
    - (void)averagePowerListInLinearFormWithCallbackBlock:(ACBAVPlayerMeteringBlock)iMeteringCallbackBlock;
    //fetch AVAudioPCMBuffer. it has useful methods to manipuate buffers or to display a visualizer
    - (void)audioPCMBufferFetchedWithCallbackBlock:(ACBAVPlayerBufferFetchedBlock)iAudioBufferFetchedBlock;
    - (void)replaceCurrentItemAndUpdateMeteringForPlayerItem:(AVPlayerItem *)item; //use this instead of "replaceCurrentItemWithPlayerItem" for metering to work.

Note: This is a beta version. There could be some issues(Hopefully minor). Use at your own risk.

Usage

    self.player = [[AVPlayer alloc] initWithURL:remoteURL];
    self.player.meteringEnabled = true;
    
    [self.player averagePowerListInLinearFormWithCallbackBlock:^(NSArray *iAvgPowerList, BOOL iSuccess) {
        if (iAvgPowerList.count > 0) {
            double power = [iAvgPowerList[0] doubleValue];
            if (iAvgPowerList.count > 1) {
                double secondPower = [iAvgPowerList[1] doubleValue];
                power = (power + secondPower) / 2;
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.progressBar setProgress:(power * 10)];
            });
        }
    }];

Screenshots

License

MIT License

Copyright (c) 2017, Akhil C Balan(https://github.com/akhilcb)

All rights reserved.

acbavplayerextension's People

Contributors

akhilcb avatar cromulentlabs avatar stevenchenalfred avatar tusharaa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

acbavplayerextension's Issues

Swift

Perfect just what I need. But Sorry I am not familiar with C.
Is it possible to get an example on how to get a power meter value on an AVPlayer in swift ??

Memory Leak Issue (MYAudioTapProcessor.m)

I ran some profiling with Instruments and found that this snippet in MYAudioTapProcessor.m, in particular the malloc, is causing severe memory leaks. I didn't need the channelVolumeList functionality for my use case so I commented it out but wanted to flag this for others.

if (aCount > 0) {
        context->channelVolumeList = malloc(aCount * sizeof(float));
        if (context->channelVolumeList == 0) {
            NSLog(@"Error! could not initialize channel volume list");
        }
}

Is it possible to get the current amplitude as the media is being played?

Great stuff!

The project seems exactly what I need but to make sure:

Is it possible to get the current amplitude as the media is being played?

I am thinking of doing a simple visualization and I'd like to get every 0.5s or so the current updated volume/amplitude of the audio being played. If so, how would I get that value (I'm thinking something between 0 and 1)?

Thanks in advance!

HLS Stream

Hi,
it seems that with HLS stream (m3u8), the volume meter doesn't work...
Could you check please?

thank you

Changing item the PCM buffer is not called anymore

I can see the callback called goo on first item, when I change the item with method: replaceCurrentItemWithPlayerItem

    AVPlayerItem *item = [self audioItem];
    [self.player replaceCurrentItemWithPlayerItem:item];

the callback audioPCMBufferFetchedWithCallbackBlock is not called anymore.

I've also tried:

    [self.player play];
    [self.player setMeteringEnabled:YES];

    [self.player audioPCMBufferFetchedWithCallbackBlock:^(AVAudioPCMBuffer *audioPCMBuffer, BOOL iSuccess) {
        
    }];

forcing metering enabled to YES again...but nothing changes.

Are there come known issues with audio/video items?
How can I fix?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.