Can't play a sound with a space in the (NSURL *)sound_url

萝らか妹 提交于 2020-01-17 01:20:15

问题


I want to play a sound like this:

#define url(x) [NSURL URLWithString:x]
....

AVAudioPlayer *myFatBeat;
myFatBeat = [[AVAudioPlayer alloc] initWithContentsOfURL:url(@"/Library/Ringtones/Bell% Tower.m4r") error:nil];
[myFatBeat setNumberOfLoops:-1];  
[myFatBeat play];

but it fails. I think it's due to the space between Belland Tower, as it works if the sound name is Àlarm. Then, I tried to escape the sound name like this Bell\ Tower, but I get error: unknown escape sequence: '\040' when compiling.

Any ideas ?


回答1:


I forgot if this is how you do it in an NSUrl, but you can try %20 instead of the space.

myFatBeat = [[AVAudioPlayer alloc]     
initWithContentsOfURL:url(@"/Library/Ringtones/Bell%20Tower.m4r") error:nil];



回答2:


Encode your URL first:

#define url(x) [NSURL URLWithString:[x stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]


来源:https://stackoverflow.com/questions/6165539/cant-play-a-sound-with-a-space-in-the-nsurl-sound-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!