如果我们通过NSData,如何播放音频? [英] How to play an audio if we pass NSData?

查看:103
本文介绍了如果我们通过NSData,如何播放音频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我转换了这个路径(文件://localhost/var/mobile/Applications/8F81BA4C-7C6F-4496-BDA7-30C45478D758/Documents/sound.wav),这是一个音频文件,即录制的。

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.

我正在将此路径转换为NSData。

I am converting this path to NSData.

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 >

我想玩这个NSData,

I want to play this NSData ,

我确实喜欢下面

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打印为空。

But not played, urlString is printing as empty.

相同路径(文件://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];
    }
}


推荐答案

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

 

这篇关于如果我们通过NSData,如何播放音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