开发者

Play any audio for given time

I want to play any file for 6 seconds. Also suppose the audio is bigger then 6 sec the application will play only for 6 sec.and if it is less开发者_高级运维 then 6 sec then play continuously. So is there any inbuilt option from any framework?


A Simple Way To Play MP3 Audio Files


Assuming you use an AVAudioPlayer, send a -stop message after 6 seconds:

NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:6
                               target:thePlayer selector:@selector(stop)
                                    userInfo:nil repeats:NO];

(Remember to invalidate the timer if the player stops early.)


If you are using AVAudioPlayer you can make use of an NSTimer as mentioned by @Kenny. Just to add to add to that answer.

NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:6
                               target:thePlayer selector:@selector(stopPlayer)
                                    userInfo:nil repeats:NO];

- (void)stopPlayer
{
[audioPlayer stop];
}

In case the duration of your audio file is less than 6 seconds or your desired limit you should continue to play right? so you must set the numberOfLoops for the audio player instance to continue playback. Here is the reference

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