2016-03-07 4 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 */ } 

} 

Swift версия

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/Screen_Shot_2016_03_07_at_21_59_37.png –

Смежные вопросы