2013-03-07 10 views
6

Przekonwertowałem tę ścieżkę (plik: //localhost/var/mobile/Applications/8F81BA4C-7C6F-4496-BDA7-30C45478D758/Documents/sound.wav), która jest plikiem audio, np. Nagranym.Jak odtwarzać dźwięk, jeśli przekazujemy NSData?

Konwertuję tę ścieżkę do NSData.

NSData jest: Przykład:

<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 23b200b200 79007900 5c015c01 fe01fe01 f101f101 fc01fc01 7b027b02 36023602 > 

chcę grać tę NSData,

zrobiłem jak poniżej

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

, ale nie grał, urlString drukuje jako pusty.

samej ścieżce (file://localhost/var/mobile/Applications/8F81BA4C-7C6F-4496-BDA7-30C45478D758/Documents/sound.wav)

bawi jeśli używamy poniżej kodu:

-(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]; 
    } 
} 
+1

Co o '[[AVAudioPlayer alloc] initWithData: ...]' jeśli ciebie czy dane są już w pamięci? –

+0

To nie działa .... to użyłem tego kodu do odtworzenia pliku .wav: NSString * str = [NSString stringWithFormat: @ "http://message.morphytron.com/vfiles/%@", obj.voiceFileString ]; self.palyer = [[alokacja AVPlayer] initWithURL: [NSURL URLWithString: str]]; [Odtwarzanie selfeglizi]; @MartinR – Babul

+0

Jak przekonwertowałeś ścieżkę do NSData? –

Odpowiedz

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

 

+0

AVAudioplayer nie może odtwarzać NSData ... –

+3

https: // programista. apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Chapters/Reference.html – BhushanVU

+1

avPlayer init z nsdata działał, moje nsdata pobierane z tabeli parse.com -> self.avPlayer = [[AVAudioPlayer alloc] initWithData: audioData błąd: i błąd]; (dziękuję, Bhushan Uparkar) – tmr

Powiązane problemy