2015-10-31 75 views
0

我想在android studio中製作音樂應用程序。我的應用程序工作正常,除了它有一個小錯誤。在我的主要活動中,我有一個從資源文件夾中的原始文件夾導入的歌曲列表。我的第二個窗口是一個NowPlaying java文件,我可以暫停,轉發並查看歌曲的歌詞。在Android Studio中的音樂應用程序(媒體播放器)中的錯誤

當我播放主要活動的第一首歌時,它工作正常。但是,當我想在播放第一首歌曲時播放第二首歌曲時,第二首歌曲會在第一首歌曲的頂部播放。歌詞得到更新,seekbar得到更新,但兩首歌一起播放。但是,當我暫停第一首歌曲時,返回到主要活動並播放第二首歌曲時,不會發生這種情況。 enter image description here 我用: enter image description here

if(song != null){ 
    if(song.isPlaying()){ 
     song.reset(); song = null; 
    } 
} 

我知道有我的NowPlaying.java文件中的錯誤:所以這裏是整個代碼:

PS。我只是在學習過程中。

MediaPlayer song = null; 
SeekBar seekBar; 
Intent intent = getIntent(); 


protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_now_playing); 

    seekBar = (SeekBar) findViewById(R.id.seekBar); 
    //String selectedSong = getIntent().getExtras().getString("selectSong"); 


    selectSong(); 


    //for pause button 
    pauseButton(); 

    //for songtile 
    getSongtitle(); 

    getSongList(); 
    getNextButton(); 
    getPreviousButton(); 

} // end of onCreate method 


/** 
* Method for selecting the song 
* Moves to startSong method 
*/ 
public void selectSong(){ 


    TextView lyricsBox = (TextView) findViewById(R.id.lyricsBox); 
    lyricsBox.setMovementMethod(new ScrollingMovementMethod()); 
    String nextPlay = intent.getStringExtra("playSong"); 

    if(song != null){ 
     if(song.isPlaying()){ 
      song.reset(); 
      song = null; 
     } 
    } 

    if (nextPlay.equals("Demons")) { 
     song = MediaPlayer.create(this, R.raw.demons); 
     lyricsBox.setText(R.string.demonsLyrics); 

    } else if (nextPlay.equals("Black Space")) { 
     song = MediaPlayer.create(this, R.raw.blankspace); 
     lyricsBox.setText(R.string.blankspaceLyrics); 

    } else if (nextPlay.equals("Case 420")) { 
     song = MediaPlayer.create(this, R.raw.case420); 
     lyricsBox.setText(R.string.case420Lyrics); 
    } 
    else if(nextPlay.equals("Jalma")){ 
     song = MediaPlayer.create(this, R.raw.jalma); 
     lyricsBox.setText(R.string.jalmaLyrics); 
    } 
    else if(nextPlay.equals("Parelima")){ 
     song = MediaPlayer.create(this, R.raw.parelima); 
     lyricsBox.setText(R.string.parelimaLyrics); 
    } 

    else if(nextPlay.equals("Mero Balyakal Ko Sathi")){ 
     song = MediaPlayer.create(this, R.raw.balyakalsathi); 
     lyricsBox.setText(R.string.balyakalSathi); 
    } 

    else if(nextPlay.equals("Euta Sathi")){ 
     song = MediaPlayer.create(this, R.raw.eutasathi); 

    } 
    else if(nextPlay.equals("Audai Jadai")){ 
     song = MediaPlayer.create(this, R.raw.audaijadai); 

    } 
    else if(nextPlay.equals("Cheerleader")){ 
     song = MediaPlayer.create(this, R.raw.cheerleader); 

    } 



    // to start playing the song 
    startButton(); 
} 



//method to make sure seekbar updates till song ends 
Runnable run = new Runnable() { 
    @Override 
    public void run() { 
     getseekBar(); 
    } 
}; 

/** 
* Method to start song (play the song) 
* 
*/ 
public void startButton() { 
     song.start(); 
    //to update seek bar 
    getseekBar(); 
} 




/** 
* Method to update the seekbar. 
* implement touch in seekbar to change song position 
*/ 
public void getseekBar() { 
    seekBar.setMax(song.getDuration()); 
    seekBar.setProgress(song.getCurrentPosition()); 
    seekBar.postDelayed(run, 1000); 

    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 

     int seek_to; 
     @Override 
     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
      seek_to = progress; 

     } 

     @Override 
     public void onStartTrackingTouch(SeekBar seekBar) { 

     } 

     @Override 
     public void onStopTrackingTouch(SeekBar seekBar) { 
      song.seekTo(seek_to); 
     } 
    }); 
} 


/** 
* Method for pause Button 
* to pause song once clicked and change button background to play image 
* Again play the song if the button is pressed again. and change background back to pause image 
*/ 
public void pauseButton(){ 
    final Button playButton = (Button) findViewById(R.id.playButton); 

    playButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (song.isPlaying()) { 
       playButton.setBackgroundResource(R.drawable.playbutton); 
       song.pause(); 

      } else { 
       playButton.setBackgroundResource(R.drawable.pausebutton); 
       song.start(); 

      } 

     } 
    }); 
} 

