gpt4 book ai didi

ios - 如何获取在NsData中下载的音频文件?

转载 作者:行者123 更新时间:2023-12-01 19:01:30 24 4
gpt4 key购买 nike

在我的项目中,我有音频网址...看我的代码...。

ViewController.h

#import <UIKit/UIKit.h>

#import <AVFoundation/AVFoundation.h>


@interface ViewController : UIViewController<AVAudioPlayerDelegate,NSURLConnectionDelegate>


{


AVAudioPlayer *song;


// NSMutableData *fileData;


NSData *myData;


}


@property (nonatomic, retain) AVAudioPlayer *song;


@property(nonatomic,retain) NSData *myData;


@end

ViewController.m
#import "ViewController.h"


@interface ViewController ()


@end

@implementation ViewController

@synthesize song,myData;


- (void)viewDidLoad

{

[super viewDidLoad];

NSURL *myUrl;

myUrl = [NSURL URLWithString:@"http://p0.bcbits.com/download/track/60523f9784a2912348eff92f7b7e21e8/mp3-128/1269403107?c22209f7da1bd60ad42305bae71896761f6cd1f4d6c29bf401ef7e97b90c3d5939b57f5479b37ad4d41c48227740d7ba8b5f79b76aa8d6735b8f87c781f44fb019762a2903fe65aeafb30d90cb56b385e27cd770cc9f9d60e5ea3f130da6ad3db728ff7e24a09fab9f351120810ee91f78f1e94fcabc98aabb37d4248358f6da4a0fa7a74fe8c89da2a768&fsig=cd06c325fc41b6f8edb0409011e8839c&id=1269403107&stream=1&ts=1395489600.0"];


myData = [NSData dataWithContentsOfURL:myUrl];



}




- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];


// Dispose of any resources that can be recreated.
}

@end

在上面的代码中如何将mydata转换为一个数组?
或如何下载此音频并将其存储在手机内存中?

最佳答案

您可以将音频数据存储到设备内存中,如下所示

myData = [NSData dataWithContentsOfURL:myUrl];

[myData writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/audio.mp3"] atomically:YES];

播放保存在设备中的音频文件:

AudioToolbox.frameworkAVFoundation.framework添加到您的项目构建中

在.h文件中
#import <AudioToolbox/AudioToolbox.h>

#import <AVFoundation/AVFoundation.h>

AVAudioPlayer *player;

在.m文件中
-(void)initPlayer
{
AVAudioSession *audioSession = [AVAudioSession sharedInstance];

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;

AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

NSError *err;

[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&err];

audioSession.delegate = self;

[audioSession setActive:YES error:&err];

player = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/audio.mp3"]] error:nil];

}

-(void)playFile
{

if (player.playing)
{
[player pause];

isPlaying = NO;

}
else
{
[player play];

isPlaying = YES;

}

}

谢谢!

关于ios - 如何获取在NsData中下载的音频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22576603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com