2015-04-05 166 views
0

在我的应用程序中,声音播放为无限循环。但是当第一个循环结束时,它会等待2-3秒再次开始。我想禁用循环之间的间隔。你能帮助我吗?禁用播放时间间隔无限循环AVAudio播放器

import UIKit 
import AVFoundation 

class ViewController: UIViewController { 


    var AudioPlayer = AVAudioPlayer(); 

    @IBOutlet weak var playb: UIButton! 

    @IBAction func play(sender: UIButton) { 
     if !AudioPlayer.playing { 

     AudioPlayer.numberOfLoops = -1 
     AudioPlayer.play() 

      playb.setTitle("Stop Rain", forState: UIControlState.Normal) 
     } else { 
     AudioPlayer.stop() 
     playb.setTitle("Start Rain", forState: UIControlState.Normal) 

     } 
    } 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     var rainSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("srs", ofType: "wav")!) 
     AudioPlayer = AVAudioPlayer(contentsOfURL: rainSound, error: nil) 
     AudioPlayer.prepareToPlay() 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


} 
+0

我试过你的代码,它看起来很好...检查你的文件,你有没有尝试过另一个扩展名,如MP3 .. – 2015-04-05 20:21:44

回答

0

你打什么文件格式?

尝试播放非打包文件格式,如.wav或.aiff。格式如MP3和AAC有少量的前导和尾随沉默,我不认为AVAudioPlayer允许您修剪!