1

I want to play the music from internet by url. I create a simply project that has one button with the following code:

NSURL *url = [[NSURL alloc] initWithString:@"http://someURL.mp3"];
NSError **err;
QTMovie *movie = [[QTMovie alloc] initWithURL:url error:err];
[movie play];

It works but with some delay (I think because it waits while file has been fully downloaded).
So what I need to do that the music starts to play immediately (when, for example, 10% of file has been downloaded)?

void
  • 718
  • 7
  • 21

2 Answers2

1

If you can consider displaying a Quicktime window over your app, you can use this code :

NSString *url = @"http://someURL.mp3";
UIWebView* tempAudioPlayer = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)]; 
[tempAudioPlayer loadHTMLString:[NSString stringWithFormat:@"<iframe frameborder=\"0\" width=\"0\" height=\"0\" src=\"%@\"></iframe>", url] baseURL:nil];
[self addSubview:tempAudioPlayer];

I first create a UIWebView which will not be displayed. Then I loadHTMLString a <iframe> in it, with the URL of my file as the src value. And I add it to my view. Quicktime appears immediately.

Pierre Espenan
  • 3,996
  • 5
  • 33
  • 51
1

Use -[QTMovie autoplay] to automatically play the music once enough data has been downloaded.

In your case:

NSURL *url = [[NSURL alloc] initWithString:@"http://someURL.mp3"];
NSError **err;
QTMovie *movie = [[QTMovie alloc] initWithURL:url error:err];
[movie autoplay];

From the QTMovie class reference:

The autoplay method configures a QTMovie object to begin playing as soon as enough data is available that the playback can continue uninterrupted to the end of the movie.

inket
  • 1,641
  • 16
  • 21