iPhone AVAudioPlayer卷问题

时间:2013-09-02 05:31:13

标签: iphone

我是iPhone应用程序开发的初学者。但我对Android应用程序开发有所了解,我的第一个i phone项目是使用AV Audio Player流式传输强制转换URL,我使用以下代码来传输URL

 NSString *urlstr=@"http://live64.jiljilradio.com";

  NSURL* url=[NSURL URLWithString:urlstr];

   AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];

   player = [[AVPlayer playerWithPlayerItem:playerItem]retain];
   [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
   NSLog(@"Play");
    [player play];

它工作正常,我使用[player setvolume:0.5]进行音量控制UI滑块,我的应用程序崩溃了......请给我建议......

1 个答案:

答案 0 :(得分:1)

这听起来可能违反直觉,但您不会使用AVPlayer设置音频。您必须使用AVPlayerItem的音频混合更改音量,而不是使用播放器。

以下是有关如何执行此操作的示例from another answer, by Jesse Crossen

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[self myAssetURL] options:nil];
NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio];

NSMutableArray *allAudioParams = [NSMutableArray array];
for (AVAssetTrack *track in audioTracks) {
  AVMutableAudioMixInputParameters *audioInputParams = 
    [AVMutableAudioMixInputParameters audioMixInputParameters];
  [audioInputParams setVolume:volume atTime:kCMTimeZero];
  [audioInputParams setTrackID:[track trackID]];
  [allAudioParams addObject:audioInputParams];
}

AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
[audioMix setInputParameters:allAudioParams];

[playerItem setAudioMix:audioMix];

你的代码应该看起来像这样:

   AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[self myAssetURL] options:nil];
    NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio];

    NSMutableArray *allAudioParams = [NSMutableArray array];
    for (AVAssetTrack *track in audioTracks) {
        AVMutableAudioMixInputParameters *audioInputParams =
        [AVMutableAudioMixInputParameters audioMixInputParameters];
        [audioInputParams setVolume:volume atTime:kCMTimeZero];
        [audioInputParams setTrackID:[track trackID]];
        [allAudioParams addObject:audioInputParams];
    }

    AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
    [audioMix setInputParameters:allAudioParams];

    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
    [playerItem setAudioMix:audioMix];

    player = [[AVPlayer playerWithPlayerItem:playerItem]retain];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    NSLog(@"Play");
    [player play];

和您的音量,在此行中设置:

    [audioInputParams setVolume:volume atTime:kCMTimeZero];

另外,如果您保存对playerItem和audioTracks的引用,您可以通过以下调用在播放期间更改音量:

NSMutableArray *allAudioParams = [NSMutableArray array];
for (AVAssetTrack *track in audioTracks) {
    AVMutableAudioMixInputParameters *audioInputParams =
    [AVMutableAudioMixInputParameters audioMixInputParameters];
    [audioInputParams setVolume:0.0 atTime:0];
    [audioInputParams setTrackID:[track trackID]];
    [allAudioParams addObject:audioInputParams];
}

AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
[audioMix setInputParameters:allAudioParams];

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
[playerItem setAudioMix:audioMix];

应该是0.0到1.0。