Explain the simple implementation of audio and music playback in IOS development _ios

Source: Internet
Author: User
Tags local time

Playback of sound effects

First, a brief introduction

In simple terms, audio can be divided into 2 kinds

(1) Sound effects

Also known as "short audio", usually plays in the program 1-2 seconds long

Play a decorative effect in your application to enhance the overall user experience

(2) Music

such as "background music" in the game, the general playback time is longer

Frame: Playing audio requires a avfoundation.framework frame

Second, the sound effect play

1. Obtain the path to the sound file

Copy Code code as follows:

Nsurl *url = [[NSBundle mainbundle] urlforresource:@ "M_03.wav" withextension:nil];

2. Load the sound effect file, get the corresponding sound effect ID

Copy Code code as follows:

Systemsoundid soundid = 0;

Audioservicescreatesystemsoundid ((__bridge cfurlref) (URL), &soundid);

3. Play sound

Copy Code code as follows:

Audioservicesplaysystemsound (Soundid);

Note: The sound file only needs to be loaded 1 times

4. Sound Playback Common Function Summary

Load sound files

Copy Code code as follows:

Audioservicescreatesystemsoundid (Cfurlref infileurl, Systemsoundid *outsystemsoundid)

Releasing sound resources
Copy Code code as follows:

Audioservicesdisposesystemsoundid (Systemsoundid Insystemsoundid)

Play sound
Copy Code code as follows:

Audioservicesplaysystemsound (Systemsoundid Insystemsoundid)

Play sound with a bit of vibration
Copy Code code as follows:

Audioservicesplayalertsound (Systemsoundid Insystemsoundid)

Third, the program example

First import the framework that you need to rely on

Import audio file footage that needs to be played

Description: The Avfoundation.framework frame in the conversion of things to CF requires the use of bridging.

code example:

Copy Code code as follows:

YYVIEWCONTROLLER.M file
//
Yyviewcontroller.m
14-Sound Playback
//
Created by Apple on 14-8-8.
Copyright (c) 2014 Yangyong. All rights reserved.
//

#import "YYViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface Yyviewcontroller ()

@end


Copy Code code as follows:

@implementation Yyviewcontroller

-(void) viewdidload
{
[Super Viewdidload];
}

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
1. Obtain the full path of the sound effect file

Nsurl *url=[[nsbundle mainbundle]urlforresource:@ "Buyao.wav" withextension:nil];

2. Load the sound effect file, create the sound effect ID (soundid, an ID corresponding to an audio file)
Systemsoundid soundid=0;
Audioservicescreatesystemsoundid ((__bridge cfurlref) URL, &soundid);

Pass the ID of the sound file that needs to be destroyed to it can be destroyed
Audioservicesdisposesystemsoundid (Soundid);

3. Play sound files
The following two functions can be used to play sound files, the first function with a vibration effect
Audioservicesplayalertsound (Soundid);
Audioservicesplaysystemsound (< #SystemSoundID insystemsoundid#>)
}

@end


Description: Click on the screen to play sound effects files.

The music plays

One, simple explanation

Music playback uses a class called Avaudioplayer, which can be used to play music files that are local to the phone.

Attention:

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

(2) shorter time (called sound effects) is created using Audioservicescreatesystemsoundid, while the local time is longer (called music) using the Avaudioplayer class.

Second, the code example

The Avaudioplayer class relies on the Avfoundation framework, so you must import the Avfoundation framework and include its header file (which includes the primary header file).

Import the necessary audio files that need to be played into the project.

code example:

Copy Code code as follows:

//
Yyviewcontroller.m
15-Play Music
//

#import "YYViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface Yyviewcontroller ()

@end


Copy Code code as follows:

@implementation Yyviewcontroller

