iphone - MPMusicPlayerController 并播放捆绑的歌曲

标签 iphone cocoa-touch iphone-sdk-3.0

如何使用 MPMusicPlayerController 播放与应用程序捆绑的歌曲文件?

我尝试过的代码,

NSString *url = [[NSBundle mainBundle] pathForResource:@"song1" ofType:@"mp3"];
MPMusicPlayerController *myMusicPlayer=[[MPMusicPlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
[myMusicPlayer play];

最佳答案

MPMusicPlayer Controller 仅用于播放用户 iPod 库中的项目。 AVAudioPlayer 将从应用程序的沙箱中播放编码文件,您应该能够轻松地转换此代码以使用 AVAudioPlayer...看起来像:

NSString *url = [[NSBundle mainBundle] pathForResource:@"song1" ofType:@"mp3"]; 
AVAudioPlayer *myMusicPlayer=[[AVAudioPlayer alloc]
        initWithContentsOfURL:[NSURL fileURLWithPath:url]
        error:nil];
[myMusicPlayer play];

关于iphone - MPMusicPlayerController 并播放捆绑的歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1636141/

相关文章:

iphone - iPhone 中我的目录路径中的文件夹名称

ios - 获取从当前位置到达某个地方的时间

iOS 自动布局 : Set the trailing space equal to the width of the superview

iphone - ios 我如何比较 nsmutablearray 中的 nsdictionary

iphone - 在另一个 subview 之上检测一个 subview

ios - 原型(prototype)单元格内带有标签的标签未更新

iphone - 可以将下载的 mp3 从应用程序移动到 iPod 吗?

objective-c - 带有动态字符串的 UIImage 错误

iphone - 我应该运行 NSTimer 的最快速度是多少?

iphone - 如何隐藏导航栏Rightbarbuttonitem?