【问题标题】:AVPlayer got the metadata but not playingAVPlayer 得到了元数据但没有播放
【发布时间】:2013-05-08 12:30:00
【问题描述】:

我正在尝试做一个非常简单的应用程序,目的是收听音频流 (AAC 64 kbps)。为此,我使用来自Apple AVFoundationAVPlayer 如下:

ViewController.m

#import "ViewController.h"
@interface ViewController ()
@end

@implementation ViewController
@synthesize playerItem, player;

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"http://stream.myjungly.fr/MYJUNGLY2"]];
    [playerItem addObserver:self forKeyPath:@"timedMetadata" options:NSKeyValueObservingOptionNew context:nil];

    player = [AVPlayer playerWithPlayerItem:playerItem];
    [player play];

    NSLog(@"player item error : %@", playerItem.error.description);
    NSLog(@"player error : %@", player.error.description);
}

- (void) observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object
                     change:(NSDictionary*)change context:(void*)context {

    if ([keyPath isEqualToString:@"timedMetadata"])
    {
        AVPlayerItem* _playerItem = object;
        for (AVMetadataItem* metadata in _playerItem.timedMetadata)
        {
            NSLog(@"\nkey: %@\nkeySpace: %@\ncommonKey: %@\nvalue: %@", [metadata.key description], metadata.keySpace, metadata.commonKey, metadata.stringValue);
        }
    }
}

@end

我的对象playerplayerItemstrong 属性:

ViewController.h

@interface ViewController : UIViewController

@property (nonatomic, strong) AVPlayerItem* playerItem;
@property (nonatomic, strong) AVPlayer* player;

@end

Key Value Observer 运行良好,这是我的日志:

2013-05-14 11:18:03.725 MusicAvPlayer[6494:907] player item error : (null)
2013-05-14 11:18:03.728 MusicAvPlayer[6494:907] player error : (null)
2013-05-14 11:18:08.140 MusicAvPlayer[6494:907] 
key: title
keySpace: comn
commonKey: title
value: Alabama Shakes - Be Mine

但是没有播放音频,我已经没有声音了!知道为什么吗?

编辑:我已经看过这个问题了:

No sound coming from AVPlayer

AVAudioPlayer, No Sound

AVAudioPlayer not playing any sound

这就是我使用强属性的原因,所以我想我的问题与 ARC 无关

【问题讨论】:

  • 就像一个魅力!很棒的代码!

标签: objective-c avfoundation audio-streaming avplayer


【解决方案1】:
// Init PlayerItem
playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:@"http://stream.myjungly.fr/MYJUNGLY2"]];

// Init Player Obj
player = [AVPlayer playerWithPlayerItem:playerItem];
// Add objserver on Player
[player addObserver:self forKeyPath:@"status" options:0 context:nil];

在你的类中添加观察者方法

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                        change:(NSDictionary *)change context:(void *)context {
    if (object == player && [keyPath isEqualToString:@"status"]) {
        if (player.status == AVPlayerStatusReadyToPlay) {
            // Start playing...
            [player play];
        } else if (player.status == AVPlayerStatusFailed) {
            // something went wrong. player.error should contain some information
        }
    }
    if ([keyPath isEqualToString:@"timedMetadata"])
    {
        AVPlayerItem* _playerItem = object;
        for (AVMetadataItem* metadata in _playerItem.timedMetadata)
        {
            NSLog(@"\nkey: %@\nkeySpace: %@\ncommonKey: %@\nvalue: %@", [metadata.key description], metadata.keySpace, metadata.commonKey, metadata.stringValue);
        }
    }

}

【讨论】:

  • 谢谢你的回答,我试过了,观察者触发正常,但是我的播放状态等于AVPlayerStatusReadyToPlay,仍然没有声音......你还有其他想法吗?跨度>
  • 我已经在我的设备上播放过,可能是您通过编程或手动方式将声音静音,检查一下,它在我的设备上工作正常
  • 再次感谢您。我的声音没有手动静音,因为我可以在其他应用程序中收听声音。以编程方式这可能是可能的,但我的应用程序非常基本,我没有做更多的问题。有没有办法查看应用程序是否以编程方式静音?我的应用程序需要播放声音吗? (就像在 Android 中一样)。
  • 感谢您的宝贵时间和好主意,但我发现了问题:iphone 处于静音模式...所以没有声音可以发出。但是我现在有一个新问题:当手机处于静音模式时,如何在扬声器上播放声音? (如官方音乐应用程序)
  • 参考this链接你可能会得到答案
【解决方案2】:

我发现问题:iphone处于静音模式......所以扬声器上没有声音,我使用耳机时播放了声音。

但是我现在有一个新问题:当手机处于静音模式时,如何在扬声器上播放声音? (如官方音乐应用程序)

编辑:...答案就在那里: Play sound on iPhone even in silent mode

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多