如何在iOS上传输音频并控制音量?
我开发了一个iOS应用程序,可以同时播放两个独立的音频流。为此,我使用了如何在iOS上传输音频并控制音量?,ios,objective-c,avaudioplayer,avplayer,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 musi
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
上的流音频音量
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];
查看类,AVFoundation框架的一部分。。。这是您用于流式音频的objective-c(iOS)类
看看这个帖子
仅在设备中工作,无模拟器
与AVPlayer兼容。谢谢navinsillu,但根据苹果的文档,AVAudioPlayer类不支持基于HTTP URL的流式音频。与initWithContentsOfURL:一起使用的URL必须是文件URL(文件://)。即,本地路径。我也不想将远程文件下载到NSData对象中,因为我正在做的是通过HTTP传输无限远程音频流(目前使用的是AVPlayer,它没有我需要的volume属性)。因此,我认为这并不能解决我的问题。
MPMusicPlayerController* musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
[musicPlayer setVolume:[SliderVolumen value]];