/** 
* Method to get the song title from first java file and display in the title 
*/ 
public void getSongtitle(){ 

    Intent intent = getIntent(); 
    String nextPlay = intent.getStringExtra("playSong"); 
    TextView Songtitle = (TextView) findViewById(R.id.Songtitle); 
    Songtitle.setText(nextPlay); 
} 

/** 
* Method for song list button 
* Goes back to the first java file once the button is cliked, 
* displays the song list 
*/ 

public void getSongList(){ 

    Button lyricsButton = (Button) findViewById(R.id.lyricsButton); 
    lyricsButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      startActivity(new Intent(NowPlaying.this, MainActivity.class)); 
     } 
    }); 

} 

/** 
* Method for next button 
* the song skips every 10 seconds once clicked 
*/ 

public void getNextButton(){ 

    Button nextButton = (Button) findViewById(R.id.nextButton); 
    nextButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      int startTime = song.getCurrentPosition(); 
      int forwardTime = 10000; 
      startTime += forwardTime; 
      if(startTime <= song.getDuration()){ 

       song.seekTo(startTime); 
      } 

      else{ 
       song.stop(); 

      } 


     } 
    }); 

} // end of getNextButton 


/** 
* Method for previous button 
* the song skips back 10 seconds once clicked 
*/ 
public void getPreviousButton(){ 

    Button previousButton = (Button) findViewById(R.id.previousButton); 
    previousButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      int startTime = song.getCurrentPosition(); 
      int previousTime = 10000; 
      startTime -= previousTime; 

      if(startTime >= 0){ 
       song.seekTo(startTime); 
      } 
      else{ 
       song.seekTo(0); 
       song.start(); 
      } 
     } 
    }); 
} 

代碼主要活動:

公共類MainActivity擴展AppCompatActivity {

String[] songList; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    populateList(); 
} 

private void populateList(){ 
    songList = new String[] {"Jalma", "Demons", "Parelima", "Mero Balyakal Ko Sathi", "Audai Jadai", 
      "Case 420", "Euta Sathi", "Cheerleader"}; 

    ListAdapter arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
      songList); 

    ListView theList = (ListView) findViewById(R.id.theList); 
    theList.setAdapter(arrayAdapter); 

    theList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      ListView theList = (ListView) findViewById(R.id.theList); 
      startActivity(new Intent(MainActivity.this, NowPlaying.class)); 
      String selectedSong = (String) (theList.getItemAtPosition(position)); 

      Intent toSecondActivity = new Intent(getApplicationContext(), NowPlaying.class); 

      toSecondActivity.putExtra("playSong", selectedSong); 
      startActivity(toSecondActivity); 

     } 
    }); 


} 

}

+0

請張貼代碼MainActivity.java –

+0

你可以把Log.d設置爲onCreate()來查看正在調用的次數編輯。 – surlac

回答

0

良好,沒有在MainActivity類我不能肯定,但它不」看起來您在NowPlaying中使用了相同的MediaPlayer實例,所以我只能假設您已爲這兩個活動創建了一個新實例。

這可能意味着原始實例在第一個活動中仍處於活動狀態。

如果是這樣,您可以在切換活動之前銷燬MediaPlayer的MainActivity實例,也可以在兩個活動中使用相同的實例。

我會推薦第二種選擇,如果你只想要一次播放一首歌曲,你會覺得有意義,然後看看Singleton類。在這種情況下,我相信這樣做會很好。

http://www.javaworld.com/article/2073352/core-java/simply-singleton.html

編輯:

給此一去,

theList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     String selectedSong = (String) (theList.getItemAtPosition(position)); 

     Intent toSecondActivity = new Intent(MainActivity.this, NowPlaying.class); 
     toSecondActivity.putExtra("playSong", selectedSong); 
     startActivity(toSecondActivity); 

    } 
}); 

,你將需要做出的thelist最終

+0

這裏是主要活動的代碼: –

+0

您可以請檢查主要活動代碼並幫助我 –

+0

我已經編輯了我的文章,您應該閱讀此解釋爲什麼您不應該使用getApplicationContext。 http://stackoverflow.com/questions/33447762/difference-btn-mcontext-get-getapplicationcontext-and-getactivity/33447949#33447949 – BrendanDodd

0
I am also facing the same issue.Songs are playing on top of each other. Finally I found the answer from a video. 
https://www.youtube.com/watch?v=4DC4XFWVFls 


//For Play Button OnClick Listener 

public void onClick(View v) { 

       if(mediaPlayer !=null) 
       { 
        mediaPlayer.release(); 
       } 
       mediaPlayer = MediaPlayer.create(context, songList.getSong()); 
       mediaPlayer.start(); 
} 

//For Stop Button 

public void onClick(View v) { 
       mediaPlayer.stop(); 
      } 

This code worked properly for me. 
+0

這段代碼不起作用,因爲你有兩個'onClick(View v)'方法。 – arcticwhite

+0

@arcticwhite ..我有兩個按鈕用於播放和停止 –

+0

是的,你有兩個按鈕,但你也有兩個相同的方法。那麼你的按鈕怎麼知道你點擊了哪一個? – arcticwhite

相關問題