7

I converted this path (file://localhost/var/mobile/Applications/8F81BA4C-7C6F-4496-BDA7-30C45478D758/Documents/sound.wav) which is an audio file i.e, recorded.

I am converting this path to NSData.

NSData is : Example :

<00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 64617461 1cf50200 32003200 e2ffe2ff 3cff3cff 08fe08fe 44fe44fe 04fe04fe e6fde6fd 95fd95fd 96fe96fe b9feb9fe ........................................................................................................................f7fef7fe 96ff96ff bdffbdff d6ffd6ff 92009200 23012301 b200b200 79007900 5c015c01 fe01fe01 f101f101 fc01fc01 7b027b02 36023602 >

I want to play this NSData ,

I did like below:

NSString *urlString = [[NSString alloc] initWithData:appDelegate.dataTestingWasteData encoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc] initWithString:urlString];
self.palyer=[[AVPlayer alloc] initWithURL:url];
[self.palyer play];

But not played, urlString is printing as empty.

Same path (file://localhost/var/mobile/Applications/8F81BA4C-7C6F-4496-BDA7-30C45478D758/Documents/sound.wav)

is playing if we use below code :

-(void)playAudio
{
    if (!audioRecorder.recording)
    {
        stopButton.enabled = YES;
        recordButton.enabled = NO;
        NSError *error;
        audioPlayer = [[AVAudioPlayer alloc]
                           initWithContentsOfURL:audioRecorder.url
                           error:&error];
        audioPlayer.delegate = self;
        if (error)
            NSLog(@"Error: %@",[error localizedDescription]);
        else
            [audioPlayer play];
    }
}
Sham Dhiman
  • 1,348
  • 1
  • 21
  • 59
Babul
  • 1,268
  • 2
  • 15
  • 42
  • 1
    What about `[[AVAudioPlayer alloc] initWithData:...]` if you have the data already in memory? – Martin R Mar 07 '13 at 13:20
  • That is not working.... thats i used this code to play the .wav file : NSString *str=[NSString stringWithFormat:@"http://message.morphytron.com/vfiles/%@",obj.voiceFileString]; self.palyer=[[AVPlayer alloc] initWithURL:[NSURL URLWithString:str]]; [self.palyer play]; @MartinR – Babul Mar 07 '13 at 13:24
  • How did you convert the path to NSData? – Martin R Mar 07 '13 at 13:28
  • NSURL *url=[NSURL URLWithString:urlString]; NSData *audiodata = [NSData dataWithContentsOfURL:audioURL]; "urlString" contains the above said path @MartinR – Babul Mar 07 '13 at 13:31
  • 2
    And `audioPlayer = [[AVAudioPlayer alloc] initWithData:audioData error &error];` does not work? What is the error? – Martin R Mar 07 '13 at 13:41
  • I just wonder why you said to me that `[[AVAudioPlayer alloc] initWithData:...]` is not working. Now you have accepted the same code as answer. – Martin R Mar 08 '13 at 08:23
  • yes, when i check i did not get the output, later the same code works for me... if u post the answer may be i given a vote for u... Thanks for ur clarification @MartinR – Babul Mar 09 '13 at 05:46
  • There is no need for another answer, I just wondered. – Martin R Mar 09 '13 at 06:44
  • 1
    Just a thought, but if the file is in the local file system then you should use:[NSURL fileURLwithPath] – Cocoadelica Mar 15 '13 at 09:24

1 Answers1

10
NSString *urlString = [[NSString alloc] initWithData:appDelegate.dataTestingWasteData encoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc] initWithString:urlString];

NSData *wavDATA = [NSData dataWithContentsOfURL:url];
NSError *error;

self.player=[[AVAudioPlayer alloc] initWithData:wavDATA error:&error];
[self.player play];

 

Todd Lehman
  • 2,880
  • 1
  • 26
  • 32
BhushanVU
  • 3,455
  • 1
  • 24
  • 33
  • 3
    https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Chapters/Reference.html – BhushanVU Jul 29 '14 at 05:45
  • 1
    avPlayer init with nsdata worked, my nsdata retrieved from parse.com table -> self.avPlayer=[[AVAudioPlayer alloc] initWithData:audioData error:&error]; (thank you Bhushan Uparkar) – tmr Nov 12 '14 at 08:00