Avaudioplayer code to play music continuously (access to a single case)

Source: Internet
Author: User

I can't read the message below, I'll explain.



-(void) Audioplayerdecodeerrordidoccur: (Avaudioplayer *) Player ERROR: (NSERROR *) error

{

[Self ordertoplaybackgroundmusic];

}

-(void) audioplayerdidfinishplaying: (Avaudioplayer *) player successfully: (BOOL) flag

{

[Self ordertoplaybackgroundmusic];

}

-(void) Ordertoplaybackgroundmusic

{


[Singletontool defaultmemory].musicindex++;

if ([Singletontool defaultmemory].musicindex = [[Singletontool defaultmemory].musicarray Count])

{

[Singletontool defaultmemory].musicindex = 0;

}

NSString * Musicname = [[Singletontool defaultmemory].musicarray Objectatindex:[singletontool defaultMemory]. Musicindex];

NSString * Musicfilepath = [[NSBundle mainbundle] pathforresource:musicname oftype:@ "MP3"]; Create a music file path

Nsurl * musicurl= [[Nsurl alloc] initfileurlwithpath:musicfilepath];

Avaudioplayer * Theplayer = [[Avaudioplayer alloc] Initwithcontentsofurl:musicurl Error:nil];

Theplayer.delegate = self;

[Theplayer play];

[Theplayer setvolume:1]; Set Volume size

Theplayer.numberofloops = 1;//Set Music playback number-1 to always loop

[Musicurl release];

[Singletontool defaultmemory].theplayer = Theplayer;

[[Singletontool Defaultmemory].playerarray removeallobjects];

[[Singletontool Defaultmemory].playerarray Addobject:theplayer];

}

-(void) Playbackgroundsound

{

[Singletontool Defaultmemory].musicarray = [[Nsmutablearray alloc] initwithcapacity:0];

[[Singletontool defaultmemory].musicarray addobject:@ "Hatsune ミク-world is Mine"];

[[Singletontool defaultmemory].musicarray addobject:@ "Avril Lavigne-girlfrieng"];

[[Singletontool Defaultmemory].musicarray addobject:@ "Bang-qi あゆみ-evolution"];

[[Singletontool defaultmemory].musicarray addobject:@ "Plant vs. Zombie Customs Song"];

[[Singletontool Defaultmemory].musicarray addobject:@ "initial tone ミク-furnace heart melt"];

[[Singletontool defaultmemory].musicarray addobject:@ "Du Yan-Millennium," the theme song of the Chinese Paladin "];

[[Singletontool Defaultmemory].musicarray addobject:@ "first sound ミク-deep-sea maiden"];

int k = Arc4random ()%3;

Switch (k)

{

Case 0:

[Singletontool defaultmemory].musicindex = 0;

Break

Case 1:

[Singletontool defaultmemory].musicindex = 2;

Break

Case 2:

[Singletontool defaultmemory].musicindex = 3;

Break

Default

[Singletontool defaultmemory].musicindex = 3;

Break

}

Avaudiosession *audiosession = [Avaudiosession sharedinstance];

Nserror *err = nil;

[Audiosession Setcategory:avaudiosessioncategoryplayandrecord error:&err];

if (ERR)

{

NSLog (@ "audiosession:%@%d%@", [err domain], [ERR code], [[Err UserInfo] description]);

}

[Audiosession Setactive:yes error:&err];

Err = nil;

if (ERR)

{

NSLog (@ "audiosession:%@%d%@", [err domain], [ERR code], [[Err UserInfo] description]);

}

Disable automatic lock screen when program is running

[[UIApplication sharedapplication] setidletimerdisabled:yes];

NSString * Musicfilepath = [[NSBundle mainbundle] Pathforresource:[[singletontool Defaultmemory].musicarray      Objectatindex:[singletontool Defaultmemory].musicindex] oftype:@ "MP3"]; Create a music file path

Nsurl * musicurl= [[Nsurl alloc] initfileurlwithpath:musicfilepath];

Avaudioplayer * Theplayer = [[Avaudioplayer alloc] Initwithcontentsofurl:musicurl Error:nil];

Theplayer.delegate = self;

[Theplayer play];

[Theplayer setvolume:1]; Set Volume size

Theplayer.numberofloops = 1;//Set Music playback number-1 to always loop

[Musicurl release];

[[Singletontool Defaultmemory].playerarray Addobject:theplayer];

[Singletontool defaultmemory].theplayer = Theplayer;

Half the song out of the background stop after

/*mpmusicplayercontroller *musicplayer = [Mpmusicplayercontroller Ipodmusicplayer];

Mpmusicplaybackstate playbackstate = musicplayer.playbackstate;

if (playbackstate = = Mpmusicplaybackstatestopped | | playbackstate = = mpmusicplaybackstatepaused) {

[Musicplayer play];

else if (playbackstate = = mpmusicplaybackstateplaying) {

[Musicplayer pause];

}*/

}

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.