iOS recording, audio playback function

Source: Internet
Author: User

#import <AVFoundation/AVFoundation.h>

{

Nsinteger _timecount;
Nstimer *_timer;

}

@property (Nonatomic,retain) Avaudioplayer *audioplayer;
@property (Nonatomic,retain) Avaudiorecorder *recorder;
@property (nonatomic,assign) Nsinteger Playtag;

/**
* Create Sound Recorder
*/
-(Avaudiorecorder *) recorder{
if (!_recorder) {
Nsurl *url = [self filePath];
Nsdictionary *setting = [nsdictionary dictionarywithobjectsandkeys:[nsnumber numberwithint:avaudioqualitymin], Avencoderaudioqualitykey,[nsnumber Numberwithint:16],avencoderbitratekey,[nsnumber NumberWithInt:2], Avnumberofchannelskey,[nsnumber Numberwithfloat:44100.0],avsampleratekey,nil];
Nserror *error=nil;
_recorder=[[avaudiorecorder Alloc]initwithurl:url settings:setting error:&error];
_recorder.delegate=self;
_recorder.meteringenabled=yes;
if (Error) {
NSLog (@ "Recording error:%@", error.localizeddescription);
}
[_recorder Preparetorecord];
}
return _recorder;
}


Returns the path of the file
-(Nsurl *) FilePath
{
NSString *strpath=[nstemporarydirectory () stringbyappendingpathcomponent:@ "AAA.CAF"];
NSLog (@ "%@-----------", strpath);
Nsurl *url=[nsurl Fileurlwithpath:strpath];
return URL;
}

Pop-up recording page

-(void) addaudioview{
self.navigationItem.rightBarButtonItem.enabled = NO;
_backview = [[UIView alloc]initwithframe:cgrectmake (0, 0, WIDTH, HEIGHT)];
_backview.alpha = 0.5;
_backview.backgroundcolor = [Uicolor blackcolor];
[Self.view Addsubview:_backview];

_audioview = (Audiorecorderalterview *) [[[NSBundle mainbundle]loadnibnamed:@ "Audiorecorderalterview" Owner:nil Options:nil]lastobject];
_audioview.frame = CGRectMake (0, 64, 200, 200);
_audioview.center = Cgpointmake (self.view.center.x, 200);
[_audioview.endbtn addtarget:self Action: @selector (Pulishvedio) forcontrolevents:uicontroleventtouchupinside];
[_audioview.cannelbtn addtarget:self Action: @selector (Canclebtnclick) forControlEvents: UIControlEventTouchUpInside];
[Self.view Addsubview:_audioview];
}

Click the Record button

-(Ibaction) Vediobtnclick: (ID) Sender {
if ([_audioplayer isplaying]) {
[_audioplayer stop];
Uiimageview *animationimageview_last = (Uiimageview *) [Self.view Viewwithtag:_playtag + 10000];
[Animationimageview_last stopanimating];
}

if ([self Canrecord]) {
[Self addaudioview];
if (![ Self.recorder IsRecording]) {
[Self.recorder Record];
}
[Self addtimer];
}
}

iOS recording, audio playback function

Related Article

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.