Simple audio playback for iOS

Source: Internet
Author: User

Because I have not done the Audio class project before, so this time I wrote a small demo of audio to achieve the function of pausing the play cycle. Look directly at the code:

I use the Avaudioplayer, first pilot the storage file, write the top file, sign the agent #import "ViewController.h" #import <AVFoundation/AVFoundation.h> typedef ns_enum (Nsinteger, Playstatus) {//This enumeration is used to control the pause and play Transitions    Playstatusno,    playstatusyes,    }; @interface Viewcontroller () <AVAudioPlayerDelegate> @property (nonatomic, assign) Playstatus Staus; @property (Nonatomic, Retain) UISlider *slider; Progress bar @property (nonatomic, retain) UILabel *timelabel; @end

  

As for the control creation, I don't write it all out. To this step is to initialize the player self.player = [[Avaudioplayer alloc] Initwithcontentsofurl:[nsurl fileurlwithpath:[[ NSBundle Mainbundle] pathforresource:@ "1" oftype:@ "MP3"]] error:nil];    _player.delegate = self;    [_player Preparetoplay]; Assign the required resources to play and add them to the internal play queue (pre-play)

  

  use Nstimer to monitor audio playback progress        //  book a timer and set a time interval. Represents a value that enters a time interval object, in seconds, of a >0 floating-point type, and if the value is <0, the system defaults to the object sent by the 0.1 self table useinfo This parameter can be nil, and when the timer fails, the timer is reserved and freed by the object you specify repeat When yes, the timer loops continuously until it expires or is released, and when no, the timer is sent out once and then fails.    _time = [Nstimer scheduledtimerwithtimeinterval:0.1 target:self selector: @selector (updateslider) Userinfo:nil repeats:yes];//played    uiswitch *loop = [[Uiswitch alloc] Initwithframe:cgrectmake (+, +, +)];    [Loop addtarget:self Action: @selector (Loopyesorno) forcontrolevents:uicontroleventvaluechanged];    Loop.on = NO; The default state does not open        [Self.view addsubview:loop];

  

Implement click Button Play Pause-(void) buttonclickstart{if (Self.staus = = playstatusyes) {Self.staus = Playstatusno; [_player pause];        Pause playback; [Nstimer scheduledtimerwithtimeinterval:0.1 target:self selector: @selector (Updatemusictimelabel) userinfo:self        Repeats:yes];    [_button settitle:@ "pause playback" forstate:uicontrolstatenormal];        } else {[_player play];//start playing Self.staus = Playstatusyes; [Nstimer scheduledtimerwithtimeinterval:0.1 target:self selector: @selector (Updatemusictimelabel) userinfo:self        Repeats:yes];    [_button settitle:@ "is playing" forstate:uicontrolstatenormal]; }}//Playback time Update-(void) updatemusictimelabel{if ((int) Self.player.currentTime% <) {Self.timeLabel.text = [    NSString stringwithformat:@ "%d:0%d", (int) self.player.currenttime/60, (int) Self.player.currentTime% 60]; } else {self.timeLabel.text = [nsstring stringwithformat:@ "%d:%d", (int) self.player.currenttime/60, (int) self.plAyer.currenttime% 60]; }    }

  

Loop Play-(void) loopyesorno{    _player.numberofloops =-1;//A positive number is played several times-1 is the infinite loop}//progress bar-(void) updateslider{self    . Slider.value = Self.player.currentTime;    } Drag the progress bar to change the playback time and playback position-(void) slidervaluechanged{    [Self.player stop];        [Self.player SetCurrentTime:self.slider.value];        [Self.player Preparetoplay];        [Self.player play];    }

  

In fact, there are a lot of similar content, such as the volume of the size and progress bar definition is similar and more simple, but also can be combined with a mute switch and the like the loop switch.

Simple audio playback for iOS

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.