简体   繁体   English

IOS iPhone使用MPMusicPlayerController播放外部音乐文件,并显示iPod界面

[英]IOS iPhone use MPMusicPlayerController to play external music file, and display iPod interface

I'm trying to use the standardized "iPod" audio player to play some MP3 tracks in an iPhone app I'm building. 我正在尝试使用标准化的“iPod”音频播放器在我正在构建的iPhone应用程序中播放一些MP3音轨。 The tracks are downloaded from the internet and stored in the app's "Documents" directory. 曲目从互联网下载并存储在应用程序的“文档”目录中。 I thought of using a MPMusicPlayerController to do this, but I don't seem to be able to get it to work. 我想过使用MPMusicPlayerController来做这件事,但我似乎无法让它工作。 Also, I've seen the AVAudioPlayer, but that just plays the audio without an interface. 此外,我已经看过AVAudioPlayer,但只播放没有接口的音频。 Any suggestions? 有什么建议么?

The MPMusicPlayerController is for playing items out of the iPod library (songs sync'd via iTunes) so you won't be able to use it for this. MPMusicPlayerController用于播放iPod库中的项目(通过iTunes同步的歌曲),因此您将无法使用它。

You can get the NSData for your audio using... 您可以使用...获取音频的NSData ...

NSData* data = [NSMutableData dataWithContentsOfFile:resourcePath options:0 error:&err];

Then use an AVAudioPlayer created from that data and call play. 然后使用从该数据创建的AVAudioPlayer并调用play。

AVAudioPlayer* player = [[AVAudioPlayer alloc] initWithData:data error:&err];
[player play];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM