3

我正在尝试做一个非常简单的应用程序,目的是收听音频流(AAC 64 kbps)。为此,我使用AVPlayerApple AVFoundation以下方法:

视图控制器.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

我的对象player强大的playerItem属性:

视图控制器.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

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

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

AVPlayer 没有声音

AVAudioPlayer,没有声音

AVAudioPlayer 不播放任何声音

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

4

2 回答 2

5

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

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

编辑:......答案就在那里: 即使在静音模式下也能在 iPhone 上播放声音

于 2013-05-17T11:59:17.447 回答
0
// 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);
        }
    }

}
于 2013-05-14T11:51:08.230 回答