AVFoundation 音乐播放器的 IOS 问题,50% 的时间都在工作
Posted
技术标签:
【中文标题】AVFoundation 音乐播放器的 IOS 问题,50% 的时间都在工作【英文标题】:IOS issues with AVFoundation music player, working 50% of the time 【发布时间】:2013-09-01 19:45:42 【问题描述】:我在使用 AVFoundation 音乐播放器时遇到了一些问题。
1) 它不会立即启动,我猜模拟器上的缓冲很慢?
2)只有50%的时候有声音启动,另外50%不启动,非常不可靠
这是我的课
Music_Player.h
@interface Music_Player : NSObject <AVAudioPlayerDelegate>
@property (nonatomic, retain) AVAudioPlayer *audioPlayer;
@property (nonatomic, retain) NSString *trackPlaying;
@property (nonatomic) BOOL isPlaying;
@property (nonatomic, retain) NSTimer *timer;
@property (nonatomic, retain) UISlider *slider;
-(void)initTrack: (NSString *) track;
-(void)startPlayer;
-(void)pausePlayer;
-(void)stopPlayer;
-(void)sliderUp;
-(void)sliderDown;
@end
Music_Player.m
#import "Music Player.h"
@implementation Music_Player
@synthesize audioPlayer;
@synthesize trackPlaying;
@synthesize timer;
@synthesize isPlaying;
@synthesize slider;
-(void)initTrack:(NSString *)track
/* Init slider */
self.isPlaying = FALSE;
self.trackPlaying = track;
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *filePath = [mainBundle pathForResource:self.trackPlaying ofType:@"mp3"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSError *error = nil;
self.audioPlayer = [[AVAudioPlayer alloc] initWithData:fileData error:&error];
[self.audioPlayer prepareToPlay];
/* Set slider max value */
self.slider.minimumValue = 0;
self.slider.maximumValue = self.audioPlayer.duration - 5;
-(void)startPlayer
if (self.isPlaying == TRUE)
NSLog(@"Pause clicked");
[self.audioPlayer pause];
self.isPlaying = FALSE;
else
NSLog(@"Play clicked");
[self.audioPlayer play];
self.isPlaying = TRUE;
if (![self.timer isValid])
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
- (void)updateTime
if (self.isPlaying == TRUE)
NSTimeInterval currentTime = self.audioPlayer.currentTime;
NSLog(@"%f", currentTime);
// update UI with currentTime;
slider.value = round(currentTime);
-(void)pausePlayer
if (self.isPlaying == TRUE)
[self.audioPlayer pause];
self.isPlaying = FALSE;
-(void)stopPlayer
if (self.isPlaying == TRUE)
NSLog(@"Stop clicked");
[self.audioPlayer stop];
self.audioPlayer.currentTime = 0;
self.slider.value = round(self.audioPlayer.currentTime);
self.isPlaying = FALSE;
-(void)sliderUp
if (self.isPlaying == FALSE)
self.audioPlayer.currentTime = round(slider.value);
[self.audioPlayer play];
self.isPlaying = TRUE;
-(void)sliderDown
if (self.isPlaying == TRUE)
self.isPlaying = FALSE;
[self.audioPlayer stop];
/* AUDIO PLAYER DELEGATES */
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
NSLog(@"Did finish with, %c", flag);
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
NSLog(@"Error %@",error);
@end
我刚刚设置了滑块属性并使用我的 viewController 中的轨道对其进行了初始化
/* Init Music */
self.music_player = [[Music_Player alloc] init];
self.music_player.slider = self.slider;
[self.music_player initTrack:@"track1"];
然后我只是将 Btn 点击和滑块值更改传递给 music_player 类,可能是什么问题?我明天将在 iPhone 上测试它,所以它可能只是一个模拟器问题吗?
【问题讨论】:
【参考方案1】:两件事:
initWithData:error:
中,调用后是否设置错误?
为什么不使用[AVAudioPlayer initWithContentsOfURL:error:]
?例如:
- (void)initTrack:(NSString *)track
self.isPlaying = NO;
self.trackPlaying = track;
NSBundle *mainBundle = ;
NSString *filePath = [[NSBundle mainBundle] pathForResource:self.trackPlaying ofType:@"mp3"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
NSError *error = nil;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
【讨论】:
现在是,我只是设置了委托,但之前没有 那个方法根本找不到文件 对不起,我们应该在这里使用 fileURLWithPath:,而不是 URLWithString:。以上是关于AVFoundation 音乐播放器的 IOS 问题,50% 的时间都在工作的主要内容,如果未能解决你的问题,请参考以下文章