2012-06-19 37 views
5

Tôi đang cố phát một bài hát từ thư viện nhạc iPhone của mình bằng AVPlayer. Mọi thứ dường như đã sẵn sàng để chơi, nhưng người chơi chỉ đơn giản là sẽ không tạo ra âm thanh nào. Tôi đã đấu tranh với điều này trong một thời gian, bất kỳ trợ giúp sẽ được đánh giá rất nhiều!AVPlayer không phát từ thư viện nhạc

Lưu ý: Tôi nhận ra mình có thể sử dụng AVAudioPlayer, nhưng tôi muốn đọc tệp ngay từ thư viện nhạc của tôi và theo hiểu biết của tôi, AVAudioPlayer không hỗ trợ (tôi sẽ phải xuất bài hát đầu tiên, thêm thời gian). Tôi không thể sử dụng MPMusicPlayerController vì mục tiêu cuối cùng là chuyển bài hát thành NSData và chơi nó trên một thiết bị khác.

Trên tất cả, tôi muốn biết tại sao mã này không được chơi:

NSArray *itemsFromQuery = [[MPMediaQuery songsQuery] items]; 
MPMediaItem *song = [itemsFromQuery objectAtIndex:29]; 
NSURL *songURL = [song valueForProperty:MPMediaItemPropertyAssetURL]; 
AVURLAsset *urlAsset = [[AVURLAsset alloc] initWithURL:songURL options:nil]; 

NSArray *keyArray = [[NSArray alloc] initWithObjects:@"tracks", nil]; 

[urlAsset loadValuesAsynchronouslyForKeys:keyArray completionHandler:^{ 

    AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:urlAsset]; 

    AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem]; 

    while (true) { 
     if (player.status == AVPlayerStatusReadyToPlay && playerItem.status == AVPlayerItemStatusReadyToPlay) { 
      break; 
     } 
    } 

    if (player.status == AVPlayerStatusReadyToPlay && playerItem.status == AVPlayerItemStatusReadyToPlay) { 
     NSLog(@"Ready to play"); 
     [player play]; 
    } 
    else 
     NSLog(@"Not ready to play"); 
}]; 

Đầu ra là "Sẵn sàng để chơi", và "tốc độ" của tài AVPlayer là 1.0 sau khi tôi gọi phương pháp chơi. MPMediaItem tồn tại và tôi có thể sử dụng phương thức valueForProperty để có được tiêu đề, nghệ sĩ chính xác, v.v. Bất kỳ ý tưởng nào tại sao không có âm thanh phát ra từ trình phát?

+1

bạn đã thử khai báo AVPlayer và AVPlayerItem ngoài khối, như ngay sau 'NSArray * keyArray'? – meggar

+0

Tôi đã làm. Khai báo và khởi tạo chúng bên ngoài không tạo ra sự khác biệt. Khai báo chúng bên ngoài và khởi tạo chúng trong trình xử lý rõ ràng là bị cấm. Cảm ơn cho ý tưởng mặc dù! – JohnG

Trả lời

12

Tìm thấy một cái gì đó mà làm việc:

  1. tôi đã AVPlayer một tài sản (cảm ơn cho meggar mẹo!)
  2. tôi đã chắc chắn rằng AVPlayer là con số không trước khi sử dụng phương pháp initWithAsset.

    NSArray *itemsFromQuery = [[MPMediaQuery songsQuery] items]; 
    MPMediaItem *song = [itemsFromQuery objectAtIndex:0]; 
    NSURL *songURL = [song valueForProperty:MPMediaItemPropertyAssetURL]; 
    AVURLAsset *urlAsset = [[AVURLAsset alloc] initWithURL:songURL options:nil]; 
    
    NSArray *keyArray = [[NSArray alloc] initWithObjects:@"tracks", nil]; 
    
    [urlAsset loadValuesAsynchronouslyForKeys:keyArray completionHandler:^{ 
    
        AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:urlAsset]; 
    
        player = nil; 
        player = [[AVPlayer alloc] initWithPlayerItem:playerItem]; 
    
        while (true) { 
         if (player.status == AVPlayerStatusReadyToPlay && playerItem.status == AVPlayerItemStatusReadyToPlay) 
          break; 
        } 
    
        [player play]; 
    
    }]; 
    

Hy vọng điều này sẽ giúp người ra!

+1

Cảm ơn rất nhiều. Có thật không. Cảm ơn. Rất nhiều. – duci9y

1

Một lý do khác là định cấu hình AVAudioSession. Đã làm cho tôi.

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 
    [[AVAudioSession sharedInstance] setActive: YES error: nil]; 
Các vấn đề liên quan