如何在Objective-C中从字节数组创建音频?

时间:2014-10-11 17:47:54

标签: ios objective-c audio bytearray

在android中我使用以下代码行从字节数组生成声音

byte audioData[];
//.
//fill audioData
//.
final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
            sampleRateInHz, AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT, audioData.lenght,
            AudioTrack.MODE_STATIC);
audioTrack.write(audioData, 0, audioData.length);
audioTrack.play();

我如何在ios sdk中实现这一目标。是否有任何等效的库从byteArray生成声音?

1 个答案:

答案 0 :(得分:0)

你可以尝试:

NSData *audioData = // take audioData from wherever it is

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

self.audioPlayer = [[AVAudioPlayer alloc] initWithData:audioData error:nil]; // audioPlayer must be a strong property. Do not create it locally

[self.audioPlayer prepareToPlay];
[self.audioPlayer play];

确保导入AVFoundation模块