生活随笔
收集整理的這篇文章主要介紹了
IOS开发基础之音频工具类封装AVAudioPlayer
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
IOS開發基礎之音頻工具類封裝AVAudioPlayer
源碼在我的主頁下面 ,項目名稱是AVAudioPlayer
關鍵性代碼
工具類的封裝
#import <Foundation/Foundation.h>NS_ASSUME_NONNULL_BEGIN
@interface LJAudioTool
: NSObject
+(BOOL
)playMusic
:(NSString
*)filename
;
+(void)pauseMusic
:(NSString
*)filename
;
+(void)stopMusic
:(NSString
*)filename
;
+(void)playSound
:(NSString
*)filename
;
+(void)disposeSound
:(NSString
*)filename
;
@endNS_ASSUME_NONNULL_END
#import "LJAudioTool.h"
#import <AVFoundation/AVFoundation.h>
@implementation LJAudioTool
static NSMutableDictionary
*_soundIDs
;
+(NSMutableDictionary
*)soundIDs
{if(!_soundIDs
){_soundIDs
= [NSMutableDictionary dictionary
];}return _soundIDs
;
}
+(void)playSound
:(NSString
*)filename
{if(!filename
) return;SystemSoundID soundID
= [[self soundIDs
][filename
] unsignedIntValue
];if(!soundID
){NSURL
*url
= [[NSBundle mainBundle
] URLForResource
:filename withExtension
:nil
];if(!url
) return;OSStatus status
=AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull
)(url
), &soundID
);NSLog(@"%d",status
); [self soundIDs
][filename
] = @(soundID
);}AudioServicesPlaySystemSound(soundID
);
}+(void)disposeSound
:(NSString
*)filename
{if(!filename
) return;SystemSoundID soundID
= [[self soundIDs
][filename
] unsignedIntValue
];if(soundID
){AudioServicesDisposeSystemSoundID(soundID
);[[self soundIDs
] removeObjectForKey
:filename
];}
}
static NSMutableDictionary
*_musicPlayers
;
+(NSMutableDictionary
*)musicPlayers
{if(!_musicPlayers
){_musicPlayers
= [NSMutableDictionary dictionary
];}return _musicPlayers
;
}
+ (BOOL
)playMusic
:(NSString
*)filename
{if(!filename
) return NO
;AVAudioPlayer
*player
= [self musicPlayers
][filename
];if(!player
){NSURL
*url
= [[NSBundle mainBundle
] URLForResource
:filename withExtension
:nil
];if(!url
) return NO
;player
= [[AVAudioPlayer alloc
] initWithContentsOfURL
:url error
:nil
];if(![player prepareToPlay
]) return NO
;[self musicPlayers
][filename
]=player
;}if(!player
.isPlaying
){return [player play
];}return YES
;
}
+ (void)pauseMusic
:(NSString
*)filename
{if(!filename
) return;AVAudioPlayer
* player
= [self musicPlayers
][filename
];if(player
.isPlaying
){[player pause
];}
}
+ (void)stopMusic
:(NSString
*)filename
{if(!filename
) return;AVAudioPlayer
* player
= [self musicPlayers
][filename
];[player stop
];[[self musicPlayers
] removeObjectForKey
:filename
];
}
@end
#import "ViewController.h"
#import "LJAudioTool.h"
@interface ViewController
()
@property(nonatomic
,strong
)NSArray
*songs
;
@property(nonatomic
,assign
)int currentIndex
;
@end
@implementation ViewController
- (NSArray
*)songs
{if(!_songs
){self.songs
= @[@"309769.mp3",@"235319.mp3",@"120125029.mp3"];}return _songs
;
}
- (IBAction
)play
:(id
)sender
{
[LJAudioTool playSound
:@"buyao.wav"];
}
- (IBAction
)pause
:(id
)sender
{[LJAudioTool pauseMusic
:self.songs
[self.currentIndex
]];
}
- (IBAction
)stop
:(id
)sender
{[LJAudioTool stopMusic
:self.songs
[self.currentIndex
]];
}
- (IBAction
)next
:(id
)sender
{
[self stop
:nil
];self.currentIndex
++;if(self.currentIndex
>=self.songs
.count
){self.currentIndex
= 0;}[self play
:nil
];
}
- (void)viewDidLoad
{[super viewDidLoad
];
}
@end
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的IOS开发基础之音频工具类封装AVAudioPlayer的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。