我在本地字典中有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];
}
第一行或你的.m片段必须从方法中调用,所以我的猜测是你没有发布任何东西。你面对的错误是什么? –
我没有得到这些错误。当我按下播放按钮不工作 –