在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生成声音?

最佳答案

您可以尝试:

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模块

关于ios - 如何在Objective-C中从字节数组创建音频?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26317481/

10-11 21:35