AVAudioPlayer code for continuous playing of Music (to the singleton)

Source: Internet
Author: User

Leave a message below. I will explain it.


-(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;
} Www.2cto.com
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]; // sets the volume.
ThePlayer. numberOfLoops = 1; // set the number of times the music is played-1 to keep repeating

[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: @ "ク-World Is Mine"];
[[SingleTonTool defaultMemory]. musicArray addObject: @ "Avril Lavigne-Girlfrieng"];
[[SingleTonTool defaultMemory]. musicArray addObject: @ "あゆみ-evolution"];
[[SingleTonTool defaultMemory]. musicArray addObject: @ "Plants vs. botnet customs clearance song"];
[[SingleTonTool defaultMemory]. musicArray addObject: @ "ク"];
[[SingleTonTool defaultMemory]. musicArray addObject: @ "du Yan-qianyuan" theme song "];
[[SingleTonTool defaultMemory]. musicArray addObject: @ "ク-"];


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 screen lock when the 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]; // sets the volume.
ThePlayer. numberOfLoops = 1; // set the number of times the music is played-1 to keep repeating

[MusicURL release];
[[SingleTonTool defaultMemory]. playerArray addObject: thePlayer];
[SingleTonTool defaultMemory]. thePlayer = thePlayer;




// Stop playing half of the songs after exiting the background
/* 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.