简体   繁体   English

在Android App上播放MP3文件?

[英]Playing MP3 files on an Android App?

I've tried many different ways, but none of them seem to work. 我尝试了许多不同的方法,但是似乎没有一个起作用。 This method gets no errors, but does nothing. 此方法没有错误,但不执行任何操作。 I'm placing all of my sounds in a subdir under my main project. 我将所有声音放在主项目下的子目录中。 Most of the code I've tried was different ways like the MediaPlayer and SoundPooling. 我尝试过的大多数代码都是使用MediaPlayer和SoundPooling之类的不同方法。 None of them worked for me and so I tried this. 他们都没有为我工作,所以我尝试了这个。 If anyone could correct this for me or get me to a tutorial, that would be great. 如果有人可以为我更正此问题或让我上教程,那将很棒。

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");
}
}

Do something like this 做这样的事情

//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

I'd advise against using MP3 files in Android because it seems to be handling OGG ones much better. 我建议不要在Android中使用MP3文件,因为它似乎可以更好地处理OGG文件。 Can you do this? 你能做这个吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM