作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的项目中,我有音频网址...看我的代码...。
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
#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 = [NSData dataWithContentsOfURL:myUrl];
[myData writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/audio.mp3"] atomically:YES];
AudioToolbox.framework
和
AVFoundation.framework
添加到您的项目构建中
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
AVAudioPlayer *player;
-(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/
我是一名优秀的程序员,十分优秀!