Convert NSData from local NSURL

南楼画角 提交于 2019-12-25 12:07:33

问题


I have a sound player which uses AVAudioPlayer class. I download some items in another view and store the filepath to it as NSURL, then pass the NSURL to the Player viewcontroller. But i get an error while trying to pass the object.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString path]: unrecognized selector sent to instance 0x1700c86c0'

And here is the code:

NSData *data = [NSData dataWithContentsOfFile:[podcastSource path]];
NSError *error;
mPlayer = [[AVAudioPlayer alloc] initWithData:data error:&error];
[mPlayer setDelegate:self];

if (mPlayer == nil)
{
    NSLog(@"%@",error);
}
else
{
    [self.player play];
....
}

Note that the filePath i receive from the NSURL is in this form:

/var/mobile/Applications/xxxxxx/Documents/90989.mp3

回答1:


From the error it seems as if podcastSource is an NSString when it needs to be an NSUrl if you're going to request its path.

But according to the Apple Docs, dataWithContentsOfFile: accepts an NSString as the argument, so no need to convert podcastSource into an NSUrl or to request that NSUrl's path.

If podcastSource is your filename in its entirety, this should work:

NSData *data = [NSData dataWithContentsOfFile:podcastSource];


来源:https://stackoverflow.com/questions/23198644/convert-nsdata-from-local-nsurl

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