iOS Development-Music playback

Source: Internet
Author: User

Now the various kinds of apps, in fact, is commonly used around the basic needs of beer and skittles, video, music in the advent of smartphones is an essential function, each cell phone will have a music player, of course, the public has its own needs so it has created a variety of music playback software, I have nothing to do in the afternoon. Simply write a random play music in the Demo,ios there are three ways to play audio Avaudioplayer, audio services, audio queues. The other two are not used temporarily, the simple practiced hand a bit avaudioplayer, or start the subject bar;

1. Create a new project or re-start a page in the original project, look at the page:

2. Import some of your favorite songs:

3. Import Avfoundation/avfoundation.h, four buttons for event operation, one Avaudioplayer can only correspond to one URL, so when playing other songs you need a situation;

Define two member variables and initialize the member variables:

@interface Musicviewcontroller () @property (Nonatomic,strong) Avaudioplayer *player; @property (Nonatomic,strong) Nsarray *musicarr; @end

Viewdidload instantiating an array:

-(void) viewdidload {    [super viewdidload];    Do any additional setup after loading the view.    [Email protected] [@ "Moist heart. mp3", @ "love to fight to win." MP3 ", @" give me a Reason to forget. mp3 "];    [Self preparemusic:self.musicarr[1]];} -(void) Preparemusic: (NSString *) path{    //1. The URL path of the audio file Nsurl *url=[[nsbundle mainbundle]urlforresource:path    Withextension:nil];        2. Instantiate the player    _player=[[avaudioplayer Alloc]initwithcontentsofurl:url Error:nil];        3. Buffering    [_player Preparetoplay];}

4. Four code for the corresponding event:

Random:

-(Ibaction) random: (ID) Sender {    [self preparemusic:self.musicarr[arc4random ()%3]];    [_player play];}

Play:

-(Ibaction) play: (ID) Sender {    //play    [_player play];}

Time out:

-(Ibaction) pause: (ID) Sender {    //pause    [_player pause];}

Stop it:

-(Ibaction) STOP: (ID) Sender {    //stop    [_player stop];}

5. Set the number of cycles, start playing time, set the volume

  Set volume    [_player setvolume:0.6];    Set current playback event    [_player setcurrenttime:60];    Set the number of cycles    [_player setnumberofloops:2];

Code in MUSICVIEWCONTROLLER.M:

musicviewcontroller.m//mypicture////Created by Keso on 15/1/17.//Copyright (c) 2015 Keso. All rights reserved.//#import "MusicViewController.h" #import <AVFoundation/AVFoundation.h> @interface Musicviewcontroller () @property (Nonatomic,strong) Avaudioplayer *player; @property (nonatomic,strong) Nsarray *    Musicarr; @end @implementation musicviewcontroller-(void) viewdidload {[Super viewdidload];    Do any additional setup after loading the view. [Email protected]    [@ "Moist heart. mp3", @ "love to fight to win." MP3 ", @" give me a Reason to forget. mp3 "]; [Self preparemusic:self.musicarr[1]];} -(void) Preparemusic: (NSString *) path{//1. The URL path of the audio file Nsurl *url=[[nsbundle mainbundle]urlforresource:path WithExten        Sion:nil];        2. Instantiate the player _player=[[avaudioplayer Alloc]initwithcontentsofurl:url Error:nil];    3. Cushioning [_player Preparetoplay];    Set volume [_player setvolume:0.6];    Set current playback event [_player setcurrenttime:60]; Set the number of cycles [_player setnumberofloops:2];} -(Ibaction) random: (ID) sender{[Self preparemusic:self.musicarr[arc4random ()%3]]; [_player play];} -(Ibaction) play: (ID) Sender {//play [_player play];} -(Ibaction) STOP: (ID) Sender {//stop [_player stop];} -(Ibaction) pause: (ID) Sender {//pause [_player pause];}    -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} /* #pragma mark-navigation//in a storyboard-based application, you'll often want to do a little preparation before Nav igation-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {//Get the new view controller using [SEG    UE Destinationviewcontroller]. Pass the selected object to the new view Controller.} */@end

Actually need to set up a lot, play an exception, or be interrupted by a higher level of the system task, you can set the corresponding delegate processing the corresponding situation, the demo is very small, ios a lot of things are like this, the concept of a lot of, when the call doesn't need to write a few lines of code, The iOS simulator plays a very good effect.

Because it is playing music, can not simulate the effect, probably experiment, there should be no problem ~

iOS Development-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.