1

I have this piece of code for playing audio, but once it is finished, I want to play the same audio again and again, I think I should use numberofloops=-1, but where I need to use this directly. Please help me.

#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
Java_User
  • 1,303
  • 3
  • 27
  • 38
AKumar
  • 11
  • 1

1 Answers1

0

To loop a song use below code after alloc init of avSongItem.

 avSongItem.actionAtItemEnd = AVPlayerActionAtItemEndNone;

More info : Looping a video with AVFoundation AVPlayer?

Also as mentioned in the link use notification.

avSongItem.actionAtItemEnd = AVPlayerActionAtItemEndNone;

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

this will prevent the player to pause at the end.

in the notification:

 - (void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *p = [notification object];
[p seekToTime:kCMTimeZero];
}
Community
  • 1
  • 1
IamAnil
  • 496
  • 5
  • 19
  • I added the code as mentioned below: AVPlayerItem *avSongItem = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[[NSString alloc] initWithFormat:name] ofType:ext]]]; avSongItem.actionAtItemEnd = AVPlayerActionAtItemEndNone; But it is not working – AKumar Oct 14 '15 at 11:55
  • Ok then after that use notification. – IamAnil Oct 14 '15 at 12:17
  • is JetNapMusicPlayer is an instance of AVPlayer? Also do settings as mentioned for AVPlayer instance and not on AVPlayerItem – IamAnil Oct 14 '15 at 12:26
  • Hi Anil, Actually I am a java developer, but i got this work and I do not know abc of Ios, so can you please edit my code and paste the modified one as your answer. I will be so thankful to you. Thanks in advance. I stuck in this issue for last two days..plzzz – AKumar Oct 14 '15 at 12:31
  • Ok try these. Method : playMusic Before "if (avSongItem) ...." add the below [[JetNapMusicPlayer sharedManager] actionAtItemEnd] = AVPlayerActionAtItemEndNone; Try this , if still didnt work then add notification – IamAnil Oct 15 '15 at 07:07
  • Notification code [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:[[JetNapMusicPlayer sharedManager] currentItem]]; – IamAnil Oct 15 '15 at 07:07
  • Then add below method: - (void)playerItemDidReachEnd:(NSNotification *)notification { AVPlayerItem *p = [notification object]; [p seekToTime:kCMTimeZero]; } – IamAnil Oct 15 '15 at 07:08