如何流式传输音频并能够控制iOS上的音量?

时间:2013-02-01 10:10:02

标签: ios objective-c avaudioplayer avplayer

我开发了一个iOS应用,可同时播放两个独立的音频流。要做到这一点,我使用AVPlayer因此:

//Use URL to set up the speech radio player.
NSURL *urlStreamSpeech = [NSURL URLWithString:self.urlAddressSpeech];
playerSpeech = [AVPlayer playerWithURL:urlStreamSpeech];

//Use URL to set up the music player.    
NSURL *urlStreamMusic = [NSURL URLWithString:self.urlAddressMusic];
playerMusic = [AVPlayer playerWithURL:urlStreamMusic];

我现在正在尝试实现音量控制,以便用户可以单独控制音频流的音量。正如我已经尝试过的那样,我得出的结论是AVPlayer类没有volume属性。我还查看了AVAudioPlayer类,但从我收集的内容来看,该类只能播放本地音频文件,而不能播放流式音频。

所以我的问题是:如何控制iOS上的流式音频音量?

2 个答案:

答案 0 :(得分:0)

NSString* resourcePath = url; //your url
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]];
NSError *error;

app.audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
app.audioPlayer.numberOfLoops = 0;
app.audioPlayer.volume = 1.0f;
[app.audioPlayer prepareToPlay];

if (app.audioPlayer == nil)
    NSLog(@"%@", [error description]);
else
    [app.audioPlayer play];

查看AVPlayer类,AVFoundation框架的一部分......这是用于流音频的objective-c(iOS)类。

查看此帖子Live streaming

答案 1 :(得分:0)

MPMusicPlayerController* musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
[musicPlayer setVolume:[SliderVolumen value]];

仅在设备中工作,无需模拟器。

与AVPlayer兼容。

相关问题