-(void) viewdidload
{
[Super Viewdidload];

}

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{

1. The URL path of the audio file
Nsurl *url=[[nsbundle mainbundle]urlforresource:@ "235319.mp3" withextension:nil];

2. Create the player (note: A avaudioplayer can play only one URL)
Avaudioplayer *audioplayer=[[avaudioplayer Alloc]initwithcontentsofurl:url Error:nil];

3. Buffer
[Audioplayer Preparetoplay];

4. Play
[Audioplayer play];
}

@end


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

You can adjust the code as follows to play the audio:

Copy Code code as follows:

#import "YYViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface Yyviewcontroller ()
@property (Nonatomic,strong) Avaudioplayer *audioplayer;
@end


Copy Code code as follows:

@implementation Yyviewcontroller

-(void) viewdidload
{
[Super Viewdidload];

}

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{

1. The URL path of the audio file
Nsurl *url=[[nsbundle mainbundle]urlforresource:@ "235319.mp3" withextension:nil];

2. Create the player (note: A avaudioplayer can play only one URL)
Self.audioplayer=[[avaudioplayer Alloc]initwithcontentsofurl:url Error:nil];

3. Buffer
[Self.audioplayer Preparetoplay];

4. Play
[Self.audioplayer play];
}

@end


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

Iii. Related Notes

Create a new item and put three buttons in the storyboard to control playback, pause, and stop music.

The program code is as follows:

Copy Code code as follows:

#import "YYViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface Yyviewcontroller ()
@property (Nonatomic,strong) Avaudioplayer *player;
-(ibaction) play;
-(ibaction) pause;
-(ibaction) stop;
@end

@implementation Yyviewcontroller

-(void) viewdidload
{
[Super Viewdidload];

1. The URL path of the audio file
Nsurl *url=[[nsbundle mainbundle]urlforresource:@ "235319.mp3" withextension:nil];

2. Create the player (note: A avaudioplayer can play only one URL)
Self.player=[[avaudioplayer Alloc]initwithcontentsofurl:url Error:nil];

3. Buffer
[Self.player Preparetoplay];

}

-(ibaction) play {
Start play/Resume Playback
[Self.player play];
}

-(ibaction) Pause {
Time out
[Self.player pause];
}

-(ibaction) Stop {
Stop it
Note: If you click on the stop, then be sure to reset the new creation, otherwise there will be some inexplicable aspects of the problem
[Self.player stop];
}
@end


Note: If you click Stop, be sure to replay the new creation, otherwise there will be a puzzling problem.

After you click on the stop, the player can no longer use it, and if you continue to use it, some of the subsequent things will be out of control.

Recommended Code:

Copy Code code as follows:

#import "YYViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface Yyviewcontroller ()
@property (Nonatomic,strong) Avaudioplayer *player;
-(ibaction) play;
-(ibaction) pause;
-(ibaction) stop;
@end


Copy Code code as follows:

@implementation Yyviewcontroller

#pragma mark-lazy Load
-(Avaudioplayer *) player
{
if (_player==nil) {

1. The URL path of the audio file
Nsurl *url=[[nsbundle mainbundle]urlforresource:@ "235319.mp3" withextension:nil];

2. Create the player (note: A avaudioplayer can play only one URL)
Self.player=[[avaudioplayer Alloc]initwithcontentsofurl:url Error:nil];

3. Buffer
[Self.player Preparetoplay];
}
return _player;
}

-(void) viewdidload
{
[Super Viewdidload];
}

-(ibaction) play {
Start play/Resume Playback
[Self.player play];
}

-(ibaction) Pause {
Time out
[Self.player pause];
}

-(ibaction) Stop {
Stop it
Note: If you click on the stop, then be sure to reset the new creation, otherwise there will be some inexplicable aspects of the problem
[Self.player stop];
Self.player=nil;
}
@end


Four, play multiple files

Click, URL, and hold common to build view.

As you can see, this URL is read-only, so it can only be set by Initwithcontentsofurl, which means that a player object can play only one audio file.

So how do you play multiple audio files?

Consider encapsulating a tool class that plays music, and the next article will show you how to implement it.

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.