2016-12-24 11 views
4

現在、これはボタンを押している間に私のサウンドを再生するために使用しているコードです。Swift 3.0 AVAudioPlayer音楽より音楽を再生する

var player: AVAudioPlayer? 

    let url = Bundle.main.url(forResource: "Sound/yatch", withExtension: "mp3")! 

    do { 
     player = try AVAudioPlayer(contentsOf: url) 
     guard let player = player else { return } 

     player.prepareToPlay() 
     player.play() 
    } catch let error { 
     print(error.localizedDescription) 
    } 

このオーディオは、現在再生中のもの(Spotify、pandora、Musicなど)で再生しようとしています。これをどうやってやりますか?現在再生中の音声の上に再生するには、アプリケーションのオーディオセッションを設定し

答えて

3

try? AVAudioSession.sharedInstance().setActive(true) 
try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient) 

AVAudioSessionヘッダによると、AVAudioSessionCategoryAmbientは音楽とオーディオを再生します、など

/* Use this category for background sounds such as rain, car engine noise, etc. 
Mixes with other music. */ 
public let AVAudioSessionCategoryAmbient: String 
+0

もあります呼び出し音を鳴らすことなくサウンドを再生する方法はありますか?それを振動状態に保つだけです。私はAppleから参照AVFoundationを見ていた何かを見つけることができなかった – CrazyCriss

+0

私はURL http://site4brandz.com/cphp/26/uploads/KaabilHoon.mp3から音を再生したいが、そのエラーエラードメイン= NSOSStatusErrorDomainコード= 2003334207 "(null) –

+0

@MaheshNarla新しい質問をする必要があります。 – JAL

関連する問題