2011-04-28 64 views
2

我创建了一个从本地播放多个音频文件的应用程序。音频文件很长。音频播放器具有用户如何创建像pandora应用程序一样的音频应用程序?

  • 前进,
  • 倒带,
  • 下一个曲目,
  • 上一曲下列选项中,

我打算使用AvAudioPlayer,这样我可以玩长时间的音频。当我更改音频文件即按下一个音轨音频。 audioplayer实例没有被释放。此问题仅出现一些时间。请帮帮我..!!我帮少..

下一曲按钮IBAction为法

- (IBAction) nextTrackPressed 
{ 
    [audioPlay stopAudio]; 
    if (audioPlay) { 
     audioPlay = nil; 
     [audioPlay release]; 
    } 

    appDelegate.trackSelected += 1; 
    [self intiNewAudioFile]; 
    [self play]; 
} 

Initializing audio file through below method 

-(void) intiNewAudioFile 
{ 
    NSAutoreleasePool *subPool = [[NSAutoreleasePool alloc] init]; 

    NSString *filePath = [[NSString alloc] init]; 

    trackObject = [appDelegate.trackDetailArray objectAtIndex:appDelegate.trackSelected]; 
    NSLog(@"%@",trackObject.trackName); 
    // Get the file path to the song to play. 
    filePath = [[NSBundle mainBundle] pathForResource:trackObject.trackName ofType:@"mp3"]; 

    // Convert the file path to a URL. 
    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath]; 

    if (audioPlay) { 
     audioPlay = nil; 
     [audioPlay release]; 
    } 

    audioPlay = [[AudioPlayerClass alloc] init]; 
    [audioPlay initAudioWithUrl:fileURL]; 

    [filePath release]; 
    [fileURL release]; 
    [subPool release]; 
} 

AudioPlayerClass实施

#import "AudioPlayerClass.h" 


@implementation AudioPlayerClass 

- (void) initAudioWithUrl: (NSURL *) url 
{ 

    curAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; 
    [curAudioPlayer setDelegate:self]; 
    [curAudioPlayer prepareToPlay]; 
} 

- (void) playAudio 
{ 
    [curAudioPlayer play]; 
} 

- (void) pauseAudio 
{ 
    [curAudioPlayer pause]; 
} 

- (void) stopAudio 
{ 
    [curAudioPlayer stop]; 
} 

- (BOOL) isAudioPlaying 
{ 
    return curAudioPlayer.playing; 
} 

- (void) setAudiowithCurrentTime:(NSInteger) time 
{ 
    curAudioPlayer.currentTime = time; 
} 

- (NSInteger) getAudioFileDuration 
{ 
    return curAudioPlayer.duration; 
} 

- (NSInteger) getAudioCurrentTime 
{ 
    return curAudioPlayer.currentTime; 
} 

- (void) releasePlayer 
{ 
    [curAudioPlayer release]; 
} 

- (void)dealloc { 
    [curAudioPlayer release]; 
    [super dealloc]; 
} 

@end 
+0

没有任何代码和我的cristal球在修复这将是艰难的。请张贴一些相关的代码。 – Robin 2011-04-28 08:18:19

+0

感谢罗宾的回复。我已经上传了代码。 – user728823 2011-04-28 10:06:12

回答

0

你的问题是在这里:

if (audioPlay) { 
     audioPlay = nil; 
     [audioPlay release]; 
    } 

之前要设置audioPlay为零调用释放,这意味着释放消息发送到零。你需要颠倒这两行的顺序。

if (audioPlay) { 
     [audioPlay release]; 
     audioPlay = nil; 
    } 
相关问题