3

I'm trying to do a very simple application, the purpose is listening an audio stream (AAC 64 kbps). To do so I'm using AVPlayer from the Apple AVFoundation has follow :

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

My object player and playerItem are strong properties :

ViewController.h

@interface ViewController : UIViewController

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

@end

The Key Value Observer is working great, here is my log :

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

But the audio is not played, I've go no sound ! Any idea why ?

EDIT: I already look at this questions :

No sound coming from AVPlayer

AVAudioPlayer, No Sound

AVAudioPlayer not playing any sound

That's why I'm using a strong property, so I guess my problem is not ARC related

Community
  • 1
  • 1
Thomas Besnehard
  • 2,106
  • 3
  • 25
  • 47

2 Answers2

5

I found problem : the iphone was in silent mode ... so no sound can go out on the speaker, the the sound was played when I was using the head phone.

But I've got a new question now : how can you play sound on the speaker when the phone is in silent mode ? (like the official Music application)

EDIT : ... and the answer is there : Play sound on iPhone even in silent mode

Community
  • 1
  • 1
Thomas Besnehard
  • 2,106
  • 3
  • 25
  • 47
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];

Add Observer Method your Class

- (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);
        }
    }

}
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
  • Thank you for your answer, I've tried it, the observer is trigger properly, but my play status equal `AVPlayerStatusReadyToPlay` and still no sound ... dou you have an other idea ? – Thomas Besnehard May 14 '13 at 16:56
  • i have played it in my device, may be sound is muted by you either programatically or manually, check that, its working fine with my device – Dipen Panchasara May 15 '13 at 04:22
  • thank you again. My sound is not muted manually because I can listen to sound in other application. Programmatically it might be possible, but my application is very basic, and I'm not doing more that what is in the question. Is there a way to see if the application is programmatically muted ? Is there any autorisation that my application need to have to play sound ? (Like in Android). – Thomas Besnehard May 16 '13 at 07:30
  • Thank you for your time and you good idea but I found problem : the iphone was in silent mode ... so no sound can go out. But I've got a new question now : how can you play sound on the speaker when the phone is in silent mode ? (like the official Music application) – Thomas Besnehard May 17 '13 at 11:58
  • refer [this](http://stackoverflow.com/questions/9508039/ios-5-no-audio-when-playing-a-video-with-silent-mode-on-with-a-buzztouch-app) link you may get your answer – Dipen Panchasara May 17 '13 at 12:04