日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IOS开发基础之音频工具类封装AVAudioPlayer

發布時間:2023/12/18 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS开发基础之音频工具类封装AVAudioPlayer 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

IOS開發基礎之音頻工具類封裝AVAudioPlayer

源碼在我的主頁下面 ,項目名稱是AVAudioPlayer

關鍵性代碼
工具類的封裝

// // LJAudioTool.h // AVAudioPlayer // // Created by 魯軍 on 2021/4/23. //#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 // // LJAudioTool.m // AVAudioPlayer // // Created by 魯軍 on 2021/4/23. // #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); //0 代表成功[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 // // ViewController.m // AVAudioPlayer // // Created by 魯軍 on 2021/4/23. // #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 playMusic:self.songs[self.currentIndex]];[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 { // [LJAudioTool stopMusic:self.songs[self.currentIndex]];[self stop:nil];self.currentIndex++;if(self.currentIndex>=self.songs.count){self.currentIndex = 0;}[self play:nil]; // [LJAudioTool playMusic:self.songs[self.currentIndex]]; } - (void)viewDidLoad {[super viewDidLoad]; } @end 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的IOS开发基础之音频工具类封装AVAudioPlayer的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。