简体   繁体   English

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

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

I need to play a audio file(mp3) that i am fetching from json in a table view, on clicking row respective song path is passing to another UIView..on loading view it should play. 我需要播放一个音频文件(mp3),该音频文件是我在表视图中从json获取的,在单击行时,相应的歌曲路径将传递到另一个UIView ..在加载视图时应播放。

I know there is a lot of similar problem pasted over internet and i tried many ways nothing worked for me. 我知道在互联网上粘贴了很多类似的问题,我尝试了许多对我无效的方法。

Here is the code snippet. 这是代码片段。

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

Where player is an object of AVAudioPlayer class 其中播放器AVAudioPlayer类的对象

   @property (strong, nonatomic) AVAudioPlayer *player;

From above, Values coming from NSLog() are(for a particular row) 从上面看,来自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

When i used NSBundle filePath was null so i commented it.Even at the end If Loop escaping to else .I guess there could be the problem. 当我使用NSBundle时,filePath为null,所以我评论了它。即使最后如果Loop转义到其他位置,我想也可能是问题所在。

Please help me out. 请帮帮我。

This code worked for me 这段代码对我有用

@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 is containing a valid path to audio file self.audioName包含音频文件的有效路径

I do this in my app and it works like a charm 我在我的应用程序中执行此操作,它就像一个魅力

/**
 *  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);
    }
}

You need to import #import AVFoundation/AVFoundation.h and the respective framework. 您需要导入#import AVFoundation / AVFoundation.h和相应的框架。

Go to project summary and build phases tab under that go to copy bundle resources. 转到项目摘要并在其下的“构建阶段”选项卡中复制包资源。 在此处输入图片说明

Under copy bundle resources check whether the audio file exists. 在副本捆绑资源下,检查音频文件是否存在。

Try this one, 试试这个

Include AVFoundtion.Framework and import into AVFoundation/AVFoundation.h into ViewController and Add AVAudioPlayerDelegate in '.h' file. 包括AVFoundtion.Framework并将其导入到AVFoundation / AVFoundation.h中,并导入到ViewController中,并在'.h'文件中添加AVAudioPlayerDelegate

Then create object for AVAudioPlayer class 然后为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];

}

Hope this would help. 希望这会有所帮助。

Your URL looks strange: 您的网址看起来很奇怪:

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

it starts with file:// and contains http:/ 它以file://开头,并包含http:/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM