2016-03-07 106 views
0

我用這莢從StackOverflow的:https://github.com/piemonte/Player防止音樂停止的應用程序

但是我有一個問題。當用播放器打開視圖時,我的音樂應用停止播放。我怎麼能讓它不停止?

這是球員代碼:

self.player = Player() 
self.player.delegate = self 
player.view.frame.size.width = UIScreen.mainScreen().bounds.size.width 

player.view.frame.size.height = player.view.frame.size.width 

player.fillMode = AVLayerVideoGravityResizeAspectFill 
player.view.sizeToFit() 
videoCell!.addSubview(player.view) 

let videoUrl: NSURL = fileUrl! 
self.player.setUrl(videoUrl) 
self.player.playFromBeginning() 

self.player.playbackLoops = true 

let tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action:"handleTapGestureRecognizer:") 
tapGestureRecognizer.numberOfTapsRequired = 1 
self.player.view.addGestureRecognizer(tapGestureRecognizer) 

回答

0

把這個的appDelegate,希望這有助於:)

#import <AVFoundation/AVFoundation.h> 
#import <AudioToolbox/AudioToolbox.h> 

- (BOOL)application:(UIApplication *)application 
willFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 

NSError *setCategoryError = nil; 
BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError]; 
if (!success) { /* handle the error condition */ } 

NSError *activationError = nil; 
success = [audioSession setActive:YES error:&activationError]; 
if (!success) { /* handle the error condition */ } 

} 

斯威夫特版本

var audioPlayer = AVAudioPlayer() 

func playAudio() { 
    do { 
     if let bundle = NSBundle.mainBundle().pathForResource("Flip 02", ofType: "wav") { 
      let alertSound = NSURL(fileURLWithPath: bundle) 
      try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) 
      try AVAudioSession.sharedInstance().setActive(true) 
      try audioPlayer = AVAudioPlayer(contentsOfURL: alertSound) 
      audioPlayer.prepareToPlay() 
      audioPlayer.play() 
     } 
    } catch { 
     print(error) 
    } 
} 
+0

我試過,但我會在錯誤兩個'audioSession'行。請提供 –

+0

,你能否提供錯誤文字..? – typedef

+0

是的,看看這裏:http://s18.postimg.org/i4yahz98p/Scr​​een_Shot_2016_03_07_at_21_59_37.png –