按下停止按钮时AVAudioPlayer无法恢复

时间:2014-11-06 01:32:04

标签: ios objective-c avaudioplayer

我正在尝试创建一个播放一些音频文件的应用。我希望从网络服务器流式传输音频,但到目前为止我正在将mp3直接放入项目中,因为我不知道如何链接到http网址。

我创建了一个停止按钮,我的理解是,如果你按停止然后再次播放,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

2 个答案:

答案 0 :(得分:1)

Write the code like this, when you click on the pause button the audio stopped then click on the play button the audio will resumed where the audio stopped.     



- (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;
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    - (IBAction)play:(id)sender {

        [player play];

    }

    - (IBAction)pause:(id)sender {
        [player stop];
    }

答案 1 :(得分:0)

@ user3934210

在.h文件中获取数组并为Player

编写协议委托

“AVAudioPlayerDelegate&gt;” 中

NSMutableArray *songsArray;
<。>文件中的

Add all the .mp3 songs to the array in viewDidLoad

songsArray = [[NSMutableArray alloc]initWithObjects:@“A”,@“B”,@“C”,nil]; //replace your files

then take one Integer value to find the current song
    NSUInteger currentTrackNumber;

and set the currentTrackNumber=0 in viewDidLoad,

then copy the below code

- (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];
        }
    }
}