tags:

views:

29

answers:

2

Hi all,

I am trying to play a .mp3 file on click of a button using this code:

NSString *audioFilePath=[[audioInputTextField stringValue] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *audioUrl = [[NSURL alloc] initWithString:audioFilePath];
QTMovie* soundToPlay = [[QTMovie alloc] initWithURL:audioUrl error:nil];
[soundToPlay play];

Here user is expected to enter a valid url for an audio file in text field and then click button next to it to play, but it is not working for me ie. no audio gets played :(

Can anyone suggest me, if I am doing anything wrong?

Thanks,

Miraaj


The sample code can be downloaded from here- Sample Code download link

One of the example links which I tried was - In the end song

A: 

Is audioFilePath a valid file system path? Is audioUrl a valid file URL or is it nil? To me it looks unlikely since you're giving a url-encoded path and trying to initialize a new URL with that string.

Your audioFilePath should be a valid /Path/To/Folder/File.mp3 and if you have a basic file path, you should create your URL with NSURL's +fileURLWithPath: convenience method.

Joshua Nozzi
thanx for your reply and suggestion... it is a valid path which I gave, eg-http://javamax.free.fr/sonorisation%20et%20radio/Linken%20Park%20-%20In%20The%20End.mp3, but it did not work. You can download the sample application and check it.
Miraaj
I think there can be some problem with the way audio file is buffered and played... but it is a guess I am still searching for its solution!
Miraaj
Ah so it's not a file path as the variable name suggests. :-) This should work. What happens when you set a breakpoint just after the NSURL creation line and inspect audioUrl? Is it valid or nil?
Joshua Nozzi
A: 

You entered a URL with characters already percent-escaped, and then your program added percent escapes. This means that you double-escaped the characters, turning the correct “%20”s into incorrect “%2520”s.

Try it both ways: Try the URL without escaping first, then if it fails, add percent escapes and try again. If that fails, present the error that initWithURL:error: gave you.

Peter Hosey
thanx...for your reply :) I will check it and let you know about it!
Miraaj