2016-02-28 25 views
0

我在tableViewController中有表格單元格。然後,我點擊單元格音頻文件下載,我可以播放它。但後來我在tableViewController中返回並再次單擊單元格音頻文件下載。如何下載音頻一次?如何下載音頻一次?

AudioPlayer.m

#import "AudioPlayer.h" 

@implementation AudioPlayer 


- (void) song{ 
if (_index1 == 0) { 
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,@"music.mp3"]; 
    [urlData writeToFile:filePath atomically:YES]; 
    self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filePath] error:nil]; 

    } 
} 

} 

- (void)initPlayer:(NSString*) audioFile fileExtension:NSURL *audioFileLocationURL = [[NSBundle mainBundle] URLForResource:audioFile withExtension:fileExtension]; 
NSError *error; 
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileLocationURL error:&error]; 
[self song]; 
} 


- (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 <UIKit/UIKit.h> 
    #import <AVFoundation/AVFoundation.h> 

    @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]; 
if (_index == 0) { 
    self.audioPlayer = [[AudioPlayer alloc] init]; 
    [self setupAudioPlayer:@""]; 
} 
} 

- (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 <UIKit/UIKit.h> 
#import "AudioPlayer.h" 

@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 
+0

這聽起來像你需要緩存的音頻,爲您的TVC數組無論是在或保存在磁盤上。 – sschale

+0

就這麼簡單...檢查你的文件是否存在...檢查這個變量... filePath –

+0

@FahimParkar請給我代碼示例。 – Dark

回答

4

您應該檢查,如果該文件是本地可用這裏 - >(無效)歌曲:

NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"music.mp3"]; 

//Check if file is available locally 
//if available load file from the device 
{ 
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filePath] error:nil]; 
} 
//else: 
{ 
[urlData writeToFile:filePath atomically:YES]; 
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:   [NSURL URLWithString:filePath] error:nil]; 

} 
+0

不起作用。我如何檢查«//檢查文件是否在本地可用 //如果可用的裝載文件»? – Dark