0

My 'play' method works with a test file I put in the Supporting Files folder called "seconds.m4a", but when I send over the file in the proper format, the URL is said to be invalid.

I have a strong feeling it has something to do with the way I'm creating the 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];

}

CLIENT CODE:

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

    );

});
4

2 回答 2

1

您引用的文件可能不在 中mainBundle,所以我绝对不建议使用它!如果您尝试从 NSURL 获取 NSURL fileURL,如何:

[NSURL URLWithString:fileURL]
于 2013-04-15T10:08:57.633 回答
0
try this ...

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

NSURL * pUrl = [NSURL fileURLWithPath:pFileUrl];
于 2013-04-15T03:53:21.780 回答