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];
}*/
}