iOS 實現後台 播放音樂聲音 AVAudioPlayer

來源:互聯網
上載者:User

標籤:1.0   path   one   並且   ret   keyword   res   uicolor   ica   

1、步驟一:在Info.plist中,添加"Required background modes"鍵,value為:App plays audio
 或者:

 

步驟二:
 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    NSError *setCategoryErr = nil;
    NSError *activationErr  = nil;
    [[AVAudioSession sharedInstance]
     setCategory: AVAudioSessionCategoryPlayback
     error: &setCategoryErr];
    [[AVAudioSession sharedInstance]
     setActive: YES
     error: &activationErr];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

步驟三:將以下代碼添加到appDelegate檔案中的- (void)applicationDidEnterBackground:(UIApplication *)application函數,也可添加到在具體類中註冊的應用進入後台後的通知方法
 

- (void)applicationDidEnterBackground:(UIApplication *)application{

    UIApplication*   app = [UIApplication sharedApplication];
    __block    UIBackgroundTaskIdentifier bgTask;
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            if (bgTask != UIBackgroundTaskInvalid)
            {
                bgTask = UIBackgroundTaskInvalid;
            }
        });
    }];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            if (bgTask != UIBackgroundTaskInvalid)
            {
                bgTask = UIBackgroundTaskInvalid;
            }
        });
    });
}

 

 

接下來就是在播放頁面進行播放了:

先加入代理:AVAudioPlayerDelegate

    //從budle路徑下讀取音頻檔案   這個檔案名稱是你的歌曲名字,mp3是你的音頻格式     NSString *string = [[NSBundle mainBundle] pathForResource:@"has_consult" ofType:@"mp3"];     //把音頻檔案轉換成url格式     NSURL *url = [NSURL fileURLWithPath:string];     //初始化音頻類 並且添加播放檔案     NSError *error;     avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];         //設定初始音量大小     // avAudioPlayer.volume = 1;     //設定音樂播放次數  -1為一直迴圈     avAudioPlayer.numberOfLoops = -1;     //預播放     [avAudioPlayer prepareToPlay];    NSLog(@"%ld",(long)error.code);//播放-(void)play{    [avAudioPlayer play];    }//停止-(void)stop{    avAudioPlayer.currentTime = 0;  //當前播放時間設定為0    [avAudioPlayer stop];  }

 

 

 

 

 

 

iOS 實現後台 播放音樂聲音 AVAudioPlayer

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.