Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Monaj

macrumors regular
Original poster
May 24, 2009
193
0
Hi all,

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

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,

Monaj

-------

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
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Instead of passing nil to the error param pass an error pointer as expected and look at the error to find out what's wrong.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
If you're pasting in that URL and then encoding it again, it'll mess it up. Try creating the URL from the text field directly.
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
Code:
	NSURL *audioUrl = [[NSURL alloc] initWithString:[audioInputTextField stringValue]];
	if (![QTMovie canInitWithURL: audioUrl]) {
		NSLog(@"Cannot init with url");
	}
Though you have to press the "go" (blank button) twice...
I saw that you have a .mp3 in the folder, note that file URL's need to be initialized with - initFileURLWithPath: (NSString *)

(this hung me up for a while while troubleshooting...) :p

EDIT: Oh and that little if statement there was helpful while testing, though I did initially send and print off and NSError * to it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.