2016-03-01 46 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]; 

希望它能幫助你。謝謝。