【问题标题】:Audio file is not playing in iPhone/iOS音频文件未在 iPhone/iOS 中播放
【发布时间】:2013-07-23 05:59:49
【问题描述】:

我需要在表格视图中播放我从json 获取的音频文件(mp3),单击行时,相应的歌曲路径正在传递到另一个 UIView..在加载视图时它应该播放。

我知道有很多类似的问题粘贴在互联网上,我尝试了很多对我没有用的方法。

这里是代码sn-p。

- (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);
}

其中 playerAVAudioPlayer 类的对象

   @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 时,文件路径为空,所以我评论了它。即使在最后 If Loop 转义到 else。我想可能是问题所在。

请帮帮我。

【问题讨论】:

标签: iphone ios json avaudioplayer


【解决方案1】:

这段代码对我有用

@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 包含指向音频文件的有效路径

【讨论】:

  • 错误:操作无法完成。 (OSStatus错误-43。),这个错误来了
  • 问题可能出在音频文件路径上。记录路径并检查它
  • 这是音频文件路径之一,存储在audioName中,www.fundamentalalvarado.com/assets/sermons/bible-doctrine-series/052613-pm-The Doctrine of Salvation.mp3,它正在浏览器中播放,但在模拟器中运行时 OSStatus 错误即将到来。
  • 好吧,再次检查路径,问题出现在我的 url 路径中。如果 url 正确,它必须工作
  • 我尝试通过在本地保存另一个 mp3 文件,然后它也无法播放..它是否在模拟器上运行..我是新手..所以无法找到在哪里问题..
【解决方案2】:

我在我的应用程序中这样做,它就像一个魅力

/**
 *  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 和相应的框架。

转到复制捆绑资源下的项目摘要和构建阶段选项卡。

在复制包资源下检查音频文件是否存在。

【讨论】:

  • 检查资源文件是否在你的资源包中?
  • 当我尝试使用本地保存的 mp3 文件时,它可以工作并且文件存在于捆绑资源中,但该路径不存在......
  • 假设这是网址-> fundamentalalvarado.com/assets/sermons/bible-doctrine-series/… 宽恕罪的教义.mp3,这将如何播放
  • 使用上面的函数是不会播放的,你应该下载文件并尝试播放或尝试AVAudioPlayer initWithContentsOfURL来实现。
  • 在 web 视图中它正在完美打开..但在 AVAudioPlayer 中相同...- (void)viewDidLoad { [super viewDidLoad]; audioFile = [audioFile stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; // [audWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:audioFile]]]; NSURL *url=[NSURL URLWithString:strAudFile]; audPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil]; if([audPlayer play]) { NSLog(@"ok"); } NSLog(@"没有播放"); } ....“未播放”
【解决方案3】:

试试这个,

包含 AVFoundtion.Framework 并将 AVFoundation/AVFoundation.h 导入 ViewController 并在 '.h' 文件中添加 AVAudioPlayerDelegate。 p>

然后为 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];

}

希望这会有所帮助。

【讨论】:

    【解决方案4】:

    您的网址看起来很奇怪:

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

    它以 file:// 开头并包含 http://

    【讨论】:

    • 在audioFile中它只有以http://开头的url路径,我用audioFile试过,OSStatus错误-43,错误来了。然后我尝试使用 NSBundle,它给出了 null 值,所以我尝试了 NSearchPath...**,希望我可以获得可能存储 json 路径的路径并在本地播放它。我是新手,所以我不不知道我错在哪里。
    • 既然要远程播放mp3,就试试[NSURL URLWithString:string_with_url]
    • 我尝试了单独的 URL,NSURL *url = [NSURL URLWithString:@"fundamentalalvarado.com/assets/sermons/bible-doctrine-series/… Doctrine of the Forgiveness of Sin.mp3"];,url 包含空值。
    • 您的路径包含空格,因此为空值
    • 我不清楚你保存 mp3 的位置...你是否将它复制到你的应用程序中,路径与你的 json 提供的路径相同?然后我将只使用 json 中的文件名,去掉其中的空格并将名称中没有空格的 mp3 副本放入您的应用程序
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多