0

I am very beginner of iPhone Application development.But i am having knowledge in android application development and my first i phone project is streaming shout cast URL using AV Audio Player and i am using following code to streaming the 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];

it works fine and i am having UI Slider for volume control by using [player setvolume:0.5],my app is crashing..Please give me suggestions...

Android_kalai
  • 423
  • 1
  • 7
  • 19
  • 1
    It would help to get some console output of why/how it is crashing. – HalR Sep 02 '13 at 06:09
  • -[AVPlayer setVolume:]: unrecognized selector sent to instance 0x8375c20 2013-09-02 11:45:09.753 VolumeControl[1577:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AVPlayer setVolume:]: unrecognized selector sent to instance 0x8375c20' This error message i got – Android_kalai Sep 02 '13 at 06:17
  • Please give me suggestions for my problem..I really spends most of the time with it..I am forward to your solutions.... – Android_kalai Sep 02 '13 at 06:19
  • I'm so sorry, I just got out of bed this morning. – HalR Sep 02 '13 at 14:42

1 Answers1

1

It might sound counter-intuitive, but you don't set audio with AVPlayer. You have to change volume using the audio mix with the AVPlayerItem, not with the player.

Here is a sample on how to do it 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];

your code should end up looking something like this:

   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];

and your volume, which is set in this line:

    [audioInputParams setVolume:volume atTime:kCMTimeZero];

also, if you save a reference to your playerItem and audioTracks, you can change the volume during playback with a call like this:

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];

should be from 0.0 to 1.0.

Community
  • 1
  • 1
HalR
  • 11,411
  • 5
  • 48
  • 80
  • I just want another clarification.In your answer you specify asset in first line..What is that?How to declare the asset in my project?? – Android_kalai Sep 03 '13 at 04:11
  • Add added these lines to my project and assign AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil]; But nothing happend.Volume not changed – Android_kalai Sep 03 '13 at 04:22
  • I added a line to the front of my answer. – HalR Sep 03 '13 at 04:22
  • Yes i modified coding as above i mentioned..but volume not changed:( – Android_kalai Sep 03 '13 at 04:36
  • You are setting the volume to 0 with this line: [audioInputParams setVolume:0.0 atTime:kCMTimeZero]; so you won't hear anything. You need to have the value be higher than 0.0, but less than or equal to 1.0 in order to hear anything. – HalR Sep 03 '13 at 04:59
  • yes you are right..Instead of setting volume as 0.0 i hearing the full sound audio that is the problem.:( – Android_kalai Sep 03 '13 at 05:08