简体   繁体   中英

AVPlayer doesn't play sound on iOS Simulator

I want to play music from URL with an AVPlayer on the iOS Simulator, but it doesn't work :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    Track *selectedTrack = self.tracksByAlbum[indexPath.row];

    NSLog(selectedTrack.preview);

    NSURL *url = [NSURL URLWithString:[selectedTrack.preview stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    AVPlayer *player = [[AVPlayer alloc] initWithURL:url];

    if (!player) {
        NSLog(@"Error");
    }

    [player play];
}

The url is :

http://cdn-preview-6.deezer.com/stream/6d180c6b61078f437b04ec21bbe0fb76-3.mp3

I read that I have to enable user interface sounds effects on OS X, but it doesn't change anything...

If you're using ARC, you'll need to retain the AVAudioPlayer as it sets to nil automatically, which would be a likely reason for no sound. In your header file if you could make sure your self.player's property is set up like this,

@property (strong, nonatomic) AVPlayer *player;

from within your didSelectRowAtIndexPath change the AVPlayer code to this

self.player = [[AVPlayer alloc] initWithURL:url];

if (!self.player) {
    NSLog(@"Error");
}

[self.player play];

I hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM