簡體   English   中英

如何創建像潘多拉應用程序這樣的音頻播放器應用程序?

[英]How to create a audioplayer application like pandora app?

我創建了一個從本地播放多個音頻文件的應用程序。 音頻文件很長。 音頻播放器為用戶提供以下選項

  • 向前,
  • 倒帶,
  • 下一首曲目,
  • 上一首曲目,

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

你的問題在這里:

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

您在調用 release 之前將 audioPlay 設置為 nil,這意味着發布消息將被發送到 nil。 您需要顛倒這兩行的順序。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM