Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8019809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:30:15+00:00 2026-06-04T21:30:15+00:00

Xcode for iOS The issue that I’m having is in the playback of an

  • 0

Xcode for iOS

The issue that I’m having is in the playback of an AVMutableComposition. I’m creating the composition then adding AVURLAssets to it inside a for loop. I’m adding everything as I would similarly add an object to a mutable array and then attempting to play it back afterwards, but playback isn’t occuring. Here is my code:

AVMutableComposition *composition = [AVMutableComposition composition];

int count;

count = array.count;

for (int y=0;y<count;y++){    

        NSURL *url;

            url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@.mp3", [[NSBundle mainBundle] resourcePath],[array objectAtIndex:y]]];





        NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];

        AVURLAsset *sourceAsset = [[AVURLAsset alloc] initWithURL:url options:options];

        //calculate times
        NSNumber *time = [soundArray1 objectAtIndex:1];

        double timenow = [time doubleValue];
        double insertTime = (240*y);

        CMTime douTime = CMTimeMake(240, timenow);
        CMTime staTime = CMTimeMake(0, 1);
        CMTimeRange editRange = CMTimeRangeMake(staTime,douTime);

        CMTime insTime = CMTimeMake(insertTime, timenow);


        NSError *editError;


        [composition insertTimeRange:editRange ofAsset:sourceAsset atTime:insTime error:&editError];

    }


AVPlayer* mp = [AVPlayer playerWithPlayerItem:[AVPlayerItem playerItemWithAsset:composition]];


[mp play];

I’d assume that I am getting it completely wrong, but I then added this inside the loop instead of after, inside an if and opening a NSRunLoop to check (a little bad, but it was just to test!):

    AVMutableComposition *composition = [AVMutableComposition composition];

int count;

count = array.count;

for (int y=0;y<count;y++){    

        NSURL *url;

            url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@.mp3", [[NSBundle mainBundle] resourcePath],[array objectAtIndex:y]]];





        NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];

        AVURLAsset *sourceAsset = [[AVURLAsset alloc] initWithURL:url options:options];

        //calculate times
        NSNumber *time = [soundArray1 objectAtIndex:1];

        double timenow = [time doubleValue];
        double insertTime = (240*y);

        CMTime douTime = CMTimeMake(240, timenow);
        CMTime staTime = CMTimeMake(0, 1);
        CMTimeRange editRange = CMTimeRangeMake(staTime,douTime);

        CMTime insTime = CMTimeMake(insertTime, timenow);


        NSError *editError;


        [composition insertTimeRange:editRange ofAsset:sourceAsset atTime:insTime error:&editError];



    if(y==3){

    AVPlayer* mp = [AVPlayer playerWithPlayerItem:[AVPlayerItem playerItemWithAsset:composition]];


    [mp play];


    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:20]];

        }
    }

Here, I just gave my array 4 elements and made it play the composition on the 4th run-through. It worked and played my 4 audio files in perfect order.

What do I need to do to make my composition play after this loop rather than in it? Am I mis-using the system?

EDIT: by sticking my open RunLoop code on the end, it plays. Should I be doing it this way, or is there a better way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:30:17+00:00Added an answer on June 4, 2026 at 9:30 pm

    So I was going about this the wrong way. I needed to create an AVMutableCompositionTrack for each element and add that to my composition

           NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
                                                                forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
    
            AVURLAsset *sourceAsset = [[AVURLAsset alloc] initWithURL:url options:options];
    
            //calculate times
            NSNumber *time = [soundArray1 objectAtIndex:1];
    
            double timenow = [time doubleValue];
    
            double insertTime = (240*y);
    
                        AVMutableCompositionTrack *track =
            [composition addMutableTrackWithMediaType:AVMediaTypeAudio
                                     preferredTrackID:kCMPersistentTrackID_Invalid];
    
            //insert the audio track from the asset into the track added to the mutable composition
            AVAssetTrack *myTrack = [[sourceAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
            CMTimeRange myTrackRange = myTrack.timeRange;
            NSError *error = nil;
            [track insertTimeRange:myTrackRange
                                          ofTrack:myTrack
                                           atTime:CMTimeMake(insertTime, timenow) 
                                            error:&error];
    
            [sourceAsset release];
    

    followed by

    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:composition];
    self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
    
    
            [self.player play];
    

    with player declared as a property in the interface section

    @interface SecondViewController ()
    
    
    
    @property (nonatomic, assign) AVPlayer *player;
    
    
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an iOS 5 app (in Xcode 4.3, using Storyboards and ARC) that
Having just upgraded to iOS 4.1 (Xcode 3.2.4) I am getting warnings when using
Main issue is that I'm developing an app for Android and iOS but can't
I'm new to iOS development and I'm having a bit of an issue deploying
I have an iOS project in Xcode that works great. I added the classes
I currently am having a issue with my main XIB. I am using iOS
The recent upgrade to XCode 3.2.4 and iOS SDK 4.1 lead to that my
For the old version xcode/ios, I used: appDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate]; to access
So I was doing a WebView for iOS on Xcode, everything is superfine but
I'm new to Xcode and programming for iOS, so I might not get what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.