我在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 :(得分:1)
您应该检查文件是否在本地可用 - &gt; (无效)歌曲:
@Html.DropDownList("Operator", new List<SelectListItem>
{
new SelectListItem() {Text = "/", Value = "/", Selected = true},
new SelectListItem() {Text = "*", Value = "*"},
new SelectListItem() {Text = "+", Value = "+"},
new SelectListItem() {Text = "-", Value = "-"}
}, new {@Class = "form-control", @Id = "Operator"})