【问题标题】:How to create a audioplayer application like pandora app?如何创建像潘多拉应用程序这样的音频播放器应用程序?
【发布时间】:2011-04-28 08:07:39
【问题描述】:

我创建了一个从本地播放多个音频文件的应用程序。音频文件很长。音频播放器为用户提供以下选项

  • 转发,
  • 倒带,
  • 下一个曲目,
  • 上一首曲目,

我打算使用 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

【问题讨论】:

  • 没有任何代码和我的水晶球正在维修,这将是艰难的。请发布一些相关代码。
  • 感谢罗宾的回复。我已经上传了代码。

标签: iphone memory-management avaudioplayer


【解决方案1】:

你的问题在这里:

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

您在调用 release 之前将 audioPlay 设置为 nil,这意味着释放消息将被发送到 nil。您需要颠倒这两行的顺序。

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

【讨论】:

    猜你喜欢
    • 2017-01-12
    • 2020-01-10
    • 2019-01-16
    • 2020-08-26
    • 2015-06-11
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多