iOS 后台播放音乐

//后台播放音乐
-(void)playAudio{
    dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_async(dispatchQueue, ^(void) {
        NSError *audioSessionError = nil;
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        if ([audioSession setCategory:AVAudioSessionCategoryPlayback error:&audioSessionError]){
            NSLog(@"Successfully set the audio session.");
        } else {
            NSLog(@"Could not set the audio session");
        }

        NSBundle *mainBundle = [NSBundle mainBundle];
        NSString *filePath = [mainBundle pathForResource:@"mySong" ofType:@"mp3"];
        NSData *fileData = [NSData dataWithContentsOfFile:filePath];
        NSError *error = nil;
        self.audioPlayer = [[AVAudioPlayer alloc] initWithData:fileData error:&error];

        if (self.audioPlayer != nil){
            self.audioPlayer.delegate = self;
            [self.audioPlayer setNumberOfLoops:-1];
            if ([self.audioPlayer prepareToPlay] && [self.audioPlayer play]){
                NSLog(@"Successfully started playing...");
            } else {
                NSLog(@"Failed to play.");
            }
        }

    });

}

调用方法一:
    SystemSoundID myAlertSound;
    NSURL *url = [NSURL URLWithString:@"/System/Library/Audio/UISounds/begin_video_record.caf"];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &myAlertSound);
    AudioServicesPlaySystemSound(myAlertSound);
调用方法二:
    NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:@"Tock" ofType:@"aiff"];
    if (path) {
        SystemSoundID theSoundID;
        OSStatus error =  AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &theSoundID);
        if (error == kAudioServicesNoError) {
            AudioServicesPlaySystemSound(theSoundID);
        }
        else
        {
            NSLog(@"Failed to create sound ");
        }
    }

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-06 20:19:22

iOS 后台播放音乐的相关文章

IOS后台播放音乐

IOS后台播放音乐 博客分类: IOS http://www.apple.com.cn/developer/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AudioandVideoTechnologies/AudioandVideoTechnologies.html#//apple_ref/doc/uid/TP40007072-CH19-SW32 1.首先在工程中导入播放音乐所使用的框架:AV Fo

ios 后台播放音乐1条注意事项

除了设置程序的后台模式,还需要几行代码 AVAudioSession *session = [AVAudioSession sharedInstance]; [session setCategory:AVAudioSessionCategoryPlayback error:nil]; [session setActive:YES error:nil]; 不加这几行代码是无法在真机上后台播放的!(模拟器好像可以不加) 在附一张background模式的设置图

使用 AVAudioSession 实现后台播放音乐

1. 前言 AVAudioSession是一个单例,无需实例化即可直接使用.AVAudioSession在各种音频环境中起着非常重要的作用 针对不同的音频应用场景,需要设置不同的音频会话分类 1.1 ?AVAudioSession的类别 AVAudioSessionCategoryAmbient –混音播放,例如雨声.汽车引擎等,可与其他音乐一起播放 AVAudioSessionCategorySoloAmbient –后台播放,其他音乐将被停止 AVAudioSessionCategoryPl

IOS后台运行 之 后台播放音乐

iOS 4开始引入的multitask,我们可以实现像ipod程序那样在后台播放音频了.如果音频操作是用苹果官方的AVFoundation.framework实现,像用AvAudioPlayer,AvPlayer播放的话,要实现完美的后台音频播放,依据app的功能需要,可能需要实现几个关键的功能. 首先,播放音频之前先要设置AVAudioSession模式,通常只用来播放的App可以设为AVAudioSessionCategoryPlayback即可.模式意义及其他模式请参考文档. //后台播放

后台播放音乐

iOS 4开始引入的multitask,我们可以实现像ipod程序那样在后台播放音频了.如果音频操作是用苹果官方的AVFoundation.framework实现,像用AvAudioPlayer,AvPlayer播放的话,要实现完美的后台音频播放,依据app的功能需要,可能需要实现几个关键的功能. 首先,播放音频之前先要设置AVAudioSession模式,通常只用来播放的App可以设为AVAudioSessionCategoryPlayback即可.模式意义及其他模式请参考文档. //后台播放

iOS后台播放

### 音乐后台播放 * 1.当程序进入后台的时候,开启后台任务 ``` - (void)applicationDidEnterBackground:(UIApplication *) { // 开启后台任务 [application beginBackgroundTaskWithExpirationHandler:nil]; } ``` * 2.在项目的Targets页面,设置Capabilities,BackgroundModes选择第一项,`Audio, AirPlay and Pictu

iOS 后台播放音频文件

首先,在info.plist里申明需要在后台播放音频内容: 添加key   UIBackgroundModes Tips:UIBackgroundModes audio键 同时还能让app使用AirPlay播放流媒体 除此之外还需要在工程中设置对AVAudioSession 进行设置: AVAudioSession *audioSession = [AVAudioSession sharedInstance]; NSError *setCategoryError = nil; BOOL succ

手机影音第十五天,利用service实现后台播放音乐,在通知栏显示当前音乐信息等

代码已经托管到码云上,有兴趣的小伙伴可以下载看看 https://git.oschina.net/joy_yuan/MobilePlayer 先来一张目前的音乐播放器的效果图,当播放时,手机的状态通知栏也会有音乐信息显示. 这里可以看到有歌名.演唱者,还有歌曲的总时间,当前播放时间,当前播放进度,音乐暂停.下一首,上一首,音乐循环模式(单曲循环,顺序播放.循环播放)功能的实现.下一步就是把中间空白的部分填充歌词,然后做成根据进度显示歌词. 由于这次的内容有点多,是写了一天半的代码,讲的没那么细,

iOS 实现后台 播放音乐声音 AVAudioPlayer

1.步骤一:在Info.plist中,添加"Required background modes"键,value为:App plays audio 或者: 步骤二: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[[UIWindow alloc] initWithFrame:[[U