使用从webservice响应获取的字节播放音频

时间:2012-09-27 07:37:47

标签: ios web-services audio byte

我收到了下面的文字

b2NSK5YTVnOxYBZzmNEo5j2VZwsq4DhfHoX9SnQqVVuiynVWl94Tt1a1NFKICtRFbwAd0KJGrnKGbu3NMsjPq6on7W561W3zveYmaa / X // 6JFVZx / 871 / 8NS + / + BKmacsSUnI225G0gCQVnYRyRaDZa61N6ZM KD + lY2RnLE4KPJiULcXAkcA + YzYYzfNMKVYqkMP1CEg7lhdcWEoMScMEXgtwuCcplRNctAfFQNwWBoXjd0xbqk8dVm4tjdhGmTJpHakupbqIC2 / 5wazxs6CNn9f / 7okiwgAOvPlVrL2roeYfKrWXvXQ8Y81msvaux7Z5qdZe9dBmg0QXKJYqCUp9dtv99gCoCOhlLgKFD6ki1f1l70jh7Z2RpQ6aPlDKEKE0G + uB74ZkEf8fIEs + GUWxOnQejS3tmj0jF4hwkc + N4 + Eb4DXtyDYxYKETSJGj3yl8eG05ulr6ObV1y069r5xneFj1 /// x9yHzFjbpXGt // 4hxAsKTKiWIwJKTjbjbiaQBYDMzIaKJTwYYzhByA3XlC + b7afaGqJVs2EXcgJci6t5287x + KxD2UrIq6gMJwHdSWFHjaJM + dHEmPpwdLgzFMggJdBQlwEQXD5anHi6yKbVlBcTVNQ + lJFzzJIUzA3NG / qRQGcdEWU1f8oqWcQqfOGtRBUv1uu / 32AKopkfSsFHmkMuRTShaXEHBnm1Lq06ibo7CG5F1LkXVWp2MWcisq9LBM3U0hBdsSxo7Fo33cMv0c9V5A + xFR3EOPWD + FBAnnv0J3lMuWqozOTNh4SLlXcXVqe9I8TX /// xWi + ga6xr5x // 8OOMtmPjf8m1ZEkpONtttppQJkl5hssHVHICulId6WBQ29lG2wU / Og3WU9YuiRViCbXBElY42eHLdPk5VqAjNFJ3NryVVjtcYGBCsVUo8CZMCmHGNZOO6sixLDKrHjollze / ht2XMnvj + JPFIwj / 6 // VvJY7NPlWH / + 6BIsgADz0HV6y9q7Hv oSp1l710OqPNZrL1rscOeqmWGNXa4OtyzENabAa6urpcR1LhAM4JEaisRTHkpeZw4cvpdu + N4rn2A4XVAyY4GoUE03YBXR7A66RFyKlo0 / Elj1cqKiix0fGJYNxNAJmEYwMTRaIjSKYznDpgRmcSSgTzZG / 5SQWp6upaR0Wn7fmdK9LMzgpes6 / ClyzJVr / 7 + 6xfJxSg + 8gMP1EOGXJBqxZx6L8FCkSnVErcjxmKdEDYVmT6NxlUWal62iVNkrDnWYTfHlnG1FRdNhxjsHRaAnNALI8sFGQUVGuMVTDSarJxLvHmYSaR9WtlIkoj / rWYFQkFOrUj6JkfSWUdMuzmrefehYsPoAW + uvpcWSbBI + 2HOB5aByEh624zj1ZvTyPUwTmQ3J9VMt4fCoXnEmB9HVAlEajmCf2E0h7RCfx2vYWKzIdpSDliSC8dNApaAI0aLAIYcJqgSuJKpEWJqsfhluwyyYorLdXZSJov / WtAqF5D1I + syP1q3d2UUkefegeJAO4qoAEuNt3 / 0D0DQFKjQEKsdVhd1OCOO9Dk5P1wWEmiX5N7iujDL8rW4rR3EPRFnpeNNpvwiJLvLhhpR + fe9l6fOCQSlox1 + 43bUClVeZL + 5RfSJNpy0YzlFG6q4iZjX9d2zfFGiBb6 /// 3ZMGr / BF + v8ZvHZv9T9Mj /

在webservice响应中,我想使用上面的字节字符串播放音频。 我试过了

NSString *str=[TBXML textForElement:result];
str= [str stringByReplacingOccurrencesOfString:@" " withString:@""];

NSData *data= [[NSData alloc] init];

data = [str dataUsingEncoding:NSUTF8StringEncoding];

if (data)
     [objApp.viewAya playAudio:data];

然而,播放器显示错误:操作无法完成。请帮助我使用webservices从字节数据中播放和下载音频。

1 个答案:

答案 0 :(得分:1)

这是代码

static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";


- (id)dataWithBase64EncodedString:(NSString *)string
{
    if (string == nil)
        [NSException raise:NSInvalidArgumentException format:nil];
    if ([string length] == 0)
        return [NSData data];

    static char *decodingTable = NULL;
    if (decodingTable == NULL)
    {
        decodingTable = malloc(256);
        if (decodingTable == NULL)
            return nil;
        memset(decodingTable, CHAR_MAX, 256);
        NSUInteger i;
        for (i = 0; i < 64; i++)
            decodingTable[(short)encodingTable[i]] = i;
    }

    const char *characters = [string cStringUsingEncoding:NSASCIIStringEncoding];
    if (characters == NULL)     //  Not an ASCII string!
        return nil;
    char *bytes = malloc((([string length] + 3) / 4) * 3);
    if (bytes == NULL)
        return nil;
    NSUInteger length = 0;

    NSUInteger i = 0;
    while (YES)
    {
        char buffer[4];
        short bufferLength;
        for (bufferLength = 0; bufferLength < 4; i++)
        {
            if (characters[i] == '\0')
                break;
            if (isspace(characters[i]) || characters[i] == '=')
                continue;
            buffer[bufferLength] = decodingTable[(short)characters[i]];
            if (buffer[bufferLength++] == CHAR_MAX)      //  Illegal character!
            {
                free(bytes);
                return nil;
            }
        }

        if (bufferLength == 0)
            break;
        if (bufferLength == 1)      //  At least two characters are needed to produce one byte!
        {
            free(bytes);
            return nil;
        }

        //  Decode the characters in the buffer to bytes.
        bytes[length++] = (buffer[0] << 2) | (buffer[1] >> 4);
        if (bufferLength > 2)
            bytes[length++] = (buffer[1] << 4) | (buffer[2] >> 2);
        if (bufferLength > 3)
            bytes[length++] = (buffer[2] << 6) | buffer[3];
    }

    realloc(bytes, length);
    return [NSData dataWithBytesNoCopy:bytes length:length];
}

并以这种方式调用它: -

NSData *try = (NSData*)[self dataWithBase64EncodedString:[dict objectForKey:@"Data"]];
imageview.image = [UIImage imageWithData:try];

希望它有所帮助......