问题
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 Bell
and 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