在iOS 9中播放mp3文件无法使用AVPlayer

时间:2016-03-01 10:26:22

标签: ios objective-c mp3 avplayer

我在本地词典中有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];
}

2 个答案:

答案 0 :(得分:3)

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

PS:此外,在将网址传递给播放器之前,最好先进行一些文件存在检查。

答案 1 :(得分: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];

希望它会对你有所帮助。感谢。