मैं अपनी पहली प्रोग्रामिंग भाषा के रूप में स्विफ्ट सीख रहा हूं।स्विफ्ट 2/AVPlayer में पृष्ठभूमि ऑडियो को फिर से शुरू कैसे करें?
मैं रुकावट (जैसे कॉल) के बाद फिर से शुरू पृष्ठभूमि ऑडियो प्लेबैक के लिए कई घंटे के लिए संघर्ष किया गया है
क्या होना चाहिए:
- ऑडियो चलता रहता है जब एप्लिकेशन (काम करता है पृष्ठभूमि में चला जाता)
- कॉल द्वारा बाधित होने पर, रुकावट के लिए अधिसूचना प्राप्त करें (काम करता है)
- जब कॉल समाप्त होता है, रुकावट के लिए नोटिफिकेशन प्राप्त होता समाप्त होता है (काम)
- फिर से शुरू ऑडियो चला रहा (काम नहीं करता है - कुछ भी नहीं सुनना)
सच किसी भी मदद की सराहना करते हैं! धन्यवाद
नोट्स:
- एप्लिकेशन पृष्ठभूमि ऑडियो के लिए पंजीकृत है और रुकावट से पहले ठीक निभाता है
- मैं खेल फिर से शुरू करने के साथ और समय देरी के बिना की कोशिश की है - न तो काम
कोड:
import UIKit
import AVFoundation
var player: AVQueuePlayer!
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true, withOptions: .NotifyOthersOnDeactivation)
} catch { }
let songNames = ["music"]
let songs = songNames.map { AVPlayerItem(URL:
NSBundle.mainBundle().URLForResource($0, withExtension: "mp3")!) }
player = AVQueuePlayer(items: songs)
let theSession = AVAudioSession.sharedInstance()
NSNotificationCenter.defaultCenter().addObserver(self,
selector:"playInterrupt:",
name:AVAudioSessionInterruptionNotification,
object: theSession)
player.play()
}
func playInterrupt(notification: NSNotification) {
if notification.name == AVAudioSessionInterruptionNotification
&& notification.userInfo != nil {
var info = notification.userInfo!
var intValue: UInt = 0
(info[AVAudioSessionInterruptionTypeKey] as! NSValue).getValue(&intValue)
if let type = AVAudioSessionInterruptionType(rawValue: intValue) {
switch type {
case .Began:
print("aaaaarrrrgggg you stole me")
player.pause()
case .Ended:
let timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "resumeNow:", userInfo: nil, repeats: false)
}
}
}
}
func resumeNow(timer : NSTimer) {
player.play()
print("attempted restart")
}
}
तरीकों ठीक से कहा जाता है करता है? विशेष रूप से फिर से शुरू करें विधि। –
हां - मुझे कॉल समाप्त होने के 3 सेकंड बाद "पुनः प्रारंभ करने का प्रयास" दिखा रहा है। –