Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play an audio if we pass NSData?

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];
    }
}
like image 915
Babul Avatar asked Mar 07 '13 13:03

Babul


People also ask

What is NSData in Swift?

NSData is toll-free bridged with its Core Foundation counterpart, CFData . See Toll-Free Bridging for more information on toll-free bridging. Important. The Swift overlay to the Foundation framework provides the Data structure, which bridges to the NSData class and its mutable subclass NSMutableData .


1 Answers

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

 

like image 179
BhushanVU Avatar answered Sep 21 '22 01:09

BhushanVU