Patrice Cote
Patrice Cote

Reputation: 3716

Audio stream with AVPlayer

There's a lot of streaming apps out there for iOS. They all use a player, which I assume is the AVPlayer. Yet it seems impossible to find a decent documentation with sample code that works ! I'm pretty sure it's nothing but a few line of codes, but I just can't figure out what's wrong...

I have an EXC_BAD_ACCESS error when trying to call the "play" method. But the url is good and there is an instance of the player.

- (void)viewDidLoad {
    [super viewDidLoad];

// Load the array with the sample file
NSString *urlAddress = @"http://mystreamadress.mp3";

//Create a URL object.
urlStream = [NSURL URLWithString:urlAddress];   
self.player = [AVPlayer playerWithURL:urlStream];   

[urlAddress release];
}

The urlStream is a property with retain attribute. Then I have an IBAction that fires when the button is clicked and that tries to play it, and that's where it crashes.

- (IBAction)playButtonPressed
{       
    [player play];  
}

Can my problem be because I'm trying to play MP3 or what ? The real url adress I'm using works fine when I use a webview to load it.

If anyone could point me to a good sample (not the AVFoundation ou AVPlayer docs from Apple nor the AVTouchController project) it would be realy appreciated.

Thanks !

Upvotes: 6

Views: 9784

Answers (2)

c.bakiyalakshmi
c.bakiyalakshmi

Reputation: 51

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:fileURL];
//AVPlayer *avPlayer = [[AVPlayer playerWithURL:[NSURL URLWithString:url]] retain];
avPlayer = [[AVPlayer playerWithPlayerItem:playerItem] retain];
//AVPlayerLayer *avPlayerLayer = [[AVPlayerLayer playerLayerWithPlayer:avPlayer] retain];
[avPlayer play];
avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; 

I use it this way to play mp3, but it doesn't support stop.

Upvotes: 0

Walter
Walter

Reputation: 5887

urlAddress release is causing the issue I think.

You didn't create your NSString with alloc, init so by releasing it you are overreleasing it and getting the EXC_BAD_ACCESS.

Unless you explicitly create an NSString with alloc and init then the convenience methods of creating the string are autoreleased.

Upvotes: 5

Related Questions