使用URLForResource withExtension的音频文件的URL无效

时间:2013-04-15 03:43:38

标签: javascript ios objective-c trigger.io

我的'play'方法适用于我放在名为“seconds.m4a”的Supporting Files文件夹中的测试文件,但是当我send over the file in the proper format时,该URL被认为是无效的。

我强烈认为这与我创建网址的方式有关,

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

PLUGIN CODE:

+ (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)}

    );

});

2 个答案:

答案 0 :(得分:1)

您引用的文件可能不在mainBundle中,所以我绝对不建议使用它!如果你想从fileURL获得NSURL,那么:

[NSURL URLWithString:fileURL]

答案 1 :(得分:0)

try this ...

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

NSURL * pUrl = [NSURL fileURLWithPath:pFileUrl];