2016-03-01 138 views
0

我在本地字典中有mp3文件。我试图使用AVPlayer播放这些文件,但它不起作用。在iOS 9中播放mp3文件不能使用AVPlayer

.h文件中:

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

@interface IndusHomePageViewController : UIViewController 
{ 
    AVPlayer *player; 
    NSTimer *playbackTimer; 
} 
-(void) setupAVPlayerForURL: (NSURL*) url; 
@end 

.m文件:

NSString *docDir1 =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *dataPathDestination = [docDir1 stringByAppendingPathComponent:@"mysore"]; 
NSString *myfilepath = [dataPathDestination stringByAppendingPathComponent:@"en_01_Mysore.mp3"]; 
NSURL *url = [NSURL URLWithString:myfilepath]; 
[self setupAVPlayerForURL:url]; 

-(void) setupAVPlayerForURL: (NSURL*) url 
{ 
    AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil]; 
    AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset]; 
    player = [AVPlayer playerWithPlayerItem:anItem]; 
    [player addObserver:self forKeyPath:@"status" options:0 context:nil]; 
} 

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{  
    if (object == player && [keyPath isEqualToString:@"status"]) { 
     if (player.status == AVPlayerStatusFailed) { 
      NSLog(@"AVPlayer Failed"); 
     } else if (player.status == AVPlayerStatusReadyToPlay) { 
      NSLog(@"AVPlayer Ready to Play"); 
     } else if (player.status == AVPlayerItemStatusUnknown) { 
      NSLog(@"AVPlayer Unknown"); 
     } 
    } 
} 

- (IBAction)play:(id)sender { 
    [player play]; 
} 

- (IBAction)pause:(id)sender { 
    [player pause]; 
} 
+0

第一行或你的.m片段必须从方法中调用,所以我的猜测是你没有发布任何东西。你面对的错误是什么? –

+0

我没有得到这些错误。当我按下播放按钮不工作 –

回答

3

为了创建NSURL你必须使用fileURLWithPath:方法的本地存储的文件,目前你试图将url初始化为远程文件。将NSURL *url = [NSURL URLWithString:myfilepath];替换为NSURL *url = [NSURL fileURLWithPath:myfilepath];,它应该可以工作。祝你好运!

PS:另外,您最好在将URL传递给播放器之前进行一些文件存在检查。

+0

itz工作谢谢 –

0

正在使用下面的代码来流式传输歌曲。请使用本地文件路径代替audioURL。请试试这个。它为我工作。

NSURL *url = [NSURL URLWithString:[audioURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]; 
AVURLAsset *asset = [AVURLAsset assetWithURL: url]; 
// self.aduioItem is an obj of AVPlayerItem 
self.audioItem = [AVPlayerItem playerItemWithAsset: asset]; 
// self.audioPlayer is an obj of AVPlayer 
self.audioPlayer = [[AVPlayer alloc] init]; 
[self.audioPlayer addObserver:self forKeyPath:@"status" options:0 context:nil]; 
AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem: self.audioItem]; self.audioPlayer = player; 
//Calling a methods to handle the current song completion.. 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:[self.audioPlayer currentItem]]; 
//Calling a method to update the slider timer.. 
self.songTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updatePlayerTime:) userInfo:nil repeats:YES]; [self.audioPlayer play]; 

希望它能帮助你。谢谢。