ios 播放安卓上传的amr文件

安卓上传的语音格式是amr,而AVAudioPlayer并不支持播放amr文件格式。

思路是把amr文件转成wav文件。

NSString *urlStr=[NSString stringWithFormat:@"%@/msp%@",ZJURL,self.shareF.shareInfo.referUris[0]];
        NSURL *url = [[NSURL alloc]initWithString:urlStr];
        NSData * audioData = [NSData dataWithContentsOfURL:url];
        
        //将数据保存到本地指定位置
        NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        filePath = [NSString stringWithFormat:@"%@/%@.amr", docDirPath , self.shareF.shareInfo.idNum];
        
        wavfile=[NSString stringWithFormat:@"%@/%@.wav", docDirPath , self.shareF.shareInfo.idNum];
        
        mp3file=[NSString stringWithFormat:@"%@/%@.mp3", docDirPath , self.shareF.shareInfo.idNum];
        [audioData writeToFile:filePath atomically:YES];
        [audioData writeToFile:mp3file atomically:YES];
//        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        [VoiceConverter amrToWav:filePath wavSavePath:wavfile];
//        });

iOS上传的mp3,所以进行判断

 NSFileManager * fileManager=[ NSFileManager defaultManager ];
 
        //播放本地音乐
        float msize= [[fileManager attributesOfItemAtPath:wavfile error:nil] fileSize]/(1024.0*1024.0);
        
        CDLog(@"wav文件大小:%f",msize);
        NSURL *fileURL;
        if (msize>0) {
            CDLog(@"mav文件");
            fileURL=[NSURL fileURLWithPath:wavfile];

        }else{
            CDLog(@"mp3文件");
            fileURL=[NSURL fileURLWithPath:mp3file];

        }
        self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
        [self.player play];
        
        self.player.delegate = self;

这里面需要VoiceConvert第三方库,网上找的会报错,不支持arm64,需要引入这些

ios 播放安卓上传的amr文件_第1张图片



你可能感兴趣的:(ios 播放安卓上传的amr文件)