簡體   English   中英

音頻文件未在iPhone / iOS中播放

[英]Audio file is not playing in iPhone/iOS

我需要播放一個音頻文件(mp3),該音頻文件是我在表視圖中從json獲取的,在單擊行時,相應的歌曲路徑將傳遞到另一個UIView ..在加載視圖時應播放。

我知道在互聯網上粘貼了很多類似的問題,我嘗試了許多對我無效的方法。

這是代碼片段。

- (void)viewDidLoad
{
     [super viewDidLoad];

     NSError *error = nil;
     [[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryAmbient error:&error];
//  audioFile--contains path-- 
//    NSString *filePath = [[NSBundle mainBundle] pathForResource:audioFile ofType:@"mp3"];
     NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSAllLibrariesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

     NSString *filePath = [documentsDirectory stringByAppendingPathComponent:audioFile];

     NSURL *url = [NSURL fileURLWithPath:filePath];

     player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
     if([self.player prepareToPlay])
     {
          NSLog(@"preparing");
     }
     else
     {           
          NSLog(@"some error");
     }
     if([self.player play])
     {
          NSLog(@"playing");
     }
     NSLog(@"not playing");
     NSLog(@"\n\nfile path-> %@\n\n url-> %@",filePath,url);
}

其中播放器AVAudioPlayer類的對象

   @property (strong, nonatomic) AVAudioPlayer *player;

從上面看,來自NSLog()的值是(對於特定行)

 file path-> /Users/ensignweb/Library/Application Support/iPhone Simulator/6.1/Applications/CE146654-3D1B-4F60-B37D-825267FD6EFB/Library/http:/www.fundamentalalvarado.com/assets/sermons/bible-doctrine-series/033113-pm-Doctrine of Creation vs The Lie of Evolution.mp3

 url-> file://localhost/Users/ensignweb/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/CE146654-3D1B-4F60-B37D-825267FD6EFB/Library/http:/www.fundamentalalvarado.com/assets/sermons/bible-doctrine-series/033113-pm-Doctrine%20of%20Creation%20vs%20The%20Lie%20of%20Evolution.mp3

當我使用NSBundle時,filePath為null,所以我評論了它。即使最后如果Loop轉義到其他位置,我想也可能是問題所在。

請幫幫我。

這段代碼對我有用

@interface PlayAudioVc : UIViewController<AVAudioPlayerDelegate>
{
//for playback section
    AVAudioPlayer *audioPlayer;
}

-(IBAction) playAudio{
        NSError *error;
        NSURL *url = [NSURL fileURLWithPath:self.audioName];
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        audioPlayer.delegate = self;
        if (error)
            NSLog(@"Error: %@", [error localizedDescription]);
        else
            [audioPlayer play];
}

self.audioName包含音頻文件的有效路徑

我在我的應用程序中執行此操作,它就像一個魅力

/**
 *  This method plays the 'pop' sound during the animation.
 */
-(void) playSound : (NSString *) fName : (NSString *) ext
{
    NSString *path  = [[NSBundle mainBundle] pathForResource : fName ofType :ext];
    if ([[NSFileManager defaultManager] fileExistsAtPath : path])
    {
        NSURL *pathURL = [NSURL fileURLWithPath : path];
        AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &audioEffect);
        AudioServicesPlaySystemSound(audioEffect);
    }
    else
    {
        NSLog(@"error, file not found: %@", path);
    }
}

您需要導入#import AVFoundation / AVFoundation.h和相應的框架。

轉到項目摘要並在其下的“構建階段”選項卡中復制包資源。 在此處輸入圖片說明

在副本捆綁資源下,檢查音頻文件是否存在。

試試這個

包括AVFoundtion.Framework並將其導入到AVFoundation / AVFoundation.h中,並導入到ViewController中,並在'.h'文件中添加AVAudioPlayerDelegate

然后為AVAudioPlayer類創建對象

@property (nonatomic, retain) AVAudioPlayer *audioPlayer;

NSString *path = [[NSBundle mainBundle]pathForResource:@"AIRTEL" ofType:@".MP3"];
NSURL *url = [NSURL fileURLWithPath:path];

NSError *error;
audioPlayer = [[AVAudioPlayer alloc]
               initWithContentsOfURL:url
               error:&error];
if (error)
{
    NSLog(@"Error in audioPlayer: %@",
          [error localizedDescription]);
} else {
    audioPlayer.delegate = self; 
    [audioPlayer prepareToPlay];
    [audioPlayer play];

}

希望這會有所幫助。

您的網址看起來很奇怪:

url-> file://localhost/Users/ensignweb/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/CE146654-3D1B-4F60-B37D-825267FD6EFB/Library/http:/www.fundam

它以file://開頭,並包含http:/

暫無
暫無

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

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