Play Music on iOS

Source: Internet
Author: User

1. Background music play cycle play long music support MP3 format

#import <AVFoundation/AVFoundation.h>;

1NSString *musicfilepath = [[NSBundle mainbundle] Pathforresource:@"Changan"OfType:@"mp3"];//Create a music file path2Nsurl *musicurl =[[Nsurl alloc] initfileurlwithpath:musicfilepath];3 4Avaudioplayer *theplayer =[[Avaudioplayer alloc] Initwithcontentsofurl:musicurl Error:nil];5 6 //Create player7Self.mybackmusic = Theplayer;//assigning to a class variable of its own definition8 9 [Musicurl release];Ten [Theplayer release]; One  A [Mybackmusic Preparetoplay]; -[Mybackmusic SetVolume:1];//Set Volume size -Mybackmusic.numberofloops =-1;//set the number of music plays-1 to keep looping the[Mybackmusic play];//Play

2. Sound effects play short sound

#import <AudioToolbox/AudioToolbox.h>

NSString *thesoundfilepath = [[NSBundle mainbundle] Pathforresource:@"clapping Crowd Studio"OfType:@"CAF"];//Create a music file pathCfurlref Thesoundurl =(cfurlref) [Nsurl Fileurlwithpath:thesoundfilepath]; Audioservicescreatesystemsoundid (Thesoundurl,&sameviewsoundid);//variable soundid corresponds to URLAudioservicesplaysystemsound (sameviewsoundid);//Play Soundid Sound

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.