2014-11-06 91 views
1

我想創建一個應用程序,將播放一些音頻文件。我希望能夠從網絡服務器傳輸音頻,但到目前爲止,我不知道如何鏈接到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 
+2

你應該暫停,而不是回採,'[播放暫停]'那麼你必須再次發揮,以恢復它。 – zaheer 2014-11-06 04:32:40

回答

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]; 
    } 
+0

謝謝蘇雷什 - 解決了我的問題!如果我想播放超過1個音頻文件,是否有任何指導可以給我?我想爲我的項目添加更多的音頻文件。 – user3934210 2014-11-06 09:56:06

0

@ user3934210

採取在h文件一個陣列和寫協議委託播放

「AVAudioPlayerDelegate」「

NSMutableArray *songsArray; 

在.m文件

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]; 
     } 
    } 
} 
+0

所有這些代碼都放在.h文件中嗎?對不起,由於您已在代碼框中以正常文本添加,因此很難閱讀。感謝您再次幫忙。 – user3934210 2014-11-06 12:07:01

+0

檢查上面的代碼 – 2014-11-06 12:09:04

+0

感謝澄清。 – user3934210 2014-11-06 12:29:13