AVAudioPlayer连续播放音乐的代码(通到单例)

来源:未知 责任编辑:智问网络 发表时间:2013-10-30 11:06 点击:

看不懂在下面留言,我会解释


- (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"];      //创建音乐文件路径
 
    NSURL * musicURL= [[NSURL alloc] initFileURLWithPath:musicFilePath];
    AVAudioPlayer * thePlayer  = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
    thePlayer.delegate = self;
    [thePlayer play];
    [thePlayer setVolume:1];   //设置音量大小
    thePlayer.numberOfLoops = 1;//设置音乐播放次数  -1为一直循环
   
    [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:@"植物大战僵尸 通关歌曲"];
    [[SingleTonTool defaultMemory].musicArray addObject:@"初音ミク-炉心融解"];
    [[SingleTonTool defaultMemory].musicArray addObject:@"杜岩-千年缘《仙剑奇侠传四》主题曲"];
    [[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]);
       
    }
   
    //禁止程序运行时自动锁屏
    [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
   
   
    NSString * musicFilePath = [[NSBundle mainBundle] pathForResource:[[SingleTonTool defaultMemory].musicArray objectAtIndex:[SingleTonTool defaultMemory].musicIndex] ofType:@"mp3"];      //创建音乐文件路径
   NSURL * musicURL= [[NSURL alloc] initFileURLWithPath:musicFilePath];
   
    AVAudioPlayer * thePlayer  = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
    thePlayer.delegate = self;
    [thePlayer play];
    [thePlayer setVolume:1];   //设置音量大小
    thePlayer.numberOfLoops = 1;//设置音乐播放次数  -1为一直循环
   
    [musicURL release];
    [[SingleTonTool defaultMemory].playerArray addObject:thePlayer];
    [SingleTonTool defaultMemory].thePlayer = thePlayer;
   
   
   
   
    //播一半的歌曲退出后台后停止
    /*MPMusicPlayerController *musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
     MPMusicPlaybackState playbackState = musicPlayer.playbackState;
     if (playbackState == MPMusicPlaybackStateStopped || playbackState == MPMusicPlaybackStatePaused) {
     [musicPlayer play];
     } else if (playbackState == MPMusicPlaybackStatePlaying) {
     [musicPlayer pause];
     }*/
   
}

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • cocos2d-x学习笔记(19)--label 、label atlas
      • cocos2d-x学习笔记(23)--地图的使用3--CCTMXLayer
      • Cocos2d-x学习(一):HelloWorld
      • cocos2dx在xcode下开发,编译到android上(2)
      • cocos2d 设置屏幕默认方向
      • Cocos2d-x 2.0 之 Actions “三板斧” 之一
      • cocos2d-x学习笔记(22)--地图的使用2(TMX) --Z-Order、AnchorPoi
      • cocos2d-x学习笔记(18)--游戏打包(windows平台)
      • cocos2d-x学习笔记(16)--spritesheet(精灵表单)
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1