0

Hi i'm new in ios and i want to make an app that play radio, so the app working fine, but when i tried to include the volume to the app, it's cruch and gives me this error :

[AVPlayer setVolume:]: unrecognized selector sent to instance 0x9d4f500
2013-05-16 09:34:47.785 RadioFM[22061:12503] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AVPlayer setVolume:]: unrecognized selector sent to instance 0x9d4f500'

and here is my source code that i'm using :

in PlayerViewController.h:

@interface PlayerViewController : UIViewController
{ AVAudioPlayer *radioPlayer; IBOutlet UISlider *Volume; }
- (IBAction)Volume:(id)sender;

@property (nonatomic,retain) AVAudioPlayer *radioPlayer;

and in PlayerViewController.m:

@synthesize radioPlayer;

- (void)viewDidLoad
{
    NSString *RadStream=<@Radio stream>;
    NSURL *url=[NSURL URLWithString:RadStream];
    NSError *error;
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url];
    self.radioPlayer = [AVPlayer playerWithPlayerItem:playerItem];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self.radioPlayer play];
    [super viewDidLoad];

}

and i have a slider view and on the action of this slider i have :

- (IBAction)Volume:(id)sender {
if (self.radioPlayer != nil)
{
    self.radioPlayer.volume = Volume.value;
}

}

so please can any one help me on this ? Thank you advance.

Hamza
  • 1
  • 2
  • Check the Name of Your Function i think first it was `setVolume`. so disconnet binding and bind it with `Volume`. And try this to change the volume `- (void)sliderValueChanged:(UISlider *)slider { myPlayer.volume = slider.value / 100.0; }` – Buntylm May 16 '13 at 08:52
  • actually it's was not setVolume, and to check if that true i just created a new uislider programmatically and i called the function that you wrote above and it's keep give me the same error, the new source code is : is viewdidload methode i create a new uislider and i called the methode above. – Hamza May 16 '13 at 11:05
  • try this https://github.com/mattgallagher/AudioStreamer – Buntylm May 16 '13 at 11:08

4 Answers4

1

First Check the Name of Your Function i think first it was setVolume. so disconnet binding and bind it with Volume. And try this to change the volume

- (void)sliderValueChanged:(UISlider *)slider 
{ 
myPlayer.volume = slider.value / 100.0; 
}

And bind your Silder with UIControlEventValueChanged. Action

Buntylm
  • 7,345
  • 1
  • 31
  • 51
1

Firstly, are you using AVPLayer or AVAudioPlayer. I am asking this question, cuz

  • you have declared the radioPLayer as AVAudioPLayer.

    @property (nonatomic,retain) AVAudioPlayer *radioPlayer;

  • and initialize the radioPlayer as AVPlayer

    self.radioPlayer = [AVPlayer playerWithPlayerItem:playerItem];

If you are planning on using AVAudioPlayer, life becomes easy. Use the volume property

self.radioPlayer.volume = iSlider.value;

If you are using AVPLayer, then you can refer to these steps. Not tried it, but seems like it has helped many: https://stackoverflow.com/a/6178912/942966

Community
  • 1
  • 1
Roshit
  • 1,589
  • 1
  • 15
  • 37
  • yes what you said is correct, i declared the radioPLayer as `AVAudioPLayer` and i initialize it as `AVPlayer` cuz `playerWithPlayerItem:playerItem` does not work with `AVAudioPLayer`, and when i tried to use `[[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];` the radio does nor even play, i don't know if the `AVPlayer` does not support that streams or what !! – Hamza May 16 '13 at 11:32
  • `AVAudioPLayer` cant be used for streaming. `AVPlayer` has to be used. So go ahead and declare the `radioPlayer` as an `AVPlayer`. Volume however cant be set in AVPlayer, so instead goto the link I referred to and try using `AudioMix` to set that up – Roshit May 16 '13 at 14:01
0

please check IBAction events is UIControlEventValueChanged

dhaya
  • 1,522
  • 13
  • 21
  • yes, it's UIControlEventValueChanged. and if i tried to print the values of the uisilder is print me the values as expected. – Hamza May 16 '13 at 11:24
0
self.radioPlayer.volume = Volume.value;

above line have nothing any meaning. Volume.value means setVolume & because of there is no such method ,it is giving error.

assigning value according to slider movement is good one.

Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
  • assigning value according to slider movement is that what i tried to do, and also when i tried to give the volume to the radio player hard coded does not work, like `self.radioPlayer.volume = 0.8;` for example. – Hamza May 16 '13 at 11:11