iphone - 音频文件无法在设备上运行,但可以在模拟器上运行

标签 iphone ipad audio video ios-simulator

我正在iphone应用程序中播放音频,但问题是它在模拟器上可以正常运行,但在设备上不起作用,这是我的代码,当该代码运行时它只是关闭了应用程序。

         NSString*thePath=[[NSBundle mainBundle] pathForResource:fileName ofType:@"MP3"];
    NSURL*url=[NSURL fileURLWithPath:thePath];



    //NSURL *url = [NSURL URLWithString:@"http://celeritas-solutions.com/pah_brd_v1/productivo/1.mp3"];
    mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    [[mp moviePlayer] prepareToPlay];
    [[mp moviePlayer] setUseApplicationAudioSession:NO];
    [[mp moviePlayer] setShouldAutoplay:YES];
    [[mp moviePlayer] setControlStyle:2];
    [[mp moviePlayer] setRepeatMode:MPMovieRepeatModeOne];
    [self presentMoviePlayerViewControllerAnimated:mp];

这是视频代码,它也可以在模拟器上使用,但不能在iPhone上使用
         NSString*thePath=[[NSBundle mainBundle] pathForResource:fileName ofType:@"MP4"];
    NSURL*url=[NSURL fileURLWithPath:thePath];
    //NSURL *url = [NSURL URLWithString:@"http://celeritas-solutions.com/pah_brd_v1/productivo/1.mp4"];
    mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    [[mp moviePlayer] prepareToPlay];
    [[mp moviePlayer] setUseApplicationAudioSession:NO];
    [[mp moviePlayer] setShouldAutoplay:YES];
    [[mp moviePlayer] setControlStyle:2];
    [[mp moviePlayer] setRepeatMode:MPMovieRepeatModeOne];
    [self presentMoviePlayerViewControllerAnimated:mp];

最佳答案

试试这个代码...

NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];

首先检查文件是否存储在项目文件夹中,并且该变量不是nil

同时查看此链接play-mp3-files-with-iphone-sdk

而本教程how-to-play-audio-and-video-in-iphone

更新:
MPMoviePlayerViewController *Mpplayer =[[MPMoviePlayerViewController alloc] initWithContentURL: [url standardizedURL]];
Mpplayer.moviePlayer.movieSourceType = MPMovieSourceTypeUnknown;  
[self presentModalViewController:Mpplayer animated:YES];
[[Mpplayer moviePlayer] play];

关于iphone - 音频文件无法在设备上运行,但可以在模拟器上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16033723/

相关文章:

iphone - 使用 XMLHTTPRequest 检查 WIFI 是否可用

ios - 用户界面图像 :imageNamed on ipad fails ok in simulator

java - 白噪声混合2音频字节数组

css - 使用带有 href 的 Div 标签作为可点击菜单元素的占位符

javascript - 在浏览器中保存 Blob 的持续时间

iphone - 适用于 iPhone/iPad 的 VPN 客户端

ios - 无法请求访问 Swift 4.2 中的 IOS 日历的权限

iphone - 播放前检测浏览器/设备是否可以内嵌播放 HTML5 视频

iphone - 是否可以直接在 google plus 上发布而无需在网络上添加其他共享对话框

iphone - iOS7 与 iPad 上 App 的兼容性