iOS音频播放

·iOS系统中的音频播放方式·AVAudioPlayer、AVPlayer、系统声音、音频队列

·AVAudioPlayer·使用简单方便,但只能播放本地音频,不支持流媒体播放

·AVPlayer·iOS4.0以后,可以使用AVPlayer播放本地音频和支持流媒体播放,但提供接口较少,处理音频不够灵活·音频队列

·音频队列主要处理流媒体播放,提供了强大且灵活的API接口(C函数的接口),但处理起来也较为复杂

·播放系统声音·音频数据可分为压缩和非压缩的存储类型。压缩的音频文件虽然文件体积较小(相对于非压缩的),但需要耗费处理器的性能进行解压和解码。

·如果音频文件体积较小,压缩后的音频文件,也不会节省较大的磁盘空间。像这一类小型非压缩的文件可以注册成为系统声音·格式为:caf/wav/aiff格式,且时长小于30s



- (void)viewDidLoad {

    [super viewDidLoad];


    //1.plist文件Required background modes

    //App plays audio or streams audio/video using AirPlay

    //2.设置后台播放

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

    //1取得本地音频文件路径

    NSString*filePath =  [[NSBundle mainBundle] pathForResource:@"安静" ofType:@"mp3"];


    NSURL*url = [NSURLfileURLWithPath:filePath];

    //创建全局播放器  AVAudioPlayer 只能播放本地音频文件

    _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];

    //2播放流媒体音乐,AVPlayer都支持

    //获得网络链接

    NSString *str = @"http://ra01.sycdn.kuwo.cn/resource/n3/32/56/3260586875.mp3";


    NSURL*url1 = [NSURLURLWithString:str];

    _player= [[AVPlayeralloc]initWithURL:url1];


}

//1播放本地音乐

- (IBAction)playLocalMusic:(id)sender {


    if(_audioPlayer.isPlaying){

        [_audioPlayerpause];

    }else{

        [_audioPlayerplay];

    }

}

//2播放流媒体音乐

- (IBAction)playMusic:(id)sender {


    if(_isPlay) {

        [_playerpause];

    }else{

        [_playerplay];

    }

    _isPlay = !_isPlay;

}

//3播放系统声音

- (IBAction)playSystemSound:(id)sender {


    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"44th Street Medium.caf" ofType:nil];

    NSURL*url = [NSURLfileURLWithPath:filePath];

    UInt32  systemSoundID =10;

    //注册系统声音

    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &systemSoundID);

    //根据id播放系统声音

    AudioServicesPlaySystemSound(systemSoundID);

    //设置手机振动

    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

}


//设置锁屏时界面显示的播放信息

-(void)_initUI{

    /* MPMediaItemPropertyAlbumTitle

     // MPMediaItemPropertyAlbumTrackCount

     // MPMediaItemPropertyAlbumTrackNumber

     // MPMediaItemPropertyArtist

     // MPMediaItemPropertyArtwork

     // MPMediaItemPropertyComposer

     // MPMediaItemPropertyDiscCount

     // MPMediaItemPropertyDiscNumber

     // MPMediaItemPropertyGenre

     // MPMediaItemPropertyPersistentID

     // MPMediaItemPropertyPlaybackDuration

     // MPMediaItemPropertyTitle*/


    MPMediaItemArtwork *artWork = [[MPMediaItemArtwork alloc ] initWithImage:[UIImage imageNamed:@"mp3.jpg"]];

   NSDictionary *dic = @{

                          MPMediaItemPropertyAlbumTitle:@"兄弟你变了",

                          MPMediaItemPropertyArtist:@"庞龙",

                          MPMediaItemPropertyArtwork:  artWork

                          };


    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dic];


}

-(BOOL )canBecomeFirstResponder{


    return YES;

}

-(void)viewWillAppear:(BOOL)animated{


    [superviewWillAppear:animated];

    [self becomeFirstResponder];

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    [self_initUI];


}

-(void)viewWillDisappear:(BOOL)animated{


    [superviewWillDisappear:animated];

    [self resignFirstResponder];

    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];


}

//设置远程监控

-(void)remoteControlReceivedWithEvent:(UIEvent *)event{


    if (event.type == UIEventTypeRemoteControl) {


        switch(event.subtype) {

            case UIEventSubtypeRemoteControlPlay:


                [_audioPlayerplay];


                NSLog(@"播放");

                break;

            case UIEventSubtypeRemoteControlPause:

                [_audioPlayerpause];


                NSLog(@"暂停");

                break;

            case UIEventSubtypeRemoteControlNextTrack:

                NSLog(@"下一首");

                break;

            case UIEventSubtypeRemoteControlPreviousTrack:

                NSLog(@"上一首");

                break;


            default:

                break;

        }

    }

}

你可能感兴趣的:(iOS音频播放)