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 8357531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:30:30+00:00 2026-06-09T10:30:30+00:00

I am trying to create a music player with start/stop functionality. I do this

  • 0

I am trying to create a music player with start/stop functionality. I do this by starting a while loop that loops if a boolean value is true. This while loop is contained in a separate thread. I start this thread in the viewDidLoad method:

[[CoreDataHelper getEditableSong].audioPlayer playOrStopSong];

playOrStopSong looks like this:

- (void) playOrStopSong {
NSLog(@"%d --- before switch", playing);
if (playing) {
    playing = NO;
}
else{
    playing = YES;
    [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
}
NSLog(@"%d --- after switch", playing);

}

My run method looks like this (most of it is probably unimportant):

- (void) run {
@autoreleasepool {
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(handleTimer:) userInfo:nil repeats:NO];
    xPlayPosition = 0;
    NSLog(@"thread started!");
    while (playing) {
        NSLog(@"lots of playings -- %d", playing);
        NSMutableDictionary *notesAtCurrentX = [song.noteDict objectForKey:[NSNumber numberWithInt:xPlayPosition]];
        if (notesAtCurrentX != nil) {
            NSString *currentTemplate = [song.soundTemplate objectAtIndex:(NSUInteger) (xPlayPosition/NOTES_PER_TEMPLATE)];
            [self playColumnWithTemplate:currentTemplate andNotesAtCurrentX:notesAtCurrentX andCurrentX:xPlayPosition];
        }
        [NSThread sleepForTimeInterval:30/[song.tempo floatValue]];
        xPlayPosition += 1;
        if (xPlayPosition > [song.length intValue]-1) {
            xPlayPosition = 0;
            [myComposeViewController performSelectorOnMainThread: @selector(animatePlaybackLine) withObject:nil waitUntilDone:NO];
        }
    }
    NSLog(@"thread stopped!");
    [NSThread exit];
}

}

This thread detaches and runs just like it is supposed to. The log prints like so:

0 --- before switch
thread started!
1 --- after switch

And then goes on to print “lots of playings — 1” a bunch of times.

But when I try to call the playOrStopSong method again, instead of stopping the thread, I just get a new one. The log looks like this:

lots of playings1 -- 1
lots of playings1 -- 1
0 --- before switch
1 --- after switch
thread started!
lots of playings1 -- 1
after click on stop --- 1
lots of playings1 -- 1
lots of playings1 -- 1

It says that it is playing (lots of playings1 — 1), but then it says that it is not playing (0 — before switch). This is almost certainly where the problem is. Where did I go wrong? If it helps answer the question, it may be helpful to know that I think that I had this working in an earlier version of my project. (though that seems implausible now…)

Thanks for the help!

  • 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-09T10:30:32+00:00Added an answer on June 9, 2026 at 10:30 am

    What your trace shows is that the old thread never stops, but a new one is started. This would indicate that you’re calling playOrStopSong on a newly created (non running) audioPlayer, not the one you actually started earlier.

    I can’t see what the life cycle of the audioPlayer is (or is intended to be) from the code in the question, but putting a trace statement inside the initializer of the audioPlayer will most likely show that you’re creating a new one when you don’t actually intend to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a music player/centre online. I have a player that
I'm trying to create a music player that will be visible in all my
I'm trying to create a C# application that extracts data from pages like this
I am trying to create a Music Player App in J2ME. I want to
I'm trying to create a shell script that runs a command and plays music
So I am trying to create a RTSP server that streams music. I do
I'm trying to start a media player that streams from a website. Right now,
I am trying to create a music player in J2ME. I want to read
I am trying to create an MP3 Player where the music carries on playing
Hi I'm trying to create a simple music library for my application that uses

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.