iOS Development outreach-Music playback

Source: Internet
Author: User

A simple explanation

Music 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) shorter time (called sound effects) is created using Audioservicescreatesystemsoundid, while local time is longer (called music) using the Avaudioplayer class.

Second, code example

The Avaudioplayer class relies on the Avfoundation framework, so you must first import the Avfoundation framework and include its header file (including the primary header file) before using the class.

Description: After iOS8, you do not have to import the Avfoundation framework

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

code example:

1 //2 //VIEWCONTROLLER.M3 //02-Music playback4 //5 //Created by Apple on 14/11/7.6 //Copyright (c) 2014 GitHub. All rights reserved.7 //8 9 #import "ViewController.h"Ten #import<AVFoundation/AVFoundation.h> One  A @interfaceViewcontroller () -  - @end the  - @implementationViewcontroller -  -- (void) Viewdidload + { - [Super Viewdidload]; +      A } at  --(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event - { -      -     //1. URL path to the audio file -Nsurl *url = [[NSBundle mainbundle]urlforresource:@"simple love. mp3"Withextension:nil]; in      -     //2. Create player (note: One avaudioplayer can only play one URL) toAvaudioplayer *player =[[Avaudioplayer alloc] Initwithcontentsofurl:url Error:nil]; +      -     //3. Buffering the [player Preparetoplay]; *      $     //4. PlayPanax Notoginseng [player play]; - } the  + @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 "ViewController.h"2 #import<AVFoundation/AVFoundation.h>3 4 @interfaceViewcontroller ()5 //player6@property (nonatomic,strong) Avaudioplayer *player;7 @end8 9 @implementationYyviewcontrollerTen  One- (void) Viewdidload A { - [Super Viewdidload]; -      the } -  --(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event - { +      -     //1. URL path to the audio file +Nsurl *url = [[NSBundle mainbundle] Urlforresource:@"simple love. mp3"Withextension:ni]; A      at     //2. Create player (note: One avaudioplayer can only play one URL) -Self.audioplayer =[[Avaudioplayer alloc] Initwithcontentsofurl:url Error:nil]; -      -     // 3. Buffering - [Self.player Preparetoplay]; -      in     //4. Play - [Self.player play]; to } +  - @end

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

Iii. Related Notes

Create a new project and place four buttons in storyboard to control the playback, pause, stop, and next song of the music, respectively.

  

The program code is as follows:

Note: If you click "Stop", then be sure to create a new player, otherwise there will be an inexplicable problem.

After you click Stop, the player will not actually be able to continue to use it, and if you continue to use it, then some of the things that follow are out of control.

Recommended Code:

1 #import "ViewController.h"2 #import<AVFoundation/AVFoundation.h>3 #import "HMAudioTool.h"4 5 @interfaceViewcontroller ()6 //player7@property (nonatomic, strong) Avaudioplayer *player;8 9 //PlayTen-(ibaction) playmusic; One  A //Pause --(ibaction) pausemusic; -  the //Stop --(ibaction) stopmusic; -  - //Next Song +-(ibaction) nextmusic; - @end +  A @implementationYyviewcontroller at  - #pragmamark-Lazy Loading --(Avaudioplayer *) Player - { -     if(!_player) -     { in         //0. Loading music files -Nsurl *url = [[NSBundle mainbundle] Urlforresource:@"simple love. mp3"Withextension:nil]; to         //1. Create a music player +Self.player =[[Avaudioplayer alloc] Initwithcontentsofurl:url Error:nil]; -         //2. Buffering music files (prepare) the [Self.player Preparetoplay]; *     } $     return_player;Panax Notoginseng } -  the- (void) Viewdidload + { A [Super Viewdidload]; the } +  --(ibaction) playmusic { $     //The URL of the player is readonly, so it cannot be set, that is, a player corresponds to a song $     //Self.player.url = nil; -     //Start play/Resume playback - [Self.player play]; the } - Wuyi-(ibaction) pausemusic { the     //Pause - [Self.player pause]; Wu } -  About-(ibaction) stopmusic { $     //Stop - [Self.player stop]; - #warningNote: If the player is destroyed, the player will not be able to use it, because if the player is destroyed it is not a controller, it is recommended to empty the player after each call to the player's Destroy method -     Self.player = nil; A  + } the  --(ibaction) nextmusic { $  the } the  the @end

If you click the Stop button, the concert starts playing from the beginning.

Four, play multiple files

Self.player = [[Avaudioplayer alloc] Initwithcontentsofurl:url Error:nil];

Self.player.url;

Click, URL, and press common to build a view.

It can be found that this URL is read-only and therefore can only be set by Initwithcontentsofurl, which means that only one audio file can be played by a player object.

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 do it.

iOS Development outreach-Music playback

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.