你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS鎖屏頁面控制音樂播放

iOS鎖屏頁面控制音樂播放

編輯:IOS開發綜合

//1、調整音頻會話設置,確保應用進入後台或靜音開關已開啟時音頻仍將繼續播放 2、鎖屏狀態下顯示媒體信息 3、鎖屏上的空間可以控制音頻播放

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

@interface ViewController ()

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@property (weak, nonatomic)  UIButton *playButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton * playButton = [UIButton buttonWithType:UIButtonTypeSystem];
    playButton.frame = CGRectMake(0, 0, 200, 40);
    playButton.center = self.view.center;
    [playButton setTitle:@"在後台播放音頻" forState:UIControlStateNormal];
    [playButton addTarget:self action:@selector(playMusicInBackground:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:playButton];

    NSError *playerInitError = nil;

    NSString *audioPath =
    [[NSBundle mainBundle] pathForResource:@"background_audio"
                                    ofType:@"mp3"];

    NSURL *audioURL = [NSURL fileURLWithPath:audioPath];

    self.audioPlayer = [[AVAudioPlayer alloc]
                        initWithContentsOfURL:audioURL
                        error:&playerInitError];

    AVAudIOSession *session = [AVAudIOSession sharedInstance];

    NSError *activeError = nil;
    if (![session setActive:YES error:&activeError]) {
        NSLog(@"Failed to set active audio session!");
    }

    //No.1
    //開始寫代碼,調整音頻會話設置,確保即便應用進入後台或靜音開關已開啟,音頻仍將繼續播放

    NSError *categoryError = nil;
    [session setCategory:AVAudIOSessionCategoryPlayback error:&categoryError];


    //end_code


}
- (void)playMusicInBackground:(id)sender {

    if ([self.audioPlayer isPlaying]) {
        [self.audioPlayer stop];

        [self.playButton setTitle:@"正在播放音樂"
                         forState:UIControlStateNormal];

    } else {
        UIImage *lockImage = [UIImage imageNamed:@"belongToMe.jpg"];

        MPMediaItemArtwork *artwork =
        [[MPMediaItemArtwork alloc] initWithImage:lockImage];

        NSDictionary *mediaDict =
        @{
          MPMediaItemPropertyTitle: @"BackgroundTask Audio",
          MPMediaItemPropertyMediaType: @(MPMediaTypeAnyAudio),
          MPMediaItemPropertyPlaybackDuration:
              @(self.audioPlayer.duration),
          MPNowPlayingInfoPropertyPlaybackRate: @1.0,
          MPNowPlayingInfoPropertyElapsedPlaybackTime:
              @(self.audioPlayer.currentTime),
          MPMediaItemPropertyAlbumArtist: @"Some User",
          MPMediaItemPropertyArtist: @"Some User",
          MPMediaItemPropertyArtwork: artwork };

        [self.audioPlayer play];

        [self.playButton setTitle:@"停止播放後台音樂"
                         forState:UIControlStateNormal];

        //No.2
        //開始寫代碼,將媒體信息顯示在鎖定屏幕上,並使鎖屏上控件可以控制音頻播放


        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];


        //end_code
    }
}

//No.3
//開始寫代碼,響應遠程控制,使得進入鎖屏狀態後可以控制音樂“播放”和“暫停”


- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {

    if (receivedEvent.type == UIEventTypeRemoteControl) {
        switch (receivedEvent.subtype) {

            case UIEventSubtypeRemoteControlPlay:
                [self.audioPlayer play];
                break;

            case UIEventSubtypeRemoteControlPause:
                [self.audioPlayer pause];
                break;

            default:
                NSLog(@"沒有處理過這個事件------receivedEvent.subtype==%ld",(long)receivedEvent.subtype);
                break;
        }
    }
}


//end_code

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

以上就是iOS鎖屏頁面控制音樂播放的全文介紹,希望對您學習和使用ios應用開發有所幫助.

【iOS鎖屏頁面控制音樂播放】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved