There are three ways to play audio in iOS: Avaudioplayer, audio services, audio queues. __ios

Source: Internet
Author: User
Tags file url

This article is mainly about Avaudioplayer, the other two see related articles.

Avaudioplayer in the avfoundation frame, so we're going to import avfoundation.framework.

The Avaudioplayer class encapsulates the ability to play a single sound. The player can initialize with Nsurl or nsdata, note that Nsurl can not be a network URL and must be a local file URL, because the Avaudioplayer does not have the ability to play network audio, but we can use a little way to enable it to have this ability, To be explained later.

A avaudioplayer can play only one audio, and if you want to mix you can create multiple Avaudioplayer instances, each equivalent to one track on the mixer board.

First, create a player

Source printing. #import  <AVFoundation/AVFoundation.h>      nserror* err;   avaudioplayer* player = [[avaudioplayer alloc]                             initWithContentsOfURL:[NSURL fileURLWithPath:                                                    [[NSBundle mainBundle]pathForResource:                                                 @ "MusIC " oftype:@" M4A                                                  indirectory:@ "/"]]                             error:&err ];//use local URL to create    source print. avaudioplayer* player = [[avaudioplayer alloc]                                 initWithData:myData                                  error:&err ];/ /use NSData to create   

I told you before, Avaudioplayer can't play the Web URL, but it can play nsdata, we seem to be inspired, we can create the NSData through the Web URL, and then play NSData through the Avaudioplayer, So is it possible to play network music? But this method is not advisable, because Avaudioplayer can only play a complete file, does not support streaming playback, so must be buffered to play, so if the network file is too large or not enough speed is not to wait for a long time. So play network audio we generally use audio queues.

Second, player properties

After you create a avaudioplayer, you can access or set up a variety of its properties.

1. Volume source printing. Between player.volume=0.8;//0.0~1.0 2. Cycle times source code printing. Player.numberofloops = 3;//By default only once 3. Play location source print. Player.currenttime = 15.0;//can specify that 4. Channel number source printing from anywhere. Nsuinteger channels = player.numberofchannels;//Read-only property 5. Duration of the source code printing. Nstimeinterval Duration = player.dueration;//Gets the duration of the sample

6. Meter Count

Source printing.   player.meteringenabled = yes;//Turn on meter count function [player updatemeters];//update meter reading//read the average electric and peak level of each channel, representing the decibel number of each channel, range between -100~0.   for (int i = 0; i<player.numberofchannels;i++) {Float power = [player averagepowerforchannel:i];   float peak = [player peakpowerforchannel:i]; }

Third, play sound

Prepare for so long, finally can play, mood excited ah. Source printing. [Player Preparetoplay];

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.