0

I have manually uploaded mp3 files in PFFile object,

class: Songlist:

          1)songTitle(string)     2)song(PFFile)

I am using code below:

 - (void)viewDidLoad {
    [super viewDidLoad];
    isPlaying = NO;
     PFQuery *query = [PFQuery queryWithClassName:@"Songlist"];
    [query whereKey:@"songTitle" equalTo:@"Kamli"];
     NSLog(@"QUERY %@",query);

     PFObject *songObj= [query getFirstObject];
         NSLog(@"get Whole song file %@",[query getFirstObject]);

     PFFile *roomAudio = [songObj objectForKey:@"song"];

         NSLog(@"PFfile %@ :",roomAudio);

       NSURL url1 = [NSURL URLWithString:roomAudio.url]; NSData audioData = [NSData   dataWithContentsOfURL:url1];

          NSLog(@"Song  :%lul", audioData.length);

      self.player= [[AVAudioPlayer alloc] initWithData:audioData error:nil];

       self.player.numberOfLoops = -1;
       [self.player prepareToPlay];

   }

-(IBAction)playSong:(id)sender

  {

   if(isPlaying==NO)

    {
      NSLog(@"player should play");
    [player play];
     isPlaying =YES;

     }
       else
        {
         [player pause];
         isPlaying = NO;

        }

 }

I am getting song length "Song :3811456l", so why its not playing it?? please help me find error,Thanks

1 Answers1

0

1)

You don't know what problem your audio player is having because you are not looking at the error returned by this line in your code:

self.player= [[AVAudioPlayer alloc] initWithData:audioData error:nil];

Change it to:

NSError *error;
self.player= [[AVAudioPlayer alloc] initWithData:audioData error:&error];
if(!self.player)
{
    NSLog(@"error opening AVAudioPlayer - %@", [error localizedDescription]);
}

2)

In your "playSong" IBAction, you should be referring to your "self.player" property.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • hi,thanks for help, yes i chnaged it according to u,Now this coming up ::::::error opening AVAudioPlayer - The operation couldn’t be completed. (OSStatus error 1954115647.) – user3605148 May 20 '14 at 19:59
  • and are you still getting a reasonable looking size number for your audio data? what is the URL you are trying to load? is it a URL or is it a string that contains a URL? – Michael Dautermann May 20 '14 at 20:02
  • yes like below:PFfile : Song :3811456l error opening AVAudioPlayer - The operation couldn’t be completed. (OSStatus error 1954115647.) – user3605148 May 20 '14 at 20:04
  • hi i found problem, thanks to Michael Dautermann, i got error and i did according to this:iPhone: AVAudioPlayer unsupported file type:http://stackoverflow.com/questions/4901709/iphone-avaudioplayer-unsupported-file-type Thanks u very much all – user3605148 May 20 '14 at 20:24