Coder Social home page Coder Social logo

afsoundmanager's Introduction

AFSoundManager v2

Build Status alt text alt text

AFSoundManager

iOS audio playing (both local and streaming) and recording made easy through a complete and block-driven Objective-C class. AFSoundManager uses AudioToolbox and AVFoundation frameworks to serve the audio.

##Installation

###CocoaPods

AFSoundManager is available on CocoaPods so you can get it by adding this line to your Podfile:

pod 'AFSoundManager'

###Manual

If you don't use CocoaPods, you will have to import these files into your project:

AFSoundManager.h
AFSoundPlayback.h
AFSoundPlayback.m
AFSoundItem.h
AFSoundItem.m
AFSoundQueue.h
AFSoundQueue.m
AFSoundRecord.h
AFSoundRecord.m
NSTimer+AFSoundManager.h
NSTimer+AFSoundManager.m

Also, you need to import AVFoundation, AudioToolbox and MediaPlayer frameworks.

##Roadmap

Since the v2.0 is a very first version, there's a lot of work remaining. This is a sneak peek of what's coming soon:

  • Integration with Control Center and Locked Screen controls
  • Improve data returning to give the developer more data about the current playing
  • A brand new class to let AFSoundPlayback handle real time effects like delay, a customized EQ, etc.
  • AFSoundManager-ready UI controls to integrate a fulfilled play control in your app at a glance, powered by AFSoundManager.

##Usage

###Playing a sound

AFSoundItem will take care of any sound item, and it will be handled by AFSoundPlayback. The AFSoundItem will contain the location and the metadata of each sound, song, chapter, etc.

AFSoundItem *item = [[AFSoundItem alloc] initWithLocalResource:@"demo.mp3" atPath:nil];
    
AFSoundPlayback *player = [[AFSoundPlayback alloc] initWithItem:item];
	
[player play];

[_queue listenFeedbackUpdatesWithBlock:^(AFSoundItem *item) {
        
    NSLog(@"Item duration: %ld - time elapsed: %ld", (long)item.duration, (long)item.timePlayed);
} andFinishedBlock:^(void) {
        
    NSLog(@"Track finished playing");
}];

###Playing a queue

So, an AFSoundItem object can be played individually, but it can also be part of a queue, managed by AFSoundQueue.

AFSoundItem *item1 = [[AFSoundItem alloc] initWithLocalResource:@"demo1.mp3" atPath:nil];
AFSoundItem *item2 = [[AFSoundItem alloc] initWithLocalResource:@"demo2.mp3" atPath:nil];
AFSoundItem *item3 = [[AFSoundItem alloc] initWithLocalResource:@"demo3.mp3" atPath:nil];

AFSoundQueue *queue = [[AFSoundQueue alloc] initWithItems:@[item1, item2, item3]];
[queue playCurrentItem];

[_queue listenFeedbackUpdatesWithBlock:^(AFSoundItem *item) {
        
    NSLog(@"Item duration: %ld - time elapsed: %ld", (long)item.duration, (long)item.timePlayed);
} andFinishedBlock:^(AFSoundItem *nextItem) {
        
    NSLog(@"Finished item, next one is %@", nextItem.title);
}];

The block will be executed each 1 second, returning a dictionary with info about the current item. Expect more info returned by the block in future releases, like the current item object (AFSoundItem), current queue index, etc.

###Current playing actions

####Standard

-(void)play;
-(void)pause;
-(void)restart;
-(void)playAtSecond:(NSInteger)second;

####Queue

-(void)playCurrentItem;
-(void)pause;
-(void)playNextItem;
-(void)playPreviousItem;
-(void)playItem:(AFSoundItem *)item;
-(void)playItemAtIndex:(NSInteger)index;

###Managing a queue

To manage a queue, you can both add items with -addItem: or addItem:atIndex: or remove items from the current queue with -removeItem: and -removeItemAtIndex:

###Fetching metadata

Since you'll be working with AFSoundItem to manage your tracks, you can obtain the metadata related like the title, album, artist and artwork.

[_queue listenFeedbackUpdatesWithBlock:^(AFSoundItem *item) {
        
    NSLog(@"Item duration: %ld - time elapsed: %ld", (long)item.duration, (long)item.timePlayed);
} andFinishedBlock:^(AFSoundItem *nextItem) {
        
    NSLog(@"Finished item, next one is %@, by %@ album from the album %@", nextItem.title, nextItem.artist, nextItem.album);
    
    [self updateArtwork:nextItem.artwork];
}];

