但不在真实设备上播放

但不在真实设备上播放

本文介绍了AVPlayer在模拟器上播放,但不在真实设备上播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个基本的音频播放器,以便播放远程音频文件.文件格式为mp3.我编写的代码在模拟器上可以正常工作,但不能在真实设备上运行.但是,如果我使用safari(在同一台真实设备上)加载它,则我在应用程序中使用的相同网址就可以正常工作,因此我并没有真正注意到这一点.下面是我的代码:

I'm implementing a basic audio player in order to play remote audio files. Files are in format mp3.The code I wrote is working fine on the simulator but doesn't work on a real device. However the same url I use within my app works fine if I load it by using safari (on the same real device) so I'm not really getting the missing point.Below is my code:

self.musicPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:urlTrack]];
[self.musicPlayer play];

非常容易的事情.音乐播放器属性定义为

something extremely easy. The music player property is defined as

@property (nonatomic, retain) AVPlayer *musicPlayer;

我也尝试使用AVPlayerItem,但结果是相同的.这是我使用的代码

I also tried using an AVPlayerItem but the result is the same. Here is the code I have used

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:urlTrack]];
self.musicPlayer = [AVPlayer playerWithPlayerItem:playerItem];
[self.musicPlayer play];

最后我尝试使用下面的代码

Finally I tried to use the code below

self.musicPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:urlTrack]];
NSLog(@"Player created:%d",self.musicPlayer.status);
[self.musicPlayer addObserver:self forKeyPath:@"status" options:0 context:nil];


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    NSLog(@"Player created:%d",self.musicPlayer.status);

    if (object == self.musicPlayer && [keyPath isEqualToString:@"status"]) {
        if (self.musicPlayer.status == AVPlayerStatusReadyToPlay) {
            [self.musicPlayer play];
        } else if (self.musicPlayer.status == AVPlayerStatusFailed) {
            // something went wrong
        }
    }
}

当调用方法observationValueForKeyPath时,播放器状态为1,播放被执行,但仍然没有声音.我尝试了几个文件,例如:

When the method observeValueForKeyPath is invoked the player status is 1 and the play is exectuted but still not sound.I tried several files like:

http://www.nimh.nih.gov/audio/neurogenesis.mp3

http://www.robtowns.com/music/blind_willie.mp3

有什么主意吗?Tnx

Any idea?Tnx

推荐答案

检查文件名的拼写.该设备区分大小写,模拟器不...

Check the spelling of your filename. The device is case-sensitive, the simulator is not...

另外,请检查铃声是否关闭,关闭时您将听不到任何声音.为防止这种情况,请使用

Also, check if your ringer is off, you won't hear any sound when it's off. To prevent that, use

NSError *_error = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &_error];

就在您初始化播放器之前

right before where you init the player

这篇关于AVPlayer在模拟器上播放,但不在真实设备上播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 15:09