[IOS:OC] Simple audio playback

Source: Internet
Author: User

Here's an example of a local resource.

Play long music:

#import <AVFoundation/AVFoundation.h>    //url: Network resource path, you can also convert local resources into network resource path    //Get a resource locally, and convert to URL    nsurl * URL = [[NSBundle mainbundle]urlforresource:@ "background music" withextension:@ "CAF"];        Avaudioplayer *player = [[Avaudioplayer alloc]initwithcontentsofurl:url Error:nil];    _player = player;  You need to define a property to point to him, otherwise the method ends up being destroyed, the music stops    //Specifies the number of cycles to play, 0 for once    //or any negative numbers for infinite playback    [player setnumberofloops:-1 ];        Ready to play    [player Preparetoplay];    Play, launch a thread to play    [player play];

Play a short tone, such as responding to a sound when the button is pressed:

    nsurl* URL = [[NSBundle mainbundle]urlforresource:@] sound. * "Withextension:nil";    Create a soundid    systemsoundid soundid;    Audioservicescreatesystemsoundid (__bridge cfurlref) (URL), &soundid);    Play     Audioservicesplaysystemsound (Soundid);

[IOS:OC] Simple 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.