###Recording a sound

As the previous version, AFSoundManager lets you record sound quickly thanks to AFSoundRecord:

AFSoundRecord *recorder = [[AFSoundRecord alloc] initWithFilePath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];

[recorder startRecording];

##License AFSoundManager is under MIT license so feel free to use it!

##Author Made by Alvaro Franco. If you have any question, feel free to drop me a line at [email protected]

afsoundmanager's People

Contributors

alvarofranco avatar xbeg9 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  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  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

afsoundmanager's Issues

Lock Screen audio controls disappear when I pause

When I run the app the audio plays when the view loads. The app continues to play audio when it goes into background mode. I am able to successfully pause and/or play audio from the Control Center (accessed either from within the app or the Lock Screen) but, if I access the Lock Screen audio controls and pause the player, the music pauses and the lock screen controls disappear. I expect the music to pause, but not for the controls to disappear.

Note: I already use MPNowPlayingInfoCenter on my example.

bug for short music in queue

i found one bug, i want to play some mp3 in an queue, but the mp3 is very short, less than 1s,can‘t play other music unless the first one

Unable to record and playback.

I choose your library fot its simplicity, so i have used your example code and i'm getting this error when trying to play what i have recorded:

NSOSStatusErrorDomain -43

Weird Sounds

I am playing sounds in a queue and if the app is in background and the track finishes the queue couldn't play the next sound infact muffle /distort all the sound with the error

AFSoundItem: 0x7f9a7163fc10> 0
ERROR: >aq> 1627: failed (-66674); will stop (4096/0 frames)

Any help or solution appreciated .Thanks in advance.

time Remaining 42:48

hi, it is every time showing 42:48 time Remaining when stream from url. how can i solve this
thank you

Audio playing issue when network jam

I met a problem:
When network is slow, sometimes AFSoundManager will pause a little, and continue. sometimes it will stop playing. When it stops, try to "resume" audio playing, encounter the following problem:

elapsedTime, timeRemaining ... sometimes will stop update after resume.
But can hear audio.

Seems startStreamingRemoteAudioFromURL:andBlock: stop calling it's Block.

This problem can be reproduced in slow network.

How to switch between headphone and speaker while playing?

As the title described, I'd like to let people can switch the output device between headphone and speaker while the sound is playing. For example, when people make his/her ear close to the phone, it turns to use headphone to play the sound, otherwise use speaker to play the sound.
How to implement this use AFSoundManager?

Streaming and caching

Is possible do such things with the library?

start playing .mp3 from url immediately (like streaming or streaming)
cache it and track caching progress during it's playback
save cached file to local storage

Failed to recorded

I wanna recorded , but i failed , could you make a demo in your project? If you can make them associated with a player, I think it's better for us newbie. Thank you very much.

Warning - AVAudioRecorder

AFSoundManager/Classes/AFSoundRecord.m Null passed to a callee that requires a non-null argument

_recorder = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:filePath] settings:0 error:nil];
screen shot 2017-05-12 at 10 34 11 am

Any Updates?

Has this project been abandoned? Would like to know so that we can move on to another solution.

Thanks.

Doesn't stream mp3 with artwork

When I stream a mp3 with artwork image (copied the file from itunes into my server), I get no sound and no error. Removing the artwork, everything is fine.

Any suggestions what's wrong?

Memory Leak

I need to call the following method several time to play music from a particular view controller.
Each time I call this method memory increases. Those memory are not released when I leave that view controller. If I remove the listenFeebackUpdates block code, it working fine without any unwanted memory issues.

To reproduce the issue I attached following code snippets, please check it.

  • (void)playMusicWithFileName:(NSString*)fileName{

    AFSoundItem *item = [[AFSoundItem alloc] initWithLocalResource:fileName atPath:nil];
    [player pause];
    player = nil;
    player = [[AFSoundPlayback alloc] initWithItem:item];
    item = nil;

    // If I remove the listenFeedbackUpdate code block then there is no memory issue.
    [player listenFeedbackUpdatesWithBlock:nil andFinishedBlock:^(void) {
    NSLog(@"Finished");
    }];

    [player play];
    }

Thanks

Buffer size when playing a remote file

Hi, I'm sorry to use the issue tracker to ask this but is there a way to reduce the buffer size when playing a remote http file ? Problem is that the current buffer size is too large and songs can take more than 20 seconds to start playing even in Wifi. Thanks.

