Audio playback for IOS development

Source: Internet
Author: User

The previous period of time to do the project with audio playback function, on the internet also looked up a lot of information, finally understand their principles.

This article is to learn from others, the website is: http://www.jb51.net/article/74666.htm

The reason why the son also write this blog, is for their own later can be convenient to inquire, but also convenient for everyone to check, if the original author thinks that the wrong place, please leave a message contact me, I will delete Bo .... ~_~

A, audio playback we use the Avaudioplayer, Avaudioplayer is within the avfoudation.framework framework, so we need to add the Avfoudation.framework framework to the project when we use it .

b, the audio playback is also divided into 2:

(1), shorter audio playback, general playback time in 1-2 seconds;

(2), relatively long audio playback;

First, write a short audio playback.

1, first add the dependent library avfoudation.framework for the project (remember to import header file #import <AVFoundation/AVFoundation.h>)

2, get the path to the audio

    

1 // Get Audio Path 2     NSString * Musicpath = [[NSBundle mainbundle]pathforresource:@ "aaa" ofType:@ " M4A "];

3, create the URL path for the audio

1     // URL path for creating audio 2     Nsurl * Musicurl = [[Nsurl Alloc]initfileurlwithpath:musicpath];

4. Load the sound file while creating the sound ID

1 // loading sound files, creating a sound ID  one ID corresponds to an audio file 2     Systemsoundid Soundid = 0; 3     Audioservicescreatesystemsoundid ((__bridge cfurlref) Musicurl, &soundid);

5. Play sound files

1 // Playing sound files 2     // // with vibration effect 3           Audioservicesplaysystemsound (Soundid);

Two, here to write a longer time audio playback (This I copied the original author of the)

Long-time playback uses a class called Avaudioplayer, which can be used to play local music files on your phone.

Attention:

(1) This class (Avaudioplayer) can only be used to play local audio.

(2) Short time audio uses Audioservicescreatesystemsoundid to create, while local time longer audio uses the Avaudioplayer class.

Also need to import avfoundation framework, Import header file (#import <AVFoundation/AVFoundation.h>)

  

 1  //  //  yyviewcontroller.m  3   15-play music  4  // 5   #import   " yyviewcontroller.h   " 6   #import  <avfoundation/ Avfoundation.h>7   @interface   Yyviewcontroller ()  8   @end  
1 @implementationYyviewcontroller2- (void) Viewdidload3 {4 [Super Viewdidload];5     6 }7-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event8 {9     Ten     //1. URL path to the audio file OneNsurl *url=[[nsbundle Mainbundle]urlforresource:@"235319.mp3"Withextension:nil]; A      -     //2. Create player (note: One avaudioplayer can only play one URL) -Avaudioplayer *audioplayer=[[Avaudioplayer Alloc]initwithcontentsofurl:url Error:nil]; the      -     //3. Buffering - [Audioplayer Preparetoplay]; -      +     //4. Play - [Audioplayer play]; + } A @end

Code Description: Run the program, click the emulator interface, but not able to play audio files, because the code created in the Avaudioplayer player is a local variable, should be adjusted to the global properties.

You can adjust the code as follows to play audio:

1 #import " YYViewController.h " 2 #import <AVFoundation/AVFoundation.h>3@interface  yyviewcontroller () 4 @property (nonatomic,strong) avaudioplayer *Audioplayer; 5 @end
1 @implementationYyviewcontroller2- (void) Viewdidload3 {4 [Super Viewdidload];5     6 }7-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event8 {9     Ten     //1. URL path to the audio file OneNsurl *url=[[nsbundle Mainbundle]urlforresource:@"235319.mp3"Withextension:nil]; A      -     //2. Create player (note: One avaudioplayer can only play one URL) -Self.audioplayer=[[Avaudioplayer Alloc]initwithcontentsofurl:url Error:nil]; the      -     //3. Buffering - [Self.audioplayer Preparetoplay]; -      +     //4. Play - [Self.audioplayer play]; + } A @end

Note: A avaudioplayer can only play one URL, and if you want to play multiple files, you have to create multiple players.

Audio playback for IOS development

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.