我想創建一個應用程序,將播放一些音頻文件。我希望能夠從網絡服務器傳輸音頻,但到目前爲止,我不知道如何鏈接到http url,因此直接將mp3放到了項目中。AVAudioPlayer沒有恢復時,按停止按鈕
我已經創建了一個停止按鈕,我的理解是,如果您按停止,然後再次播放,mp3文件應該從停止的位置恢復。這不會發生在我身上。任何線索,我做錯了,請嗎?我也嘗試過使用暫停,但沒有改變。提前謝謝了。下面是我的.h文件中的代碼。
另外,如果我想擁有多個音頻文件,我需要做什麼?
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
AVAudioPlayer *player;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)play:(id)sender {
NSURL *songURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"hustle" ofType:@"mp3"]];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:songURL error:nil];
player.volume = 0.5;
[player play];
}
- (IBAction)pause:(id)sender {
[player stop];
}
- (IBAction)volumeChanged:(id)sender {
player.volume = volumeSlider.value;
}
@end
這裏是與陣列的編輯.m文件:
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
AVAudioPlayer *player;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//NSURL *songURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"hustle" ofType:@"mp3"]];
//player = [[AVAudioPlayer alloc] initWithContentsOfURL:songURL error:nil];
//player.volume = 0.5;
songsArray = [[NSMutableArray alloc] initWithObjects:@"hustle", @"hustle2", nil];
NSUInteger currentTrackNumber;
currentTrackNumber=0;
}
- (IBAction)startPlaying
{
if (player) {
[player stop];
player = nil;
}
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[[NSString alloc] initWithString:[songsArray objectAtIndex:currentTrackNumber]] ofType:@"mp3"]] error:NULL];
player.delegate = self;
[player play];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player1 successfully:(BOOL)flag
{
if (flag) {
if (currentTrackNumber < [songsArray count] - 1) {
currentTrackNumber ++;
if (player) {
[player stop];
player = nil;
}
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:[[NSString alloc] initWithString:[songsArray objectAtIndex:currentTrackNumber]] ofType:@"mp3"]] error:NULL];
player.delegate = self;
[player play];
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)play:(id)sender {
[player play];
}
- (IBAction)pause:(id)sender {
[player stop];
}
- (IBAction)volumeChanged:(id)sender {
player.volume = volumeSlider.value;
}
@end
你應該暫停,而不是回採,'[播放暫停]'那麼你必須再次發揮,以恢復它。 – zaheer 2014-11-06 04:32:40