Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to improve this method?

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default How to improve this method?

    Hello

    I am trying to improve the method I post below in order to stop the track previously selected before the next starts, so that they dont play simultaneously... I cannot find a way to do so... (because this is part of an exercise of a book I am studying, I can only use a while-loop)

     
     
     * A class to hold details of audio tracks.
     * Individual tracks may be played.
     
    public class MusicOrganizer
    {
        // An ArrayList for storing music tracks.
        private ArrayList<Track> tracks;
        // A player for the music tracks.
        private MusicPlayer player;
        // A reader that can read music files and load them as tracks.
        private TrackReader reader;
     
     
                  [I]constructor and some methods omitted[/I]     
     
     
     /**
         * Play a track in the collection.
         * @param index The index of the track to be played.
         */
        public void playTrack(int index)
     
            // call the indexValid method to check if the parameter is a valid index number.
            if(indexValid(index)) {
                Track track = tracks.get(index);            
                player.startPlaying(track.getFilename());
                // increments by 1 every time a track is played
                track.incrementCount();
                System.out.println("Now playing: " + track.getArtist() + " - " + track.getTitle());
            }
        }

    the Player class has this method: stop() to stop playing the track.
    Last edited by bihlas; June 23rd, 2014 at 11:34 AM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,104
    My Mood
    Mellow
    Thanks
    244
    Thanked 755 Times in 741 Posts

    Default Re: How to improve this method?

    I don't really understand your question or what you mean by, "I can only use a while-loop." What you've described seems more appropriately done with an if() codition, something like:
    if ( trackIsNotAlreadyPlaying )
    {
        playANewTrack();
    }
    If you require a while() loop to do this same thing, please be specific or give us an example of how you might write it with any issues there are with your approach.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    bihlas (June 24th, 2014)

  4. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: How to improve this method?

    I dont necessarily need to use a while loop to do it. I assumed i did, because the section of the book the exercise is, is introducing the while loop...
    The whole point of my problem is how to stop playing the track I originally selected by calling the method for the first time, before a second one will start playing by calling the method for a second time. The way the method is written for now it will play the tracks simultaneously...
    There is this method inside MusicOrganiser class that calls a method of the MusicPlayer class in oreder to to stop playing the track that is currently playing.
     
     /**
         * Stop the player.
         */
        public void stopPlaying()
        {
            player.stop();
        }

    Now How to make use of this method inside the startPlaying method, so that to stop the previous track that started playing when I first called the method, before the next one starts by calling the method again...

    I am really puzzled and cant go on if I dont first solve this...

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,104
    My Mood
    Mellow
    Thanks
    244
    Thanked 755 Times in 741 Posts

    Default Re: How to improve this method?

    The method playTrack() should play the track passed to it, period. If checking for a track already playing and deciding what to do should occur before calling playTrack(). So back up the bus. Write down the requirements and logic flow for your application. A very small piece of that logic flow should be something like:

    User selects track to play
    If track already playing,
    - stop track currently playing
    Call playTrack() with user's selection

    The if() statement can be resolved using a flag (a boolean) that is set to true when a track is playing and false when there are no tracks playing.

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    bihlas (June 25th, 2014)

  7. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: How to improve this method?

    Greg Thank you very much for your time and help, I did it finally and I feel soooooo excited...
    I see it now and it seems so simple after all... I mean I struggled to find the solution thinking the wrong way and it was so straightforward...
    I am a very beginner in Object Oriented Programming but I am really hooked... Its absolutely amazing stuff...
    I post the improved method bellow... (just to show my success to somebody)

    public void playTrack(int index)
        {
            if(indexValid(index)) {
                Track track = tracks.get(index);
                if(TrackIsNotPlaying) {
                    player.startPlaying(track.getFilename());
                    track.incrementCount();
                    TrackIsNotPlaying = false;
                    System.out.println("Now playing: " + track.getArtist() + " - " + track.getTitle());
                }
                else {
                    stopPlaying();
                    player.startPlaying(track.getFilename());
                    System.out.println("Now playing: " + track.getArtist() + " - " + track.getTitle());
                }
            }
     
        }
    And I really appreciate the fact that you didnt just write the code and send it to me but you explained how to do it by my self...

Similar Threads

  1. Help improve this code
    By takami in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 9th, 2013, 12:04 AM
  2. How can I improve on this?
    By Erko in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 3rd, 2012, 03:42 PM
  3. I want to improve my code.
    By niigata in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 11th, 2012, 10:17 AM
  4. Help me improve my code.
    By JeremiahWalker in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 10th, 2012, 09:24 PM
  5. How can i improve this code?
    By ziplague in forum What's Wrong With My Code?
    Replies: 16
    Last Post: December 12th, 2011, 12:24 PM