簡體   English   中英

在Iphone中播放連續音頻

[英]Playing continuous audio in Iphone

我有這段代碼來播放音頻,但是一旦完成,我想一次又一次地播放相同的音頻,我想我應該使用numberofloops = -1,但是我需要直接使用它。 請幫我。

#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

要循環播放歌曲,請在分配avSongItem的init之后使用以下代碼。

 avSongItem.actionAtItemEnd = AVPlayerActionAtItemEndNone;

更多信息: 使用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];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM