音频文件无法在设备上运行,但它正在模拟器上运行 [英] Audio file not working on device but it is working on simulator

查看:178
本文介绍了音频文件无法在设备上运行,但它正在模拟器上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am playing audio in iphone app but problem is that it works fine on simulator but it is not working on device here is my code it just closes the app when this code runs.

         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

Here is the video code this also works on simulator but not on 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];


推荐答案

试试这段代码......

try this code...

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

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

first check the file is stored in your project folder or not and also that variable is not nil.

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

本教程怎么玩音频和-video-in-iphone

更新:

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

这篇关于音频文件无法在设备上运行,但它正在模拟器上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