如何创建像 pandora 应用程序这样的音频播放器应用程序?

发布于 2024-11-04 06:49:28 字数 2237 浏览 1 评论 0原文

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

  • 前进、
  • 后退、
  • 下一曲目、
  • 上一曲目,

我计划使用 AvAudioPlayer,以便我可以长时间播放音频。当我更改音频文件时,即按下一曲目音频。音频播放器实例没有被释放。此问题仅出现几次。请帮我..!!我无能为力..

下一曲目按钮 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

I am created an application which play multiple audio files from locally. Audio files are long. Audio player has following options for the user

  • Forward,
  • Rewind,
  • Next Track,
  • Previous Track,

I am planning to use AvAudioPlayer so that i can play an audio with long time. When i am changing the audio file ie pressing next track audio. The audioplayer instance is not getting released. This problem is appearing some times only. Please help me..!! I am help less..

Next Track Button IBAction Method

- (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 Implementation

#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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耶耶耶 2024-11-11 06:49:28

你的问题在这里:

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

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

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

Your problem is here:

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

You are setting audioPlay to nil before calling release which means that the release message is getting sent to nil. You need to reverse the order of these 2 lines.

if (audioPlay) {
        [audioPlay release];
         audioPlay = nil;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文