在iOS应用中的音频播放:AZSoundManager

jopen 9年前

Intro

AZSoundManager

AZSoundManager is a simple tool for playing sound and music in iOS apps.

Installation

To use the AZSoundManager:

  1. Just drag the class files into your project:

    AZSoundManager.h AZSoundManager.m AZSoundItem.h AZSoundItem.m

  2. Add the AVFoundation framework.

Classes

The AZSoundManager package defines two classes: AZSoundManager and AZSoundItem.

AZSoundItem properties

<pre>@property (nonatomic, readonly) NSString name;</pre>

The name of the item.

<pre>@property (nonatomic, readonly) NSURL
URL;</pre>

The absolute URL of the sound file.

<pre>@property (nonatomic, readonly) NSTimeInterval duration;</pre>

The duration (in seconds) of the sound file.

<pre>@property (nonatomic, readonly) NSTimeInterval currentTime;</pre>

The current time offset (in seconds) of the sound file.

<pre>@property (nonatomic, readonly) NSString title;</pre>

The title from metadata of item.

<pre>@property (nonatomic, readonly) NSString
album;</pre>

The album name from metadata of item.

<pre>@property (nonatomic, readonly) NSString artist;</pre>

The artist name from metadata of item.

<pre>@property (nonatomic, readonly) UIImage
artwork;</pre>

The artwork image from metadata of item.

AZSoundItem creation

+ (instancetype)soundItemWithContentsOfFile:(NSString*)path;  - (instancetype)initWithContentsOfFile:(NSString*)path;  + (instancetype)soundItemWithContentsOfURL:(NSURL*)URL;  - (instancetype)initWithContentsOfURL:(NSURL*)URL;

These methods create a new AZSoundItem instance from a file path or URL.

AZSoundManager properties

<pre>@property (nonatomic, readonly) AZSoundStatus status;</pre>

The status of audio player.

<pre>@property (nonatomic, readonly) AZSoundItem currentItem;</pre>

The current item of audio player.

<pre>@property (nonatomic, assign) float volume;</pre>

The sound volume. Should be in the range 0 - 1.

Usage

Playing a sound

NSString filePath = [[NSBundle mainBundle] pathForResource:@"demo"</span> ofType:@"mp3"</span>];  AZSoundItem item = [AZSoundItem soundItemWithContentsOfFile:filePath];    [[AZSoundManager sharedManager] playSoundItem:item];</pre></div>  

Preloading a sound

NSString filePath = [[NSBundle mainBundle] pathForResource:@"demo"</span> ofType:@"mp3"</span>];  AZSoundItem item = [AZSoundItem soundItemWithContentsOfFile:filePath];    [[AZSoundManager sharedManager] preloadSoundItem:item];  ...  [[AZSoundManager sharedManager] play];</pre></div>  

Playing actions

- (void)play;  - (void)pause;  - (void)stop;  - (void)restart;  - (void)playAtSecond:(NSTimeInterval)second;

Get actual info about playing item

NSString filePath = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"mp3"];  AZSoundItem item = [AZSoundItem soundItemWithContentsOfFile:filePath];    [[AZSoundManager sharedManager] playSoundItem:item];    [[AZSoundManager sharedManager] getItemInfoWithProgressBlock:^(AZSoundItem item) {      NSLog(@"Item duration: %ld - current time: %ld", (long)item.duration, (long)item.currentTime);  } completionBlock:^{      NSLog(@"finish playing");  }];</pre>

项目主页:http://www.open-open.com/lib/view/home/1439035315192