我的包中沒有幾個音頻文件,我想在UITableView
中顯示它們(名稱)。當用戶點擊任何cell
時,應該播放具有該名稱的音頻文件。將文件從Bundle顯示到UITableView - iOS
我使用Google搜索,但我得到的只是從Documents Directory中讀取文件。
如何顯示我的包文件?
請幫忙, 謝謝!
我的包中沒有幾個音頻文件,我想在UITableView
中顯示它們(名稱)。當用戶點擊任何cell
時,應該播放具有該名稱的音頻文件。將文件從Bundle顯示到UITableView - iOS
我使用Google搜索,但我得到的只是從Documents Directory中讀取文件。
如何顯示我的包文件?
請幫忙, 謝謝!
您必須首先獲取指向您的主包的指針,其中存儲了應用程序的資源。使用這個指針,你可以搜索擴展mp3的所有資源。 您可以使用如下代碼:
NSBundle *mainBundle = [NSBundle mainBundle];
NSArray *paths = [mainBundle pathsForResourcesOfType:@"mp3" inDirectory:nil];
在每個路徑的最後一個元素是各自的MP3文件的文件名。
欲瞭解更多信息,請查閱Apple的Bundle Programming Guide。
在的tableview didSelectRowAtIndexPath方法方法編寫此代碼
NSString * soundSelected = cell.textlabel.text;
NSString *path = [NSString stringWithFormat:@"%@/%@.mp3", [[NSBundle mainBundle] resourcePath],soundSelected];
NSURL *soundUrl = [NSURL fileURLWithPath:path];
AVAudioPlayer* cellTapSound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[cellTapSound play];