2013-04-15 37 views
0

音频文件的URL无效我的'播放'方法与我放入支持文件文件夹中称为“seconds.m4a”的测试文件一起使用,但当我send over the file in the proper format时,该URL被认为是无效的。使用URLForResource与扩展

我有它是与我创建的URL的方式一种强烈的感觉,

NSURL* url = [[NSBundle mainBundle] URLForResource:fileURL withExtension:@"m4a"]; // THIS FORMS THE INVALID URL 

插件代码:

+ (void)play:(ForgeTask*)task { 



    // parse the file url from the file object 

    ForgeFile* file = [[ForgeFile alloc] initWithFile:[task.params objectForKey:@"file"]]; 

    NSString* fileURL = [file url]; // eg. "/var/mobile/Applications/AC963D11-88EC-4559-9C2E-68F666AC44D5/Library/Application Support/Forge/assets-6B531B6A-AF26-44FB-BCB1-D036BD4A7293/src/audio/seconds.m4a" 



    NSLog(@"Playing file at %@", fileURL); 



    NSURL* url = [[NSBundle mainBundle] URLForResource:fileURL withExtension:@"m4a"]; // THIS FORMS THE INVALID URL 





    // TESTING - THIS WORKS IN THE FORGEINSPECTOR WHEN UNCOMMENTED 

    //url = [[NSBundle mainBundle] URLForResource:@"seconds" withExtension:@"m4a"]; 

    // END TESTING 



    NSAssert(url, @"URL is invalid."); // THIS IS THE ERROR MESSAGE THAT COMES UP 



    // create the player 

    NSError* error = nil; 

    player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; 

    if(!player) 

    { 

     NSLog(@"Error creating player: %@", error); 

    }; 



    [player play]; 



[task success:nil]; 

} 

客户端代码:

forge.file.getLocal('/audio/seconds.m4a', function(file) { 

    debug(null, file); // SHOWS THE PROPER FILE: {"uri":"/var/mobile/Applications/AC963D11-88EC-4559-9C2E-68F666AC44D5/Library/Application Support/Forge/assets-6B531B6A-AF26-44FB-BCB1-D036BD4A7293/src/audio/seconds.m4a"} 

    forge.internal.call(

     'audio.play', 

     {file: file}, 

     function() { alert('Success!') }, 

     function (e) { alert('Error: '+e.message)} 

    ); 

}); 

回答

0
try this ... 

NSString * pFileUrl = [[NSBundle mainBundle] pathForResource:@"seconds" ofType:@"m4a"]; 

NSURL * pUrl = [NSURL fileURLWithPath:pFileUrl]; 
1

您引用的文件可能不在mainBundle,所以我绝对不会推荐使用它!如果你想从fileURL,如何得到一个NSURL:

[NSURL URLWithString:fileURL] 
+0

此错误创建播放器时:'错误创建播放器:错误域= NSOSStatusErrorDomain码= 2003334207 \“操作couldn \ 200 \231吨(OSStatus error 2003334207。)\“' – Garrett