0

我是 iPhone 应用程序开发的初学者。但我在 android 应用程序开发方面有知识,我的第一个手机项目是使用 AV 音频播放器流式播放 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];

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

4

1 回答 1

1

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

以下是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。

于 2013-09-02T14:44:32.493 回答