Warning: Incompatible pointer types

Xcode 5.0.2, iOS 7.1
Test on simulator iPhone Retina (4-inch 64-bit): No warning at all.
Test on simulator iPhone Retina (3.5-inch): Has following Warning.
After testing on 3.5-inch, when change back to 4-inch. the warning doesn't go away. Don't know why the compiler didn't detect the warning in the first place..

Detail:

In AFAudioRouter.m, line 31:
[[AVAudioSession sharedInstance]setDelegate:self]; <-----

Warning Message:
AFSoundManager/AFAudioRouter.m:31:53: Incompatible pointer types sending 'Class' to parameter of type 'id'

Crashed when play a remote mp3 file without artwork!

Hi @AlvaroFranco ,
I try to play a mp3 file from remote url, so the app crashed.
Console Logs:

2015-04-04 09:21:17.653 QNSShow[602:12426] -[__NSCFData objectForKey:]: unrecognized selector sent to instance 0x7fcd91f0ad10
2015-04-04 09:21:17.801 QNSShow[602:12426] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData objectForKey:]: unrecognized selector sent to instance 0x7fcd91f0ad10'
*** First throw call stack:

if I disable these code lines, the app will work

  1. _artwork = [UIImage imageWithData:[[metadataItem.value copyWithZone:nil]
  2. _artwork = [UIImage imageWithData:[metadataItem.value copyWithZone:nil]];

You can pick any mp3 url from this like below to reproduce this problem
http://download.chiasenhac.com/mp3/us-uk/u-pop/nothing-else-matters~metalica~1001903_download.html

How to seek when using a queue?

Hi, how to seek when using a queue, i found AFSoundPlayback class has a method named "playAtSecond:", but AFSoundQueue class doesn't have.

sound's metadata not accurate

for example, many tracks I put into my project using AFSoundManager get 0:00 for duration, it seems thats the library exists severe issue on metadata fields.

can't get duration

 NSURL *url = [NSURL URLWithString:@"http://222.186.15.28:8083/demo_store/2015/12/28/94ef7255caf8704acfa36fdf3d444052.mp3"];

NSURL *ceceurl = [NSURL URLWithString:@"http://www.cecesat.com/FileCenter/data/APS/TF/TPO-20-C1.mp3"];
AFSoundItem *item6 = [[AFSoundItem alloc] initWithStreamingURL:url ];
AFSoundItem *item7 = [[AFSoundItem alloc] initWithStreamingURL:ceceurl];

item6 is ok,but item7 cant get duration,Is mp3 file damaged? But the android is good

url1:
2016-01-14 10:39:45.454 AFSoundManager-Demo[22069:77459] Item duration: 73 - time elapsed: 20
2016-01-14 10:39:46.454 AFSoundManager-Demo[22069:77459] Item duration: 73 - time elapsed: 0

ceceurl:
2016-01-14 10:39:47.453 AFSoundManager-Demo[22069:77459] Item duration: 0 - time elapsed: 0
2016-01-14 10:39:47.453 AFSoundManager-Demo[22069:77459] Finished item, next one is (null)
2016-01-14 10:39:47.459 AFSoundManager-Demo[22069:77459] Item duration: 0 - time elapsed: 0
2016-01-14 10:39:47.459 AFSoundManager-Demo[22069:77459] Finished item, next one is (null)
2016-01-14 10:39:47.460 AFSoundManager-Demo[22069:77459] Item duration: 0 - time elapsed: 0

can not play download audio file

If I download some audio files, I can't play it. because you play bundle audio file by default. I think you should let the user choose which file to play. not be limited to bundle file

Using AFSoundQueue with UITableView

How can I check when current AFSoundItem is stop/start playing if I use AFSoundQueue with UITableView (each AFSoundItem in UITableViewCell)?
Thank you!

Improper init for AFSoundPlayback

It seems that you are using _currentItem in setUpItem:

_currentItem.duration = (int)CMTimeGetSeconds(_player.currentItem.asset.duration);

Before it is assigned to.

Easy to fix by setting _currentItem before calling setUpItem:

-(id)initWithItem:(AFSoundItem *)item {

    if (self == [super init]) {

        _currentItem = item;
        [self setUpItem:item];

        _status = AFSoundStatusNotStarted;
    }
    return self;
}

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.