Хочу сделать музыкальный проигрыватель. Но не знаю как можно скачать файл с облака и сохранить на устройстве. Помогите пожалуйста с решением этой проблемы.
код:
AudioPlayer.m
#import "AudioPlayer.h"
@implementation AudioPlayer
- (void)initPlayer:(NSString*) audioFile fileExtension:(NSString*)fileExtension
{
NSURL *audioFileLocationURL = [[NSBundle mainBundle] URLForResource:audioFile withExtension:fileExtension];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileLocationURL error:&error];
}
- (void)playAudio {
[self.audioPlayer play];
}
- (void)pauseAudio {
[self.audioPlayer pause];
}
- (BOOL)isPlaying {
return [self.audioPlayer isPlaying];
}
-(NSString*)timeFormat:(float)value{
float minutes = floor(lroundf(value)/60);
float seconds = lroundf(value) - (minutes * 60);
int roundedSeconds = lroundf(seconds);
int roundedMinutes = lroundf(minutes);
NSString *time = [[NSString alloc]
initWithFormat:@"%d:%02d",
roundedMinutes, roundedSeconds];
return time;
}
- (void)setCurrentAudioTime:(float)value {
[self.audioPlayer setCurrentTime:value];
}
- (NSTimeInterval)getCurrentAudioTime {
return [self.audioPlayer currentTime];
}
- (float)getAudioDuration {
return [self.audioPlayer duration];
}
@end
AudioPlayer.h
#import
@interface AudioPlayer : UIViewController
@property (nonatomic, retain) AVAudioPlayer *audioPlayer;
- (void)initPlayer:(NSString*) audioFile fileExtension:(NSString*)fileExtension;
- (void)playAudio;
- (void)pauseAudio;
- (BOOL)isPlaying;
- (void)setCurrentAudioTime:(float)value;
- (float)getAudioDuration;
- (NSString*)timeFormat:(float)value;
- (NSTimeInterval)getCurrentAudioTime;
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.audioPlayer = [[AudioPlayer alloc] init];
[self setupAudioPlayer:@"gendel-allilujjja-(mp3-crazy.com)"];
NSString *stringURL = @"https://drive.google.com/uc?export=download&id=0B0EJbcHq3ZALWUo0a05LMWNzeDg";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"gendel-allilujjja-(mp3-crazy.com)"];
[urlData writeToFile:filePath atomically:YES];
}
}
- (BOOL)prefersStatusBarHidden
{
return NO;
}
- (void)setupAudioPlayer:(NSString*)fileName
{
NSString *fileExtension = @"mp3";
[self.audioPlayer initPlayer:fileName fileExtension:fileExtension];
self.currentTimeSlider.maximumValue = [self.audioPlayer getAudioDuration];
self.timeElapsed.text = @"0:00";
self.duration.text = [NSString stringWithFormat:@"-%@",
[self.audioPlayer timeFormat:[self.audioPlayer getAudioDuration]]];
}
- (IBAction)playAudioPressed:(id)playButton
{
[self.timer invalidate];
if (!self.isPaused) {
[self.playButton setBackgroundImage:[UIImage imageNamed:@"audioplayer_pause.png"]
forState:UIControlStateNormal];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateTime:)
userInfo:nil
repeats:YES];
[self.audioPlayer playAudio];
self.isPaused = TRUE;
} else {
[self.playButton setBackgroundImage:[UIImage imageNamed:@"audioplayer_play.png"]
forState:UIControlStateNormal];
[self.audioPlayer pauseAudio];
self.isPaused = FALSE;
}
}
- (void)updateTime:(NSTimer *)timer {
if (!self.scrubbing) {
self.currentTimeSlider.value = [self.audioPlayer getCurrentAudioTime];
}
self.timeElapsed.text = [NSString stringWithFormat:@"%@",
[self.audioPlayer timeFormat:[self.audioPlayer getCurrentAudioTime]]];
self.duration.text = [NSString stringWithFormat:@"-%@",
[self.audioPlayer timeFormat:[self.audioPlayer getAudioDuration] - [self.audioPlayer getCurrentAudioTime]]];
if (![self.audioPlayer isPlaying]) {
[self.playButton setBackgroundImage:[UIImage imageNamed:@"audioplayer_play.png"]
forState:UIControlStateNormal];
[self.audioPlayer pauseAudio];
self.isPaused = FALSE;
}
}
- (IBAction)setCurrentTime:(id)scrubber {
[NSTimer scheduledTimerWithTimeInterval:0.01
target:self
selector:@selector(updateTime:)
userInfo:nil
repeats:NO];
[self.audioPlayer setCurrentAudioTime:self.currentTimeSlider.value];
self.scrubbing = FALSE;
}
- (IBAction)userIsScrubbing:(id)sender {
self.scrubbing = TRUE;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
ViewController.h
#import
@interface ViewController : UIViewController
@property (nonatomic, strong) AudioPlayer *audioPlayer;
@property (weak, nonatomic) IBOutlet UISlider *currentTimeSlider;
@property (weak, nonatomic) IBOutlet UIButton *playButton;
@property (weak, nonatomic) IBOutlet UILabel *duration;
@property (weak, nonatomic) IBOutlet UILabel *timeElapsed;
@property BOOL isPaused;
@property BOOL scrubbing;
@property NSTimer *timer;
@end
Ответ
Ну допустим скачать можно вот так
NSString *stringURL = @"http://www.cloud.com/music.mp3";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"music.mp3"];
[urlData writeToFile:filePath atomically:YES];
}
Загрузить, и начать играть
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"music.mp3"];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filePath] error:nil];
[self.player play];
Комментариев нет:
Отправить комментарий