Search code examples
androidmedia-playerupdates

How to update media player to play new file


I have a section of code shown below that gets the pathname for an mp3 audio file stored in the content provider database. Next it uses this pathname to load the mp3 file and start playing it. The problem is that if I get a different pathname and hit pause or play it keeps playing the same old audio file, not the new file. What is the best way to solve this problem?

      String pathName; // pathname of audio file stored in SD card
      String selection = MediaStore.Images.Media.DATA + "='" + pathName +"'";
      String[] projection = { MediaStore.Images.Media.LATITUDE };
      cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, selection, null, null);

      if(cursor!=null){
         cursor.moveToFirst();
        }
        musicName = (String) cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.LATITUDE));

         Toast.makeText(AudioService.this, "from AUDIOSERVICE CLASS music name: " + musicName, Toast.LENGTH_SHORT).show();

         cursor.close();    

         player.setDataSource(musicName);

        player.prepare();

        player.start();

        }

Solution

  • check if path is same then play same file again and if file path is different then first reset mediaplayer and then play new file change your code as:

          String pathName=""; // pathname of audio file stored in SD card
           public static String musicName="";
          String selection = MediaStore.Images.Media.DATA + "='" + pathName +"'";
          String[] projection = { MediaStore.Images.Media.LATITUDE };
          cursor = getContentResolver().query(MediaStore.Images.
                 Media.EXTERNAL_CONTENT_URI, projection, selection, null, null);
    
          if(cursor!=null){
             cursor.moveToFirst();
            }
    
    if(musicName.length()==0){
             musicName = (String) cursor.getString(cursor.getColumnIndex(
                                          MediaStore.Images.Media.LATITUDE));
    
             Toast.makeText(AudioService.this, 
                                        "from AUDIOSERVICE CLASS music name:
                                          " + musicName, Toast.LENGTH_SHORT).show();
    
             cursor.close();    
    
             player.setDataSource(musicName);
    
            player.prepare();
    
            player.start();
    
      }
    else{
     String musicNametemp = (String) cursor.getString(cursor.getColumnIndex(
                                              MediaStore.Images.Media.LATITUDE));
    
    if(musicNametemp.equals(musicName)){
    
    ///play old music here
    }
    
    else{
    //reset mediaplayer here
    
    if(player !=null){
     player.reset();
     player.release();
     player = new MediaPlayer();
    
    }
    cursor.close();    
    musicName=musicNametemp;
    player.setDataSource(musicName);
    
    player.prepare();
    
    player.start();
    }
    
    }