2013-03-07 18 views
6

私は、このパス(ファイル://localhost/var/mobile/Applications/8F81BA4C-7C6F-4496-BDA7-30C45478D758/Documents/sound.wav)をオーディオファイル、つまり記録したものに変換しました。NSDataを渡すとオーディオを再生する方法は?

私はこのパスをNSDataに変換しています。例:

たNSDataがあり、私はこれのNSDataをプレイしたい

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

私は

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

でもない演奏以下のように、urlStringは空として印刷されませんでした。

同じパス我々はコードの下に使用している場合(file://localhost/var/mobile/Applications/8F81BA4C-7C6F-4496-BDA7-30C45478D758/Documents/sound.wav)

再生されている:

-(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

何 '[[AVAudioPlayerのalloc] initWithData:...]について'あなたの場合データはすでにメモリに入っていますか? –

+0

これは動作していません.... thats私はこのコードを.wavファイルを再生するために使用:NSString * str = [NSString stringWithFormat:@ "http://message.morphytron.com/vfiles/%@"、obj.voiceFileString ]; self.palyer = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:str]]; [self.palyer play]; @MartinR – Babul

+0

パスをNSDataにどのように変換しましたか? –

答えて

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 can not play NSData ... –

+3

https://developerを使用してください。 apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Chapters/Reference.html – BhushanVU

+1

nsdataが動作しているavPlayer init、parse.comテーブルから取得した自分のnsdata - > self.avPlayer = [[AVAudioPlayer alloc] initWithData:audioDataエラー:&エラー]; (Bhushan Uparkarに感謝します) – tmr

関連する問題