2015-10-14 2 views
1

У меня есть этот кусок кода для воспроизведения звука, но как только он закончен, я хочу снова и снова воспроизводить один и тот же звук, я думаю, что я должен использовать numberofloops = -1, но там, где мне нужно используйте это напрямую. Пожалуйста, помогите мне.Воспроизведение непрерывного звука в Iphone

#import "JetNapMusicPlayer.h" 
    #import <AVFoundation/AVFoundation.h> 

    @interface JetNapMusicPlayer() 
    @property(nonatomic,strong) AVQueuePlayer *avQueuePlayer; 
    @end 
    static JetNapMusicPlayer *sharedManager = nil; 

    @implementation JetNapMusicPlaye 
    #pragma mark Singleton Methods 
    + (id)sharedManager { 
     @synchronized(self) { 
      if(sharedManager == nil) 
       sharedManager = [[super alloc] init]; 
     } 
     return sharedManager; 
    } 
    - (id)init { 
     if (self = [super init]) { 
    //  [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 

      MPRemoteCommandCenter *rcc = [MPRemoteCommandCenter sharedCommandCenter]; 
      MPRemoteCommand *playCommand = rcc.playCommand; 
      [playCommand setEnabled:YES]; 
      [playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) { 
       [(JetNapMusicPlayer *)[JetNapMusicPlayer sharedManager] play]; 

       return MPRemoteCommandHandlerStatusSuccess; 
      }]; 

      MPRemoteCommand *pauseCommand = rcc.pauseCommand; 
      [pauseCommand setEnabled:YES]; 
      [pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) { 
       [(JetNapMusicPlayer *)[JetNapMusicPlayer sharedManager] pause]; 

       return MPRemoteCommandHandlerStatusSuccess; 
      }]; 
     } 
     return self; 
    } 
    - (void)dealloc { 
     [super dealloc]; 
    } 
    -(AVPlayer *)avQueuePlayer 
    { 
     if (!_avQueuePlayer) { 
      [self initSession]; 
      _avQueuePlayer = [[AVQueuePlayer alloc] init]; 
     } 
     return _avQueuePlayer; 
    } 
    -(void)initSession 
    { 
     [[NSNotificationCenter defaultCenter] addObserver: self 
               selector: @selector(audioSessionInterrupted:) 
                name:  AVAudioSessionInterruptionNotification 
                object:  [AVAudioSession sharedInstance]]; 
     //set audio category with options - for this demo we'll do playback only 
     NSError *categoryError = nil; 
     [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&categoryError]; 
     if (categoryError) { 
      NSLog(@"Error setting category! %@", [categoryError description]); 
     } 
     //activation of audio session 
     NSError *activationError = nil; 
     BOOL success = [[AVAudioSession sharedInstance] setActive: YES error: &activationError]; 
     if (!success) { 
      if (activationError) { 
       NSLog(@"Could not activate audio session. %@", [activationError localizedDescription]); 
      } else { 
       NSLog(@"audio session could not be activated!"); 
      } 
     } 
    } 
    #pragma mark - notifications 
    -(void)audioSessionInterrupted:(NSNotification*)interruptionNotification 
    { 
     NSLog(@"interruption received: %@", interruptionNotification); 
    } 
    #pragma mark - player actions 
    -(void) pause 
    { 
     [[self avQueuePlayer] pause]; 
    } 
    -(void) play 
    { 
     [[self avQueuePlayer] play]; 
    } 
    -(void) clear 
    { 
     [[self avQueuePlayer] removeAllItems]; 
    } 
    #pragma mark - remote control events 
    #pragma mark - Kony FFI 
    + (BOOL)playMusic:(NSString *)filename artistname:(NSString *)artistname songname:(NSString *)songname { 
     NSString *name = [filename stringByDeletingPathExtension]; 
     NSString *ext = [filename pathExtension]; 
     AVPlayerItem *avSongItem = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[[NSString alloc] initWithFormat:name] ofType:ext]]]; 
     if (avSongItem) { 
      [(JetNapMusicPlayer *)[JetNapMusicPlayer sharedManager] clear]; 
      [[[JetNapMusicPlayer sharedManager] avQueuePlayer] insertItem:avSongItem afterItem:nil]; 
      [(JetNapMusicPlayer *)[JetNapMusicPlayer sharedManager] play];  
      [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = @{MPMediaItemPropertyTitle: songname, MPMediaItemPropertyArtist:artistname}; 
     } 
     return YES; 
    } 
    + (BOOL)stopMusic { 
     [(JetNapMusicPlayer *)[JetNapMusicPlayer sharedManager] pause]; 
     [(JetNapMusicPlayer *)[JetNapMusicPlayer sharedManager] clear]; 
     return YES; 
    } 
    @end 
+0

Это может быть полезно: http://stackoverflow.com/a/11376298 – IamAnil

ответ

0

Чтобы закодировать песню, используйте ниже код после выделения init avSongItem.

avSongItem.actionAtItemEnd = AVPlayerActionAtItemEndNone; 

Подробнее: Looping a video with AVFoundation AVPlayer?

Кроме того, как указано в уведомлении использования ссылки.

avSongItem.actionAtItemEnd = AVPlayerActionAtItemEndNone;

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(playerItemDidReachEnd:) 
              name:AVPlayerItemDidPlayToEndTimeNotification 
             object:[avPlayer currentItem]]; 

Это не позволит игроку остановиться в конце.

в уведомлении:

- (void)playerItemDidReachEnd:(NSNotification *)notification { 
AVPlayerItem *p = [notification object]; 
[p seekToTime:kCMTimeZero]; 
} 
+0

я добавил код, как указано ниже: AVPlayerItem * avSongItem = [[AVPlayerItem Alloc] initWithURL : [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource: [[NSString alloc] initWithFormat: name] ofType: ext]]]; avSongItem.actionAtItemEnd = AVPlayerActionAtItemEndNone; Но он не работает – AKumar

+0

Я добавил код в нужное место? – AKumar

+0

Ok после этого использование извещение. – IamAnil

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