当我按下“yourButton”或“yourButton2”或“yourButton3”时,每个人都播放一个音频文件,当播放音频时,UIButton被设置为选中状态。
我希望“yourButton4”能够连续实现其他UIButton的方法。 (首先,播放音频文件并设置选择“yourButton”然后“yourButton2”和“yourButton3”)。
然而,当我按下“yourButton4”时,“yourButton2”和“yourButton3”会同时播放play()方法异步播放声音。
let url1 = Bundle.main.bundleURL.appendingPathComponent("music1.mp3")
let url2 = Bundle.main.bundleURL.appendingPathComponent("music2.mp3")
let url3 = Bundle.main.bundleURL.appendingPathComponent("music3.mp3")
@IBOutlet weak var yourButton: customButton!
@IBOutlet weak var yourButton2: customButton!
@IBOutlet weak var yourButton3: customButton!
@IBOutlet weak var yourButton4: customButton!
fileprivate var player1:AVAudioPlayer?
fileprivate var player2:AVAudioPlayer?
fileprivate var player3:AVAudioPlayer?
@IBAction func pushButton1(sender: UIButton) {
Player(url: url1)
}
@IBAction func pushButton2(sender: UIButton) {
Player1(url: url2)
}
@IBAction func pushButton3(_ sender: UIButton) {
Player2(url: url1, url2: url2, url3: url3)
}
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
if (player === player1) {
yourButton.isSelected = false
} else if (player === player2) {
yourButton2.isSelected = false
} else if (player === player3) {
yourButton.isSelected = false
player2!.play()
yourButton2.isSelected = true
player2!.play()
yourButton3.isSelected = true
player1!.play()
}
}
func Player(url: URL) {
do {
try player1 = AVAudioPlayer(contentsOf:url)
player1!.play()
yourButton.isSelected = true
player1!.delegate = self
} catch {
print(error)
}
}
func Player1(url: URL) {
do {
try player2 = AVAudioPlayer(contentsOf:url)
player2!.play()
yourButton2.isSelected = true
player2!.delegate = self
} catch {
print(error)
}
}
func Player2(url: URL, url2: URL, url3: URL) {
do {
try player3 = AVAudioPlayer(contentsOf:url)
try player2 = AVAudioPlayer(contentsOf: url2)
try player1 = AVAudioPlayer(contentsOf: url3)
player3!.play()
yourButton.isSelected = true
player3!.delegate = self
player2!.delegate = self
player1!.delegate = self
} catch {
print(error)
}
}
答案 0 :(得分:0)
你正在同时玩所有游戏,没有任何延迟或没有等待前一个完成。
您的班级应该成为AVAudioPlayer
的代表,因此您首先播放第一个音频文件,一旦您获得代理电话audioPlayerDidFinishPlaying:successfully:
,您就会播放下一个音频文件。
你应该修改你的委托调用,看起来像这样:
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
if (player === player1) {
player2!.play()
} else if (player === player2) {
player3!.play()
} else if (player === player3) {
// You're done!
}
}
答案 1 :(得分:0)
不要在VC中使用这3种不同
var player1:AVAudioPlayer?
而不是在AppDelegate中仅使用其中一个并从VC访问它,因此您不会遇到同样的问题。当下一个音频播放时,前一个音频将停止
在App委托中使用:
func Player1(url: URL) {
do {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
try appDelegate.player1 = AVAudioPlayer(contentsOf:url)
appDelegate.player1!.play()
yourButton2.isSelected = true
appDelegate.player1!.delegate = self
} catch {
print(error)
}
}
再次在VC中使用此实例
import pandas as pd
df = pd.DataFrame({'x': [[1, 2], [3, 4, 5]]})
df_new = df['x'].apply(pd.Series).unstack().reset_index().dropna()
df_new[['level_1',0]]`