2017-05-25 9 views
-1

私はサーバのURLからオーディオを再生しようとしますが、再生はしませんが、ドキュメントディレクトリのURLからオーディオを再生しようとしています。iosのサーバURLから.cafオーディオファイルを再生する方法

NSData *songData=[NSData dataWithContentsOfURL:[NSURL URLWithString: 
[NSString stringWithFormat:@"%@",aSongURL]]]; 
AVAudioPlayer *abc = [[AVAudioPlayer alloc] initWithData:songData error:nil]; 
abc.numberOfLoops=0; 
[abc prepareToPlay]; 
[abc play]; 
+0

あなたのURL –

+0

を印刷することができます。これは、URLはhttps://dev.epixelsoft .co/love_app/audio/img_14957068361.caf – iOS

答えて

1

これを試すことができます。それが役に立てば幸い。

AVPlayer *objAVPlayer = [[AVPlayer alloc] initWithURL:url]; 
[objAVPlayer play]; 
+0

このコードを使用していますが、何も起こりませんでした – iOS

+0

@Inderpalsingh NSDataインスタンスが実際に作成されているかどうかを確認できます。作成されている場合は、エラーパラメータにNSError型のポインタを渡してください。 initWithDataの後:エラー:NSErrorがポインタがnilでない場合は、エラーが何であるかをチェックアウトします。 –

0

私の答えは

を下回っているViewController.h

#import <UIKit/UIKit.h> 
#import <AVFoundation/AVFoundation.h> 

@interface ViewController : UIViewController 
@property (nonatomic,strong)AVAudioPlayer *player; 
- (IBAction)actionPlayAudio:(id)sender; 
@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 
@end 
@implementation ViewController 
@synthesize player; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 
- (IBAction)actionPlayAudio:(id)sender { 
    NSString *strAudioFileURL = @"dev.epixelsoft.co/love_app/audio/img_14957068361.caf"; 
    NSURL *soundFileURL = [NSURL fileURLWithPath:strAudioFileURL]; 
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil]; 
    [player play]; 
} 
関連する問題