Lan Yi iOS Learning audio playback

Source: Internet
Author: User

Prepare song data

NSString *path = [[NSBundle mainbundle] pathforresource:@ "Alan Walker-fade.mp3" oftype:nil];

NSData *musicdata = [NSData Datawithcontentsoffile:path];

Initializing the audio player (based on NSData initialization)

Nserror *errormsg = nil;

Self.player = [[Avaudioplayer alloc] Initwithdata:musicdata error:&errormsg];

ErrorMsg = = nil?: NSLog (@ "%@", errormsg);

self.player.meteringEnabled = YES; Enable level

Self.player.enableRate = YES; Enable playback of variable speed

Self.player.numberOfLoops =-1; 0 plays only once, 1 infinite loops, positive numbers are played several times

NSLog (@ "%@", self.player.settings); Show audio file most basic information

Timer Update Play progress bar

[Nstimer scheduledtimerwithtimeinterval:.1

Target:self

Selector: @selector (Updateprogressview)

Userinfo:nil

;

Self.slid.minimumValu Repeats:yes];

To reset the initial value of a Slider

Self.slid.value = 0e = 0;

Self.slid.maximumValue = self.player.duration;

}

-(void) Updateprogressview {

Progress's progress range is 0~1.

Current Playback progress = Current broadcast-put Time ÷ total time

Duration Total Audio Time

CurrentTime Current Playback time

self.jindu.progress = self.player.currenttime/self.player.duration;

Level

[Self.player Updatemeters];

[Self.pv0 setprogress: ([self.player averagepowerforchannel:0] + +)/animated:yes];

[Self.pv1 setprogress: ([self.player averagepowerforchannel:1] + +)/animated:yes];

}

-(Ibaction) Bofang: (UIButton *) Sender {

Delay playback (current device playback time + delay time)

[Self.player Playattime:[self.player CurrentTime] + 10];

One button for pause, play function

if (self.player.playing) {

[Self.player pause];

[Sender setimage:[uiimage imagenamed:@ "1"] forstate:uicontrolstatenormal];

} else {

[Self.player play];

[Sender Setimage:[uiimage imagenamed:@ "2"] forstate:uicontrolstatenormal];

}

}

-(Ibaction) SLI: (UISlider *) Sender {

Self.player.volume = Sender.value; 0~1, after 1 is equivalent to gain, the sound quality drops

Self.player.rate = Sender.value; 0.5~2, playback speed factor, 1 is the default (original speed)

Self.player.currentTime = Sender.value;

Self.player.rate = 50;

----------------------Functional Partitioning-----------------------------

Self.player.volume = Sender.value; Volume control [0,1], after 1 is equivalent to the gain, the sound quality drops, the negative will become maximum

Self.player.rate = Sender.value; Play Speed factor (0,+∞], need to be greater than 0, 0~1 is spin down, 1~+∞ is accelerated. Corresponds to the maximum value of the Slidert to be initialized Gaiyixia

Self.player.currentTime = Sender.value; Current playback time (modify playback progress via Slider)

}

Lan Yi iOS Learning audio playback

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.