简体   繁体   English

如何在android中播放音频文件

[英]how to play audio file in android

I have a mp3 file in my android mobile, lets it's a xyz.mp3 somewhere in my sdcard.我的 android 手机中有一个 mp3 文件,让它在我的 sdcard 某处是一个 xyz.mp3。 How to play it through my application?如何通过我的应用程序播放它?

Simply you can use MediaPlayer and play the audio file. 只需使用MediaPlayer播放音频文件。 Check out this nice example for playing Audio: 看看这个播放音频的好例子

 public void audioPlayer(String path, String fileName){
    //set up MediaPlayer    
    MediaPlayer mp = new MediaPlayer();

    try {
        mp.setDataSource(path + File.separator + fileName);
        mp.prepare();
        mp.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Niranjan, If you are using a raw file from res/raw folder, ie., reading a file stored inside the project, we can use: @Niranjan,如果您正在使用res / raw文件夹中的原始文件,即读取存储在项目中的文件,则可以使用:

mediaplayer.setDataSource(context, Uri.parse("android.resource://urpackagename/res/raw/urmp3name");

If you have to use from SD card: 如果必须使用SD卡:

 MediaPlayer mediaPlayer = new MediaPlayer();
 File path = android.os.Environment.getExternalStorageDirectory();
 mediaPlayer.setDataSource(path + "urmp3filename");

See this related question: MediaPlayer issue between raw folder and sdcard on android 请参阅此相关问题: Android上原始文件夹和sdcard之间的MediaPlayer问题

    public class MainActivity extends Activity implements OnClickListener {
    Button play;
    MediaPlayer mp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        play=(Button)findViewById(R.id.button1);
        play.setOnClickListener(this);

    }
    @Override
    public void onClick(View arg0)
    {
        mp=MediaPlayer.create(getApplicationContext(),R.raw.song);// the song is a filename which i have pasted inside a folder **raw** created under the **res** folder.//
        mp.start();


    }

    @Override
    protected void onDestroy() {
        mp.release();
        super.onDestroy();
    }

}

If the audio is in the local raw resource: 如果音频在本地原始资源中:

MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start(); // no need to call prepare(); create() does that for you

To play from a URI available locally in the system: 要从系统本地可用的URI播放:

Uri myUri = ....; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();

The replay from https://stackoverflow.com/users/726863/lalit-poptani is great one, it worked the first time, but as I used to have the full path of the file, I did it this way来自https://stackoverflow.com/users/726863/lalit-poptani的重播很棒,它第一次工作,但因为我曾经拥有文件的完整路径,所以我是这样做的

public void audioPlayer(String path){
        //set up MediaPlayer
        MediaPlayer mp = new MediaPlayer();

        try {
            mp.setDataSource(path );
            mp.prepare();
            mp.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Credit to http://www.helloandroid.com/tutorials/how-play-video-and-audio-android归功于http://www.helloandroid.com/tutorials/how-play-video-and-audio-android

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

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