【问题标题】:Audio file not working on device but it is working on simulator音频文件无法在设备上运行,但在模拟器上运行
【发布时间】:2013-04-16 09:44:53
【问题描述】:

我正在 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];

【问题讨论】:

    标签: iphone ipad audio video ios-simulator


    【解决方案1】:

    首先添加 AVFoundation.framework 并添加以下代码。

    在.h文件中

    #import <AVFoundation/AVFoundation.h>
    

    和.m文件

     [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    

    【讨论】:

      【解决方案2】:

      试试这个代码...

      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];
      

      【讨论】:

      • @JdeveloperIphone 哦,好吧,伙计,然后看看这个链接.. stackoverflow.com/questions/2058067/…
      • @JdeveloperIphone 也可以查看本教程.. iphoneapp-dev.blogspot.in/2011/07/… 我希望这对你有帮助.. :)
      • 如果我使用任何服务器 url 文件,则相同的代码在设备上也可以正常工作,同样的问题也适用于视频
      • 不知道是什么原因不能在设备上玩它在模拟器上运行良好我已经尝试了所有
      • 嘿检查设备的互联网连接和一些设置..检查... bcoz它现在在我的项目中的工作性能..
      猜你喜欢
      • 2014-12-24
      • 2012-10-27
      • 2011-03-31
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多