我是斯威夫特傢伙回去做一些Objc工作,我想一個聲音添加到我的應用程序,問題在於,path
是null
試圖找到時文件定位音頻文件的目標C發揮
該文件位於myProj/Resources/Sounds/genericButtonPress.wav
NSString *path = [[NSBundle mainBundle] pathForResource:@"myProj/Resources/Sounds/genericButtonPress" ofType:@"wav"];
NSLog(@"%@", path); // NULL
NSURL *url = [NSURL fileURLWithPath:path];
AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:NULL];
[player play];
從文檔,它看起來像pathForResource
必須是絕對的,但我也試圖與剛剛genericButtonPress
爲我的字符串。
想在這裏得到一些幫助。謝謝!
您是否試過'NSString * path = [[NSBundle mainBundle] pathForResource:@「genericButtonPress」ofType:@「wav」];'? –
我做到了。沒有運氣。 –
對於你的問題,你說'wav'文件名是'mysound'。但是在你的代碼中你正在尋找'genericButtonPress'。你確定你沒有使用錯誤的名字嗎?嘗試'NSString * path = [[NSBundle mainBundle] pathForResource:@「myProj/Resources/Sounds/mysound」ofType:@「wav」];'。 –