0

我需要在表格视图中播放我从json获取的音频文件(mp3) ,在单击行时,相应的歌曲路径正在传递给另一个 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);
}

其中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 文件路径为空时,我对其进行了评论。即使在最后,如果循环转义到else。我想可能有问题。

请帮帮我。

4

4 回答 4

2

这段代码对我有用

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

于 2013-07-23T06:06:50.620 回答
1

试试这个,

包含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];

}

希望这会有所帮助。

于 2013-07-23T10:09:22.350 回答
1

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

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

转到复制捆绑资源下的项目摘要和构建阶段选项卡。 在此处输入图像描述

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

于 2013-07-23T06:28:50.770 回答
0

您的网址看起来很奇怪:

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

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

于 2013-07-23T06:23:33.027 回答