In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how iOS realizes the lock screen page to control music playback. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
The details are as follows
/ 1. Adjust the audio session settings to ensure that the audio will continue to play / / 2 when the application enters the background or when the mute switch is turned on, the media information is displayed in the lock screen / / 3, and the space on the lock screen can control the audio playback # import "ViewController.h" # import # import @ 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:@ "playing audio in the background" 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 / / start writing code to adjust the audio session settings to ensure that the audio will continue to play NSError * categoryError = nil even if the application enters the background or the mute switch is turned on [session setCategory:AVAudioSessionCategoryPlayback error:&categoryError]; / / end_code}-(void) playMusicInBackground: (id) sender {if ([self.audioPlayer isPlaying]) {[self.audioPlayer stop]; [self.playButton setTitle:@ "is playing music" 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:@ "stop playing background music" forState:UIControlStateNormal] / / No.2 / / start writing code to display media information on the lock screen and enable controls on the lock screen to control audio playback [[UIApplication sharedApplication] beginReceivingRemoteControlEvents] / / end_code}} / / No.3// starts writing code in response to remote control so that music "play" and "pause"-(void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {if (receivedEvent.type = = UIEventTypeRemoteControl) {switch (receivedEvent.subtype) {case UIEventSubtypeRemoteControlPlay: [self.audioPlayer play]; break; case UIEventSubtypeRemoteControlPause: [self.audioPlayer pause]; break can be controlled after entering the lock screen state Default: NSLog (@ "did not handle this event-receivedEvent.subtype==%ld", (long) receivedEvent.subtype); break;}} / / end_code- (void) didReceiveMemoryWarning {[super didReceiveMemoryWarning] } @ end, this is the end of the article on "how to lock the screen page to control music playback in iOS". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.