AVPlayer无法播放音频-iOS 9,目标-C [英] AVPlayer not playing audio - iOS 9, Objective - C

查看:199
本文介绍了AVPlayer无法播放音频-iOS 9,目标-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从应用程序中的URL播放音频.一切都按预期在iOS 8中发生(模拟器和物理设备).对于iOS 9,它可以在模拟器中运行,但是在设备上,音频根本无法播放.出现流式传输,如果我单击播放,则进度栏还会显示正在加载和播放音频,但声音没有出现.此问题发生在iOS 9物理设备中.它与iOS 8物理设备完全兼容.

I am trying to play audio from a URL in my app. Everything happens as expected in iOS 8 (simulator and physical devices). For iOS 9, it works in simulator but on device, the audio simply does not play. The streaming appears, if I click on play, the progress bar also shows that audio is getting loaded and playing but the sound does not come. This issue is happening in iOS 9 physical device. It works totally fine with iOS 8 physical device.

我创建了一个简单的按钮.单击按钮后,将加载AVPlayerViewController并使用给定的URL初始化AVPlayer.代码如下:

I created a simple button. On clicking on button, the AVPlayerViewController is loaded and AVPlayer is initialized with the given URL. The code is following:

#import "ViewController.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)playAudio:(id)sender {
    NSURL *url = [NSURL URLWithString:@"https://path/to/my.mp3"];
    [self performSegueWithIdentifier:@"playSegue" sender:url];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    AVPlayerViewController *viewController = (AVPlayerViewController *)segue.destinationViewController;
    viewController.player = [AVPlayer playerWithURL:(NSURL *)sender];
}

@end

推荐答案

经过5天以上的反复试验和尝试,我终于发现我的问题出在手机的静音开关上.静音时,音频不会播放!因此,我添加了以下代码行以将我的类别指定为播放类别,现在即使我的设备开关处于静音状态也可以播放

After doing various trial and error tactics for over 5 days, I finally figured out that my problem was with the mute switch of the phone. When it is on mute, the audio does't play! So, I added the following line of code to specify my category as playback and now it plays even when my device switch is on mute

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

谢谢!

这篇关于AVPlayer无法播放音频-iOS 9,目标-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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