IOS 声音播放(利用AVAudioPlayer)

需要player设为全局变量
一、播放声音
AVAudioPlayer *player = [[AVAudioPlayeralloc] initWithContentsOfURL:[NSURLfileURLWithPath:[[NSBundlemainBundle] pathForResource:@"星月神话" ofType:@"mp3"]] error:nil];//使用本地URL创建

二、播放器属性
创建一个AVAudioPlayer以后你就可以对它的各种属性进行访问或设置了。
1、音量
 player.volume =0.8;//0.0-1.0之间
2、循环次数
player.numberOfLoops =3;//默认只播放一次
3、播放位置 
player.currentTime =15.0;//可以指定从任意位置开始播放
4、声道数
NSUInteger channels = player.numberOfChannels;//只读属性
5、持续时间
NSTimeInterval duration = player.duration;//获取持续时间
6、仪表计数
player.meteringEnabled =YES;//开启仪表计数功能
[playerupdateMeters];//更新仪表计数

三、播放声音
[player prepareToPlay];//分配播放所需的资源,并将其加入内部播放队列

[playerplay];//播放

[playerstop];//停止

四、代理方法

这个类对应的AVAudioPlayerDelegater的委托方法。 audioPlayerDidFinishPlaying:successfully: 当音频播放完成之后触发。当播放完成之后,可以将播放按钮的文本重新回设置成:Play Audio File

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    //播放结束时执行的动作

    [audioButton setTitle:@"Play Audio File" forState:UIControlStateNormal];

}

- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error;

{
   //解码错误执行的动作

}

- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player;

{
   //处理中断的代码

}

audioPlayerEndInterruption:,当程序被应用外部打断之后,重新回到应用程序的时候触发。在这里当回到此应用程序的时候,继续播放音乐。

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player

{

    [audioPlayer play];

}
发布了69 篇原创文章 · 获赞 47 · 访问量 14万+
展开阅读全文

AVAudioPlayer无法播放音频的问题

04-23

今天开始做录音的功能。点击recordbtn开始录音,再次点击,则停止录音,点击play键播放录音 然后就遇到问题了。 点击play键后,[self.audioPlayer prepareToPlay] 总是为no,即使把这个去掉,强制[self.audioPlayer play];也没有效果,请问是怎么回事? 另:用player播放其他音频是可以的,作为对比测试,播放了一首歌曲,没问题,就是播放自己录制的音频出问题,是不是录制的时候就出错了? 附代码: ``` -(void)record{ NSLog(@"recording"); AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryRecord error:nil]; [audioSession setActive:YES error:nil]; NSString *tripReportAudioPath = [self audioRecordingPath]; NSError *err = nil; NSURL *tripReportAudioURL = [NSURL URLWithString:tripReportAudioPath]; AVAudioRecorder *tripReportRecorder = [[AVAudioRecorder alloc]initWithURL:tripReportAudioURL settings:[self audioRecordingSetting] error:&err]; if (tripReportRecorder != nil) { tripReportRecorder.delegate = self; if ([tripReportRecorder prepareToRecord] && self.recordBtn.selected == NO) { self.recordBtn.selected = YES; [tripReportRecorder record]; }else if (self.recordBtn.selected == YES){ self.recordBtn.selected = NO; [tripReportRecorder stop ]; } }else{ NSLog(@"record err:%@",[err description]); } } //生成录音文件地址 -(NSString *)audioRecordingPath{ NSString *result = nil; NSArray *folders = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docdir = [folders objectAtIndex:0]; result = [docdir stringByAppendingPathComponent:@"tripReport.caf"]; return result; } //录音器的相关设置 -(NSDictionary *)audioRecordingSetting{ NSDictionary *result = nil; NSMutableDictionary *settings = [[NSMutableDictionary alloc]init ]; //录制的音频格式 [settings setValue:[NSNumber numberWithInteger:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey]; //采样率 [settings setValue:[NSNumber numberWithFloat:44100.0f] forKey:AVSampleRateKey]; //信道数 [settings setValue:[NSNumber numberWithInteger:1] forKey:AVNumberOfChannelsKey]; //录音的质量 [settings setValue:[NSNumber numberWithInteger:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey]; [settings setValue:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey]; [settings setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey]; result = [NSDictionary dictionaryWithDictionary:settings]; return result; } -(void)play{ AVAudioSession *audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil]; [audioSession setActive:YES error:nil]; NSError *playBackError = nil; NSError *readingError = nil; NSString *audioPath = [self audioRecordingPath]; // NSString *audioPath = [[NSBundle mainBundle]pathForResource:@"Love" ofType:@"mp3"]; NSLog(@"path:%@",audioPath); NSURL *url = [NSURL URLWithString:audioPath]; NSData *fileData = [NSData dataWithContentsOfFile:audioPath options:NSDataReadingMapped error:&readingError]; self.audioPlayer = [[AVAudioPlayer alloc]initWithData:fileData error:&playBackError]; if (self.audioPlayer != nil) { NSLog(@"finishi3"); self.audioPlayer.delegate = self; self.audioPlayer.volume = 50.0; if ([self.audioPlayer prepareToPlay]) { NSLog(@"finishi4"); UInt32 doChangeDefaultRoute = 1; AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(doChangeDefaultRoute), &doChangeDefaultRoute); [self.audioPlayer play]; } }else{ NSLog(@"error:%@",[playBackError description]); } } ``` 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览