我在本地字典中有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片段必須從方法中調用,所以我的猜測是你沒有發佈任何東西。你面對的錯誤是什麼? –
我沒有得到這些錯誤。當我按下播放按鈕不工作 –