播放声音文件AVAudioPlayer

NSData *data=[NSData dataWithBytes:pbWkD length:pbWkL];

//*nsdata保存成文件,从文件播放声音
NSArray *paths1=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDirectory1=[paths1 objectAtIndex:0];
NSString *savedImagePath1=[documentsDirectory1 stringByAppendingPathComponent:@"test.wav"];

[data writeToFile:savedImagePath1 atomically:NO];
//将文件路径转换为nsurl
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:savedImagePath1];
m_player= [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil]; 
*/

//直接从nsdata播放声音
NSError *error;
m_player=[[AVAudioPlayer alloc] initWithData:data error:&error];
[m_player setVolume:volume];
//[m_player setVolume:2.0];
[m_player prepareToPlay];

你可能感兴趣的:(play)