【问题标题】:Audio looping in Objective-C/iPhoneObjective-C/iPhone 中的音频循环
【发布时间】:2010-03-19 09:49:00
【问题描述】:

所以,我正在完成一个 iPhone 应用程序。

我有以下代码来播放文件:

while(![player isPlaying]) {
  totalSoundDuration = soundDuration + 0.5; //Gives a half second break between sounds
  sleep(totalSoundDuration); //Don't play next sound until the previous sound has finished
  [player play]; //Play sound
  NSLog(@" \n Sound Finished Playing \n"); //Output to console
}

由于某种原因,声音播放一次,然后代码循环并输出以下内容:

Sound Finished Playing
Sound Finished Playing
Sound Finished Playing
etc...

这只会永远重复,我想你们这些可爱的人都无法理解这可能是什么?

干杯!

【问题讨论】:

    标签: iphone objective-c xcode iphone-sdk-3.0


    【解决方案1】:

    我不确定您的代码有什么问题,可能是 [player play] 是一个异步调用,但是您永远循环,而没有让播放器真正开始播放或意识到它实际上正在播放。我不建议在任何 iPhone 应用程序中使用 sleep,因为整个模型都是基于异步事件的。

    我没有测试这段代码,甚至没有编译它,但我希望你能从中得到灵感。

    - (void) startPlayingAgain:(NSTimer *)timer { AVAudioPlayer *player = timer.userInfo; [播放器播放]; } - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player 成功:(BOOL)标志 { NSTimer *timer = [NSTimer scheduleTimerWithTimeInterval:0.5 目标:自己 选择器:@selector(startPlayingAgain:) 用户信息:玩家 重复:否]; } - (void)startPlaying:(NSString *)url { AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url 错误:NULL]; player.delegate = 自我; [播放器播放]; }

    【讨论】:

    • 你的意思是像:-(void)audioPlayerDidFinishPlaying:(nsstring *)audioPlayer finished:(BOOL *)finished context:(void *)context { } - 任何机会你可以弹出一个简单的例子请问我? :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    相关资源
    最近更新 更多