2014-04-28 115 views
0

我已經嘗試了很多不同的方式,但他們都沒有工作。這個方法沒有錯誤,但什麼都不做。我將我的所有聲音放在我的主項目下的一個子目錄中。我嘗試過的大多數代碼都是不同的方式,如MediaPlayer和SoundPooling。他們都沒有爲我工作,所以我嘗試了這一點。如果有人能爲我糾正這個問題,或者讓我接受一個教程,那會很棒。在Android應用上播放MP3文件?

package me.javoris767.twds2soundboard; 

import java.io.IOException; 

import me.javoris767.twds2soundboard.R; 
import android.view.View; 
import android.app.Activity; 
import android.content.res.AssetFileDescriptor; 
import android.media.MediaPlayer; 
import android.os.Bundle; 


public class ClementinePage extends Activity { 
MediaPlayer mp=new MediaPlayer(); 
public boolean isPlaying; 

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

} 

public void playSound(String file) { 
    AssetFileDescriptor afd = null; 
    try { 
     afd = getAssets().openFd(file); 
     MediaPlayer player = new MediaPlayer(); 
     player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), 
       afd.getLength()); 

     player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
      public void onPrepared(MediaPlayer p1) { 
       p1.start(); 
       isPlaying = true; 
      } 
     }); 
}catch (IOException e) { 
    e.printStackTrace(); 
    } 
} 

public void onNoClick(View v) { 
    playSound("sounds/ohno.mp3"); 
} 
} 

回答

0

做這樣的事情

//initialising the two new buttons 
    final Button btSong1 = (Button)findViewById(R.id.btnPlaySong1); 
    final Button btSong2 = (Button)findViewById(R.id.btnPlaySong2); 

    //initialising the two new media player instances 
    song1 = new MediaPlayer(); 
    song1 = MediaPlayer.create(this, R.raw.song3); 

    song2 = new MediaPlayer(); 
    song2 = MediaPlayer.create(this, R.raw.song4); 



    //The code for the two buttons go here 

    btSong1.setOnClickListener(new OnClickListener(){ 
     public void onClick(View v){ 
     //This is where your code will go 
      switch(stateOfPlaying){ 
      case 0: 
       song1.start(); 
       stateOfPlaying = 1; 
       btSong1.setText("Pause Song 1"); 
       btSong2.setVisibility(View.INVISIBLE); 


       break; 
      case 1: 
       song1.pause(); 
       stateOfPlaying = 0; 
       btSong1.setText("Play Song 1"); 
       btSong2.setVisibility(View.VISIBLE); 
       break; 
      }//switch 

     }//end of onClick 
    });//btSong1 

    btSong2.setOnClickListener(new OnClickListener(){ 
     public void onClick(View v){ 
     //This is where your code will go 
      switch(stateOfPlaying){ 
      case 0: 
       song2.start(); 
       stateOfPlaying = 1; 
       btSong2.setText("Pause Song 2"); 
       btSong1.setVisibility(View.INVISIBLE); 



       break; 
      case 1: 
       song2.pause(); 
       stateOfPlaying = 0; 
       btSong2.setText("Play Song 2"); 
       btSong1.setVisibility(View.VISIBLE); 
       break; 
      }//switch 

     }//end of onClick 
    });//btSong2 
+0

我對** R.raw.song **/** R.id.btnPlaySong1 **行有個疑問。你如何設置這些?我不太瞭解它。 – user1947638

+0

要在您的應用中使用媒體,請在'res'文件夾中創建一個名爲'raw'的文件夾,並將您的音頻文件放在那裏'btnPlaySong1'是該按鈕的ID。 –

+0

現在工作嗎? –

0

我建議針對Android的使用MP3文件,因爲它似乎是處理OGG好得多。你能做這個嗎?

+0

是的,我可以很容易地轉換它們。 – user1947638