iOS--about playing songs in itunes

Source: Internet
Author: User

For details, see: Http://www.cnblogs.com/kenshincui/p/4186022.html is well written.

#import <Foundation/Foundation.h> @protocol mymusicplayer <NSObject> @optional @optional/** * Current Playback time * * @ param time */-(void) theprocess: (nstimeinterval) time;;/ * * Playback status change */-(void) playbackstatechanged;/** * Volume change */-(void) playvolumechanged;/** * song change */-(void) playitemchanged;//* #import <Foundation/Foundation.h> #import " MyMusicPlayer.h "#import <mediaplayer/mediaplayer.h>typedef ns_enum (Nsinteger, playmode) {Reaptone = 1, Rand omlist= 2, Reaptlist = 3,}; @protocol Mymusicplayer; @interface kbmediaplayer:nsobject@property (nonatomic,weak) ID <MyMusicPlayer> delegate; @property (nonatomic) mpmediaquery *mediasource; @property (nonatomic, strong) Nstimer * Time; @property (nonatomic, weak) Mpmusicplayercontroller *musicplayer; @property (nonatomic, copy) Nsarray * Itemsfromgenericquery, @property (nonatomic) PlayMode play;//+ (instancetype) sharedinstance;-(void) playaction;-( void) stopaction;-(void) pauseaction;-(void) playnextone;-(void) playlastone;-(void) dolloc;//************************************* #import "KBMediaplayer.h" @implementation kbmediaplayer-(instancetype) init{self = [Super init    ];        if (self) {self.mediasource = [[Mpmediaquery alloc] init];                Self.itemsfromgenericquery = [Self.mediasource items]; if ([[Uidevice currentdevice].systemversion floatvalue]>=8.0) {self.musicplayer = [Mpmusicplayercontroller]        Systemmusicplayer];        }else{Self.musicplayer = [Mpmusicplayercontroller Ipodmusicplayer]; } switch (self.play) {case reaptone: [Self.musicplayer setrepeatmode:mpmusicrep                Eatmodeone];            Break                Case reaptlist: [Self.musicplayer Setrepeatmode:mpmusicrepeatmodeall];            Break    Case randomlist: [Self.musicplayer Setshufflemode:mpmusicshufflemodedefault];            Break        Default:break;                } [Self.musicplayer SetQueueWithQuery:self.mediaSource]; Self.time = [Nstimer scheduledtimerwithtimeinterval:0.5 target:self selector: @selector (updateslidervalue) UserInfo:                Nil Repeats:yes];                   [Self setnotification]; } return self;}        -(void) setnotification{nsnotificationcenter *notificationcenter = [Nsnotificationcenter defaultCenter]; [Notificationcenter addobserver:self selector: @selector (handle_nowplayingitemchanged) Name:mpmus Icplayercontrollernowplayingitemdidchangenotification object:self.musicplayer];//Playback of items changes when notified [notification Center addobserver:self selector: @selector (handle_playbackstatechanged) Name:mpmusicplayercontro Llerplaybackstatedidchangenotification Notification of object:self.musicplayer];//status change [Notificationcenter addObse  Rver:self selector:  @selector (handle_playvolumechanged) Name:mpmusicplayercontrollervolumedidchangenotification object: self.musicplayer];//volume notification [self.musicplayer begingeneratingplaybacknotifications];} -(void) handle_nowplayingitemchanged{if ([Self.delegate respondstoselector: @selector (playitemchanged)]) {[Self].    Delegate playitemchanged];        }}-(void) handle_playbackstatechanged{if ([Self.delegate respondstoselector: @selector (playbackstatechanged)]) {    [Self.delegate playbackstatechanged]; }}-(void) handle_playvolumechanged{if ([Self.delegate respondstoselector: @selector (playvolumechanged)]) {[Self].    Delegate playvolumechanged]; }}-(void) playaction{[Self.musicplayer play];} -(void) stopaction{[Self.musicplayer stop];} -(void) pauseaction{[self.musicplayer pause];} -(void) Changeplayermode: (nsinteger) mode{switch (mode) {Case 1: [Self.musicplayer setrepeatmode:mpm           Usicrepeatmodeone]; Break            Case 2: [Self.musicplayer Setshufflemode:mpmusicshufflemodedefault];        Break            Case 3: [Self.musicplayer Setrepeatmode:mpmusicrepeatmodeall];        Break    Default:break;        }}-(void) playandpausemusic{mpmusicplaybackstate playbackstate = [Self.musicplayer playbackstate]; if (playbackstate = = Mpmusicplaybackstatestopped | |    Playbackstate = = mpmusicplaybackstatepaused) {[Self.musicplayer play];    } else if (playbackstate = = mpmusicplaybackstateplaying) {[Self.musicplayer pause];        }}-(void) playnextone{[Self.musicplayer Skiptonextitem];            if ([self.musicplayer playbackstate] = = Mpmusicplaybackstatestopped | |        [Self.musicplayer playbackstate] = = mpmusicplaybackstatepaused) {[Self.musicplayer play];        }}-(void) playlastone{[Self.musicplayer Skiptopreviousitem]; if ([self.musicplayer playbackstate] = = Mpmusicplaybackstatestopped | |        [Self.musicplayer playbackstate] = = mpmusicplaybackstatepaused) {[Self.musicplayer play]; }}-(void) updateslidervalue{if ([Self.delegate respondstoselector: @selector (theprocess:)]) {[Self.delegate TheP    Rocess:self.musicPlayer.currentPlaybackTime]; }}-(void) dolloc{[[Nsnotificationcenter Defaultcenter] removeobserver:self Name:mpmusicplayercontro        Llernowplayingitemdidchangenotification Object:self.musicPlayer]; [[Nsnotificationcenter Defaultcenter] removeobserver:self name:mpmusicplayercontrollerplaybackstatedid        Changenotification Object:self.musicPlayer]; [[Nsnotificationcenter Defaultcenter] removeobserver:self Name:mpmusicplayercontrollervolumedidchangen        Otification Object:self.musicPlayer];    [Self.musicplayer endgeneratingplaybacknotifications];    [Self.musicplayer stop]; [_time invalidate];} @end//*************@interface Viewcontroller () <mymusicplayer>{kbmediaplayer *musicplayer;}    @end @implementation viewcontroller-(void) viewdidload {[Super viewdidload];    Musicplayer=[[kbmediaplayer Alloc]init];        musicplayer.delegate=self;    Uitextfield *txet=[[uitextfield Alloc]init];        Additional setup after loading the view, typically from a nib. Self.view.backgroundcolor=rgb (245, 165, 45);}    -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} -(Ibaction) ButtonPress: (ID) Sender {[Musicplayer playaction];} Appdelegate Add the following code to stop playback after killing the program-(void) Applicationdidenterbackground: ( UIApplication *) Application {//Use the method to release shared resources, save user data, invalidate timers, and St    Ore enough application state information-to-restore your application to their current state in case it is terminated later. If yourApplication supports background execution, this method is called instead of Applicationwillterminate:when the user quits. __block uibackgroundtaskidentifier identifier = [[UIApplication sharedapplication] Beginbackgroundtaskwithexpi rationhandler:^{if (identifier! = uibackgroundtaskinvalid) {[[UIApplication sharedapplication] Endback            Groundtask:identifier];        identifier = Uibackgroundtaskinvalid; }    }];} -(void) Applicationwillterminate: (uiapplication *) application {//called when the application was about to terminate. S Ave data if appropriate.        See also Applicationdidenterbackground:.    [[Mpmusicplayercontroller Ipodmusicplayer]pause]; [[Mpmusicplayercontroller ipodmusicplayer]stop];}

  

iOS--about playing songs in itunes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.