1

I have a MP3 file on my server that is 30 seconds long. I only want to play the first 5 seconds as a "preview".

Currently I am loading the full MP3 and then playing the file through AVAudioPlayer for the first 5 seconds. The issue I have with this is if the user is not on a WiFi network, this eventually can become expensive in terms of the user's data plan. I thought of finding a way to cut off downloading of the file after x amount (which I would have to calculate) of data is sent but not sure if this would be the right solution.

Any suggestions are appreciated.

MMiroslav
  • 1,672
  • 20
  • 32
SierraMike
  • 1,539
  • 1
  • 11
  • 18

2 Answers2

1

If you want to stream audio, try AVPlayer.

You can use NStimer:

[NSTimer scheduledTimerWithTimeInterval:numberOfSeconds
    target:self
    selector:@selector(stopAudio)
    userInfo:nil
    repeats:NO];

When you start streaming just fire timer and in stopAudio method stop streaming audio after numberOfSeconds seconds.

MMiroslav
  • 1,672
  • 20
  • 32
  • I am using similar code for stopping the audio but did not know about AVPlayer. Let me try this out to make sure I do not have any more questions before approving answer. Thank you as well! – SierraMike Jun 23 '15 at 14:48
  • If the user has a fast + expensive phone plan, it is quite possible that all 30 seconds are downloaded within 5 seconds. – gnasher729 Jun 23 '15 at 16:05
  • I'm not to worried about how fast something is downloaded/streamed. @gnasher729 Just the application I am creating has lots of "previews" being played at once and the current way downloaded the whole file where as I rather just stream the first 5 seconds instead saving the user data. I found this out the hard way because I went over my data plan twice before finding this was one of the main causes =P – SierraMike Jun 23 '15 at 16:31
0

According to this post, AVA is not the best for streaming audio :

Streaming with an AVAudioplayer

Hope it helps

Community
  • 1
  • 1
c00ler
  • 1
  • 2